Skip to content

Commit 575d3c9

Browse files
authored
Add support for openClockOnFocus prop (#119)
1 parent 9219b5a commit 575d3c9

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ Displays an input field complete with custom inputs, native input and a clock.
107107
|onChange|Function called when the user picks a valid time.|n/a|`(value) => alert('New time is: ', value)`|
108108
|onClockClose|Function called when the clock closes.|n/a|`() => alert('Clock closed')`|
109109
|onClockOpen|Function called when the clock opens.|n/a|`() => alert('Clock opened')`|
110+
|openClockOnFocus|Whether to open the clock on input focus.|`true`|`false`|
110111
|required|Whether date input should be required.|`false`|`true`|
111112
|secondAriaLabel|`aria-label` for the second input.|n/a|`"Second"`|
112113
|secondPlaceholder|`placeholder` for the second input.|`"--"`|`"ss"`|

src/TimePicker.jsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default class TimePicker extends PureComponent {
7171
}
7272

7373
onFocus = (event) => {
74-
const { disabled, onFocus } = this.props;
74+
const { disabled, onFocus, openClockOnFocus } = this.props;
7575

7676
if (onFocus) {
7777
onFocus(event);
@@ -82,7 +82,9 @@ export default class TimePicker extends PureComponent {
8282
return;
8383
}
8484

85-
this.openClock();
85+
if (openClockOnFocus) {
86+
this.openClock();
87+
}
8688
}
8789

8890
openClock = () => {
@@ -310,6 +312,7 @@ TimePicker.defaultProps = {
310312
closeClock: true,
311313
isOpen: null,
312314
maxDetail: 'minute',
315+
openClockOnFocus: true,
313316
};
314317

315318
const isValue = PropTypes.oneOfType([
@@ -351,6 +354,7 @@ TimePicker.propTypes = {
351354
onClockClose: PropTypes.func,
352355
onClockOpen: PropTypes.func,
353356
onFocus: PropTypes.func,
357+
openClockOnFocus: PropTypes.bool,
354358
required: PropTypes.bool,
355359
secondAriaLabel: PropTypes.string,
356360
secondPlaceholder: PropTypes.string,

src/TimePicker.spec.jsx

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -248,22 +248,60 @@ describe('TimePicker', () => {
248248
expect(clock2).toHaveLength(1);
249249
});
250250

251-
it('opens Clock component when focusing on an input inside', () => {
252-
const component = mount(
253-
<TimePicker />,
254-
);
251+
describe('handles opening Clock component when focusing on an input inside properly', () => {
252+
it('opens Clock component when focusing on an input inside by default', () => {
253+
const component = mount(
254+
<TimePicker />,
255+
);
255256

256-
const clock = component.find('Clock');
257-
const input = component.find('input[name^="hour"]');
257+
const clock = component.find('Clock');
258+
const input = component.find('input[name^="hour"]');
258259

259-
expect(clock).toHaveLength(0);
260+
expect(clock).toHaveLength(0);
260261

261-
input.simulate('focus');
262-
component.update();
262+
input.simulate('focus');
263+
component.update();
263264

264-
const clock2 = component.find('Clock');
265+
const clock2 = component.find('Clock');
265266

266-
expect(clock2).toHaveLength(1);
267+
expect(clock2).toHaveLength(1);
268+
});
269+
270+
it('opens Clock component when focusing on an input inside given openClockOnFocus = true', () => {
271+
const component = mount(
272+
<TimePicker openClockOnFocus />,
273+
);
274+
275+
const clock = component.find('Clock');
276+
const input = component.find('input[name^="hour"]');
277+
278+
expect(clock).toHaveLength(0);
279+
280+
input.simulate('focus');
281+
component.update();
282+
283+
const clock2 = component.find('Clock');
284+
285+
expect(clock2).toHaveLength(1);
286+
});
287+
288+
it('does not open Clock component when focusing on an input inside given openClockOnFocus = false', () => {
289+
const component = mount(
290+
<TimePicker openClockOnFocus={false} />,
291+
);
292+
293+
const clock = component.find('Clock');
294+
const input = component.find('input[name^="hour"]');
295+
296+
expect(clock).toHaveLength(0);
297+
298+
input.simulate('focus');
299+
component.update();
300+
301+
const clock2 = component.find('Clock');
302+
303+
expect(clock2).toHaveLength(0);
304+
});
267305
});
268306

269307
it('closes Clock component when clicked outside', () => {

0 commit comments

Comments
 (0)