Skip to content

Commit 2ad8adb

Browse files
TeaSeaLancsleMaik
authored andcommitted
Add property passdowns and autoOpen properties (#28)
* Add TimePickerProps and ClockProps * Add autoOpen prop * Add specs
1 parent c46e234 commit 2ad8adb

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

src/TimeInput.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ class TimeInput extends React.Component {
2525
defaultValue.setSeconds(0)
2626
defaultValue.setMilliseconds(0)
2727

28+
const open = !!props.autoOpen
29+
const value = props.value || props.defaultValue || props.initialTime || defaultValue
30+
2831
this.state = {
29-
open: false,
30-
value: props.value || props.defaultValue || props.initialTime || defaultValue,
31-
hasChanged: false
32+
open,
33+
value,
34+
hasChanged: false,
35+
newValue: open ? value : null
3236
}
3337
}
3438

@@ -80,6 +84,7 @@ class TimeInput extends React.Component {
8084
render () {
8185
const {
8286
autoOk,
87+
autoOpen, // eslint-disable-line
8388
cancelLabel,
8489
classes,
8590
defaultValue,
@@ -91,6 +96,8 @@ class TimeInput extends React.Component {
9196
okLabel,
9297
onChange,
9398
value: valueProp,
99+
ClockProps,
100+
TimePickerProps,
94101
...other
95102
} = this.props
96103

@@ -114,11 +121,13 @@ class TimeInput extends React.Component {
114121
onClose={this.handleCancel}
115122
>
116123
<TimePicker
124+
{...TimePickerProps}
117125
mode={mode}
118126
value={newValue}
119127
onChange={this.handleChange}
120128
onMinutesSelected={autoOk ? this.handleOk : null}
121129
classes={{ header: classes.header, body: classes.body }}
130+
ClockProps={ClockProps}
122131
/>
123132
<DialogActions>
124133
<Button onClick={this.handleCancel} color='primary'>{cancelLabel}</Button>
@@ -133,6 +142,8 @@ class TimeInput extends React.Component {
133142
TimeInput.propTypes = {
134143
/** If true, automatically accept and close the picker on set minutes. */
135144
autoOk: PropTypes.bool,
145+
/** If true, automatically opens the dialog when the component is mounted */
146+
autoOpen: PropTypes.bool,
136147
/** Override the label of the cancel button. */
137148
cancelLabel: PropTypes.string,
138149
/** This default value overrides initialTime and placeholder. */
@@ -150,7 +161,11 @@ TimeInput.propTypes = {
150161
/** Callback that is called with the new date (as Date instance) when the value is changed. */
151162
onChange: PropTypes.func,
152163
/** The value of the time picker, for use in controlled mode. */
153-
value: PropTypes.instanceOf(Date)
164+
value: PropTypes.instanceOf(Date),
165+
/** Properties to pass down to the TimePicker component */
166+
TimePickerProps: PropTypes.object,
167+
/** Properties to pass down to the Clock component */
168+
ClockProps: PropTypes.object
154169
}
155170

156171
TimeInput.defaultProps = {

src/TimeInput.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { unwrap } from '@material-ui/core/test-utils'
66
import Button from '@material-ui/core/Button'
77
import Input from '@material-ui/core/Input'
88
import TimeInput from './TimeInput'
9+
import TimePicker from './TimePicker'
910
import Clock from './Clock'
1011
import * as testUtils from '../test/utils'
1112

@@ -192,6 +193,32 @@ describe('<TimeInput />', () => {
192193
const tree = mount(<TimeInput value={new Date(2017, 10, 15, 13, 37, 0, 0)} mode='24h' inputComponent={CustomInput} />)
193194
expect(tree.find(CustomInput).length).toBe(1)
194195
})
196+
197+
it('automatically opens when the autoOpen prop is true', () => {
198+
const tree = mount(<TimeInput />)
199+
expect(tree.find(TimePicker).length).toBe(0)
200+
201+
const tree2 = mount(<TimeInput autoOpen />)
202+
expect(tree2.find(TimePicker).length).toBe(1)
203+
})
204+
205+
it('supports sending properties down to the time picker via TimePickerProps', () => {
206+
const TimePickerProps = {
207+
some: 'timePickerProp'
208+
}
209+
210+
const tree = mount(<TimeInput TimePickerProps={TimePickerProps} autoOpen />)
211+
expect(tree.find(TimePicker).prop('some')).toEqual('timePickerProp')
212+
})
213+
214+
it('supports sending properties down to the clock via ClockProps', () => {
215+
const ClockProps = {
216+
some: 'clockProp'
217+
}
218+
219+
const tree = mount(<TimeInput ClockProps={ClockProps} autoOpen />)
220+
expect(tree.find(Clock).prop('some')).toEqual('clockProp')
221+
})
195222
})
196223

197224
function getValue (timeInput) {

src/TimePicker.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ class TimePicker extends React.Component {
136136
render () {
137137
const {
138138
classes,
139-
mode
139+
mode,
140+
ClockProps
140141
} = this.props
141142

142143
const clockMode = this.state.select === 'm' ? 'minutes' : mode
@@ -181,6 +182,7 @@ class TimePicker extends React.Component {
181182
</div>
182183
<div className={classes.body}>
183184
<Clock
185+
{...ClockProps}
184186
mode={clockMode}
185187
onChange={this.handleClockChange}
186188
value={clockMode === 'minutes' ? minutes : hours}
@@ -203,7 +205,9 @@ TimePicker.propTypes = {
203205
/** Callback that is called when the minutes are changed. Can be used to automatically hide the picker after selecting a time. */
204206
onMinutesSelected: PropTypes.func,
205207
/** The value of the time picker, for use in controlled mode. */
206-
value: PropTypes.instanceOf(Date)
208+
value: PropTypes.instanceOf(Date),
209+
/** Properties to pass down to the Clock component */
210+
ClockProps: PropTypes.object
207211
}
208212

209213
TimePicker.defaultProps = {

0 commit comments

Comments
 (0)