diff --git a/src/Clock.js b/src/Clock.js old mode 100644 new mode 100755 index 2229092..96d329d --- a/src/Clock.js +++ b/src/Clock.js @@ -35,7 +35,8 @@ const styles = (theme) => ({ userSelect: 'none', '&.selected': { color: getContrastRatio(theme.palette.primary.main, theme.palette.common.black) < 7 ? theme.palette.common.white : theme.palette.common.black - } + }, + '&.disabled': { opacity: '0.3' } }, smallNumber: { fontSize: '12px', @@ -134,7 +135,7 @@ class Clock extends React.PureComponent { } render () { - const { classes, mode, value, ...other } = this.props + const { classes, mode, value, minutesStep, ...other } = this.props const { touching } = this.state return ( @@ -191,7 +192,9 @@ class Clock extends React.PureComponent { {mode === 'minutes' && getNumbers(12, { size, start: 5, step: 5 }).map((digit, i) => ( this.setState({ open: true, newValue: this.state.value }) + showDialog = () => this.setState({ open: true }) - handleChange = (newValue) => { - this.setState({ newValue }) + handleChange = (value) => { + if (this.props.updateImmediately && this.props.onChange != null) { + this.props.onChange(value) + } + this.setState({ value }) } handleOk = () => { - if (this.props.onChange != null) { - this.props.onChange(this.state.newValue) + if (!this.props.updateImmediately && this.props.onChange != null) { + this.props.onChange(this.state.value) } - this.setState({ open: false, value: this.state.newValue, newValue: null }) + this.setState({ open: false, initValue: this.state.value }) } - handleCancel = () => this.setState({ open: false, newValue: null }) + handleCancel = () => { + if (this.props.updateImmediately && this.props.onChange != null) { + this.props.onChange(this.state.initValue) + } + this.setState({ open: false, value: this.state.initValue }) + } render () { const { @@ -59,15 +68,19 @@ class TimeInput extends React.Component { okLabel, onChange, // eslint-disable-line value: valueProp, // eslint-disable-line + minutesStep, + selectOnClose, + updateImmediately, ...other } = this.props - const { value, newValue } = this.state + const { initValue, value } = this.state + const inputValue = updateImmediately ? value : initValue - const { hours, isPm } = formatHours(value.getHours(), mode) + const { hours, isPm } = formatHours(inputValue.getHours(), mode) const formattedValue = mode === '12h' - ? `${hours}:${twoDigits(value.getMinutes())} ${isPm ? 'pm' : 'am'}` - : `${twoDigits(value.getHours())}:${twoDigits(value.getMinutes())}` + ? `${hours}:${twoDigits(inputValue.getMinutes())} ${isPm ? 'pm' : 'am'}` + : `${twoDigits(inputValue.getHours())}:${twoDigits(inputValue.getMinutes())}` const { muiFormControl } = this.context const disabled = disabledProp || (muiFormControl != null && muiFormControl.disabled) @@ -80,16 +93,18 @@ class TimeInput extends React.Component { value={formattedValue} readOnly key='TimeInput-input' + classes={{ input: classes.input }} />, ', () => { tree.find(Button).at(0).simulate('click') expect(getValue(tree)).toBe('13:37') // unchanged - expect(changeHandler).not.toHaveBeenCalled() }) }) }) diff --git a/src/TimePicker.js b/src/TimePicker.js old mode 100644 new mode 100755 index 8e85cb9..c8dbbe6 --- a/src/TimePicker.js +++ b/src/TimePicker.js @@ -83,10 +83,8 @@ class TimePicker extends React.Component { } else { this.setState({ hours: value }, this.propagateChange) } - } else { - this.setState({ minutes: value }, () => { - this.propagateChange() - }) + } else if (value % (this.props.minutesStep) === 0) { + this.setState({ minutes: value }, this.propagateChange) } } @@ -186,6 +184,7 @@ class TimePicker extends React.Component { value={clockMode === 'minutes' ? minutes : hours} onMouseUp={this.handleClockChangeDone} onTouchEnd={this.handleClockChangeDone} + minutesStep={this.props.minutesStep} /> @@ -196,6 +195,8 @@ class TimePicker extends React.Component { TimePicker.propTypes = { /** The initial value of the time picker. */ defaultValue: PropTypes.instanceOf(Date), + /** Steps between minutes. */ + minutesStep: PropTypes.number, /** Sets the clock mode, 12-hour or 24-hour clocks are supported. */ mode: PropTypes.oneOf(['12h', '24h']), /** Callback that is called with the new date (as Date instance) when the value is changed. */ @@ -207,7 +208,8 @@ TimePicker.propTypes = { } TimePicker.defaultProps = { - mode: '12h' + mode: '12h', + minutesStep: 1 } export default withStyles(styles)(TimePicker) diff --git a/src/__snapshots__/Clock.spec.js.snap b/src/__snapshots__/Clock.spec.js.snap index 330069b..eb81c6a 100644 --- a/src/__snapshots__/Clock.spec.js.snap +++ b/src/__snapshots__/Clock.spec.js.snap @@ -20,6 +20,7 @@ exports[` 12h matches the snapshot 1`] = ` "smallPointer": "Clock-smallPointer-7", } } + minutesStep={1} mode="12h" value={7} > @@ -209,6 +210,7 @@ exports[` 24h matches the snapshot 1`] = ` "smallPointer": "Clock-smallPointer-7", } } + minutesStep={1} mode="24h" value={7} > @@ -530,6 +532,7 @@ exports[` minutes matches the snapshot 1`] = ` "smallPointer": "Clock-smallPointer-7", } } + minutesStep={1} mode="minutes" value={42} > diff --git a/src/__snapshots__/TimeInput.spec.js.snap b/src/__snapshots__/TimeInput.spec.js.snap index a2edf34..117a62a 100644 --- a/src/__snapshots__/TimeInput.spec.js.snap +++ b/src/__snapshots__/TimeInput.spec.js.snap @@ -15,10 +15,18 @@ exports[` 12h matches the snapshot 1`] = ` } } defaultValue={2017-10-15T13:37:00.000Z} + minutesStep={1} mode="12h" okLabel="Ok" + selectOnClose={false} + updateImmediately={false} > 24h matches the snapshot 1`] = ` } } defaultValue={2017-10-15T13:37:00.000Z} + minutesStep={1} mode="24h" okLabel="Ok" + selectOnClose={false} + updateImmediately={false} > 12h matches the snapshot 1`] = ` "time": "TimePicker-time-3", } } + minutesStep={1} mode="12h" value={2017-10-15T13:37:00.000Z} > @@ -65,6 +66,7 @@ exports[` 12h matches the snapshot 1`] = ` className="TimePicker-body-7" > 12h matches the snapshot 1`] = ` "smallPointer": "Clock-smallPointer-14", } } + minutesStep={1} mode="12h" onChange={[Function]} onMouseUp={[Function]} @@ -282,6 +285,7 @@ exports[` 24h matches the snapshot 1`] = ` "time": "TimePicker-time-3", } } + minutesStep={1} mode="24h" value={2017-10-15T13:37:00.000Z} > @@ -317,6 +321,7 @@ exports[` 24h matches the snapshot 1`] = ` className="TimePicker-body-7" > 24h matches the snapshot 1`] = ` "smallPointer": "Clock-smallPointer-14", } } + minutesStep={1} mode="24h" onChange={[Function]} onMouseUp={[Function]}