Skip to content

Commit beaa53e

Browse files
author
Ben Fox
committed
Add boolean prop to allow datepicker input to maintain focus when selected/interacted with
1 parent ec4631a commit beaa53e

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed

components/component-docs.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5960,6 +5960,17 @@
59605960
"computed": false
59615961
}
59625962
},
5963+
"canInputMaintainFocus": {
5964+
"type": {
5965+
"name": "bool"
5966+
},
5967+
"required": false,
5968+
"description": "When `true`, clicking or typing in `Input` will not result in focus shifting away from `Input`",
5969+
"defaultValue": {
5970+
"value": "false",
5971+
"computed": false
5972+
}
5973+
},
59635974
"className": {
59645975
"type": {
59655976
"name": "union",

components/date-picker/__docs__/storybook-stories.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Datepicker from '../../date-picker';
88
import { DATE_PICKER } from '../../../utilities/constants';
99

1010
import Default from '../__examples__/default';
11+
import MaintainInputFocus from '../__examples__/maintain-input-focus'
1112
import IsoWeekdays from '../__examples__/iso-weekday';
1213
import CustomInput from '../__examples__/custom-input';
1314
import SnaphotDefault from '../__examples__/snapshot-default';
@@ -33,6 +34,7 @@ storiesOf(DATE_PICKER, module)
3334
.add('Default - Right to Left (RTL)', () =>
3435
makeRtl(<Default action={action} />)
3536
)
37+
.add('Maintain Input Focus', () => <MaintainInputFocus action={action} canInputMaintainFocus />)
3638
.add('ISO weekdays', () => <IsoWeekdays action={action} />)
3739
.add('Custom Input', () => <CustomInput action={action} />)
3840
.add('Inline menu', () => (
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* eslint-disable no-console, react/prop-types */
2+
import React from 'react';
3+
4+
import IconSettings from '~/components/icon-settings';
5+
import Datepicker from '~/components/date-picker';
6+
7+
class Example extends React.Component {
8+
static displayName = 'DatepickerExample';
9+
10+
render() {
11+
return (
12+
<IconSettings iconPath="/assets/icons">
13+
<Datepicker
14+
canInputMaintainFocus
15+
labels={{
16+
label: 'Date',
17+
}}
18+
onChange={(event, data) => {
19+
if (this.props.action) {
20+
const dataAsArray = Object.keys(data).map((key) => data[key]);
21+
this.props.action('onChange')(event, data, ...dataAsArray);
22+
} else if (console) {
23+
console.log('onChange', event, data);
24+
}
25+
}}
26+
onCalendarFocus={(event, data) => {
27+
if (this.props.action) {
28+
const dataAsArray = Object.keys(data).map((key) => data[key]);
29+
this.props.action('onCalendarFocus')(event, data, ...dataAsArray);
30+
} else if (console) {
31+
console.log('onCalendarFocus', event, data);
32+
}
33+
}}
34+
/>
35+
</IconSettings>
36+
);
37+
}
38+
}
39+
40+
export default Example; // export is replaced with `ReactDOM.render(<Example />, mountNode);` at runtime

components/date-picker/date-picker.jsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ const propTypes = {
5252
* Aligns the right or left side of the menu with the respective side of the trigger. _Tested with snapshot testing._
5353
*/
5454
align: PropTypes.oneOf(['left', 'right']),
55+
/**
56+
* When `true`, clicking or typing in `Input` will not result in focus shifting away from `Input`
57+
*/
58+
canInputMaintainFocus: PropTypes.bool,
5559
/**
5660
* CSS classes to be added to tag with `slds-datepicker`. If you are looking for the outer DOM node (slds-dropdown-trigger), please review `triggerClassName`. _Tested with snapshot testing._
5761
*/
@@ -197,6 +201,7 @@ const defaultProps = {
197201
previousMonth: 'Previous month',
198202
year: 'Year',
199203
},
204+
canInputMaintainFocus: false,
200205
formatter(date) {
201206
return date
202207
? `${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear()}`
@@ -309,6 +314,7 @@ class Datepicker extends React.Component {
309314
this.props.assistiveTextPreviousMonth || assistiveText.previousMonth // eslint-disable-line react/prop-types
310315
}
311316
assistiveTextYear={assistiveText.year}
317+
canStealFocus={!this.props.canInputMaintainFocus}
312318
id={this.getId()}
313319
isIsoWeekday={this.props.isIsoWeekday}
314320
monthLabels={
@@ -524,6 +530,11 @@ class Datepicker extends React.Component {
524530
this.setState({ isOpen: true });
525531
}
526532

533+
if (event.keyCode === KEYS.ESCAPE || event.keyCode === KEYS.ENTER) {
534+
EventUtil.trapEvent(event);
535+
this.setState({ isOpen: false });
536+
}
537+
527538
// Please remove `onKeyDown` on the next breaking change.
528539
/* eslint-disable react/prop-types */
529540
if (this.props.onKeyDown) {
@@ -537,7 +548,7 @@ class Datepicker extends React.Component {
537548
this.props.onOpen(event, { portal });
538549
}
539550

540-
if (this.selectedDateCell) {
551+
if (this.selectedDateCell && !this.props.canInputMaintainFocus) {
541552
this.selectedDateCell.focus();
542553
}
543554
};

components/date-picker/private/calendar-wrapper.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class DatepickerCalendarWrapper extends React.Component {
4040
* One letter abbreviations of the days of the week, starting on Sunday.
4141
*/,
4242
abbreviatedWeekDayLabels: PropTypes.array.isRequired,
43+
/**
44+
* Whether or not the `CalendarWrapper` can steal focus from the main `Input`
45+
*/
46+
canStealFocus: PropTypes.bool.isRequired,
4347
/**
4448
* CSS classes to be added to tag with `slds-datepicker`.
4549
*/
@@ -108,7 +112,7 @@ class DatepickerCalendarWrapper extends React.Component {
108112

109113
state = {
110114
initialDateForCalendarRender: this.props.selectedDate,
111-
isCalendarFocused: true,
115+
isCalendarFocused: this.props.canStealFocus,
112116
};
113117

114118
handleCalendarBlur = (event, { direction }) => {

0 commit comments

Comments
 (0)