|
| 1 | +import PropTypes from 'prop-types' |
| 2 | +import React from 'react' |
| 3 | + |
| 4 | +const getTrackConfig = ({ error, source, target, disabled }) => { |
| 5 | + const basicStyle = { |
| 6 | + left: `${source.percent}%`, |
| 7 | + width: `calc(${target.percent - source.percent}% - 1px)`, |
| 8 | + } |
| 9 | + |
| 10 | + if (disabled) return basicStyle |
| 11 | + |
| 12 | + const coloredTrackStyle = error |
| 13 | + ? { |
| 14 | + backgroundColor: 'rgba(214,0,11,0.5)', |
| 15 | + borderLeft: '1px solid rgba(214,0,11,0.5)', |
| 16 | + borderRight: '1px solid rgba(214,0,11,0.5)', |
| 17 | + } |
| 18 | + : { |
| 19 | + backgroundColor: 'rgba(98, 203, 102, 0.5)', |
| 20 | + borderLeft: '1px solid #62CB66', |
| 21 | + borderRight: '1px solid #62CB66', |
| 22 | + } |
| 23 | + |
| 24 | + return { ...basicStyle, ...coloredTrackStyle } |
| 25 | +} |
| 26 | + |
| 27 | +const Track = ({ error, source, target, getTrackProps, disabled }) => ( |
| 28 | + <div |
| 29 | + className={`react_time_range__track${disabled ? '__disabled' : ''}`} |
| 30 | + style={getTrackConfig({ error, source, target, disabled })} |
| 31 | + {...getTrackProps()} |
| 32 | + /> |
| 33 | +) |
| 34 | + |
| 35 | +Track.propTypes = { |
| 36 | + source: PropTypes.shape({ |
| 37 | + id: PropTypes.string.isRequired, |
| 38 | + value: PropTypes.number.isRequired, |
| 39 | + percent: PropTypes.number.isRequired |
| 40 | + }).isRequired, |
| 41 | + target: PropTypes.shape({ |
| 42 | + id: PropTypes.string.isRequired, |
| 43 | + value: PropTypes.number.isRequired, |
| 44 | + percent: PropTypes.number.isRequired |
| 45 | + }).isRequired, |
| 46 | + getTrackProps: PropTypes.func.isRequired, |
| 47 | + disabled: PropTypes.bool |
| 48 | +} |
| 49 | + |
| 50 | +Track.defaultProps = { disabled: false } |
| 51 | + |
| 52 | +export default Track |
0 commit comments