Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for openWidgetsOnFocus prop #154

Merged
merged 1 commit into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ Displays an input field complete with custom inputs, native input, a calendar, a
|onChange|Function called when the user clicks an item on the most detailed view available.|n/a|`(value) => alert('New date is: ', value)`|
|onClockClose|Function called when the clock closes.|n/a|`() => alert('Clock closed')`|
|onClockOpen|Function called when the clock opens.|n/a|`() => alert('Clock opened')`|
|openWidgetsOnFocus|Whether to open the widgets on input focus.|`true`|`false`|
|returnValue|Which dates shall be passed by the calendar to the onChange function and onClick{Period} functions. Can be `"start"`, `"end"` or `"range"`. The latter will cause an array with start and end values to be passed.|` "start"`|`"range"`|
|required|Whether datetime input should be required.|`false`|`true`|
|secondAriaLabel|`aria-label` for the second input.|n/a|`"Second"`|
Expand Down
36 changes: 22 additions & 14 deletions src/DateTimePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ export default class DateTimePicker extends PureComponent {
}

onFocus = (event) => {
const { disabled, onFocus } = this.props;
const {
disabled,
onFocus,
openWidgetsOnFocus,
} = this.props;

if (onFocus) {
onFocus(event);
Expand All @@ -122,19 +126,21 @@ export default class DateTimePicker extends PureComponent {
return;
}

switch (event.target.name) {
case 'day':
case 'month':
case 'year':
this.openCalendar();
break;
case 'hour12':
case 'hour24':
case 'minute':
case 'second':
this.openClock();
break;
default:
if (openWidgetsOnFocus) {
switch (event.target.name) {
case 'day':
case 'month':
case 'year':
this.openCalendar();
break;
case 'hour12':
case 'hour24':
case 'minute':
case 'second':
this.openClock();
break;
default:
}
}
}

Expand Down Expand Up @@ -429,6 +435,7 @@ DateTimePicker.defaultProps = {
isCalendarOpen: null,
isClockOpen: null,
maxDetail: 'minute',
openWidgetsOnFocus: true,
};

const isValue = PropTypes.oneOfType([
Expand Down Expand Up @@ -482,6 +489,7 @@ DateTimePicker.propTypes = {
onClockClose: PropTypes.func,
onClockOpen: PropTypes.func,
onFocus: PropTypes.func,
openWidgetsOnFocus: PropTypes.bool,
required: PropTypes.bool,
secondAriaLabel: PropTypes.string,
secondPlaceholder: PropTypes.string,
Expand Down
120 changes: 98 additions & 22 deletions src/DateTimePicker.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -317,40 +317,116 @@ describe('DateTimePicker', () => {
expect(calendar2).toHaveLength(1);
});

it('opens Calendar component when focusing on a date input inside', () => {
const component = mount(
<DateTimePicker />,
);
describe('handles opening Calendar component when focusing on an input inside properly', () => {
it('opens Calendar component when focusing on an input inside by default', () => {
const component = mount(
<DateTimePicker />,
);

const calendar = component.find('Calendar');
const input = component.find('input[name="day"]');
const calendar = component.find('Calendar');
const input = component.find('input[name="day"]');

expect(calendar).toHaveLength(0);
expect(calendar).toHaveLength(0);

input.simulate('focus');
component.update();
input.simulate('focus');
component.update();

const calendar2 = component.find('Calendar');
const calendar2 = component.find('Calendar');

expect(calendar2).toHaveLength(1);
expect(calendar2).toHaveLength(1);
});

it('opens Calendar component when focusing on an input inside given openWidgetsOnFocus = true', () => {
const component = mount(
<DateTimePicker openWidgetsOnFocus />,
);

const calendar = component.find('Calendar');
const input = component.find('input[name="day"]');

expect(calendar).toHaveLength(0);

input.simulate('focus');
component.update();

const calendar2 = component.find('Calendar');

expect(calendar2).toHaveLength(1);
});

it('does not open Calendar component when focusing on an input inside given openWidgetsOnFocus = false', () => {
const component = mount(
<DateTimePicker openWidgetsOnFocus={false} />,
);

const calendar = component.find('Calendar');
const input = component.find('input[name="day"]');

expect(calendar).toHaveLength(0);

input.simulate('focus');
component.update();

const calendar2 = component.find('Calendar');

expect(calendar2).toHaveLength(0);
});
});

it('opens Clock component when focusing on a time input inside', () => {
const component = mount(
<DateTimePicker />,
);
describe('handles opening Clock component when focusing on an input inside properly', () => {
it('opens Clock component when focusing on an input inside by default', () => {
const component = mount(
<DateTimePicker />,
);

const clock = component.find('Clock');
const input = component.find('input[name^="hour"]');
const clock = component.find('Clock');
const input = component.find('input[name^="hour"]');

expect(clock).toHaveLength(0);
expect(clock).toHaveLength(0);

input.simulate('focus');
component.update();
input.simulate('focus');
component.update();

const clock2 = component.find('Clock');
const clock2 = component.find('Clock');

expect(clock2).toHaveLength(1);
expect(clock2).toHaveLength(1);
});

it('opens Clock component when focusing on an input inside given openWidgetsOnFocus = true', () => {
const component = mount(
<DateTimePicker openWidgetsOnFocus />,
);

const clock = component.find('Clock');
const input = component.find('input[name^="hour"]');

expect(clock).toHaveLength(0);

input.simulate('focus');
component.update();

const clock2 = component.find('Clock');

expect(clock2).toHaveLength(1);
});

it('does not open Clock component when focusing on an input inside given openWidgetsOnFocus = false', () => {
const component = mount(
<DateTimePicker openWidgetsOnFocus={false} />,
);

const clock = component.find('Clock');
const input = component.find('input[name^="hour"]');

expect(clock).toHaveLength(0);

input.simulate('focus');
component.update();

const clock2 = component.find('Clock');

expect(clock2).toHaveLength(0);
});
});

it('closes Calendar component when clicked outside', () => {
Expand Down