Skip to content
This repository was archived by the owner on Oct 19, 2021. It is now read-only.

Commit 3bc0c37

Browse files
emyarodjoshblack
authored andcommitted
fix(Modal): add support for primary focus selector prop (#1124)
* fix(Modal): add support for primary focus selector prop * docs(ModalWrapper): add example of `selectorPrimaryFocus` in use * refactor(Modal): rename parameter to be in line with current convention * chore(ModalWrapper): update snapshot
1 parent df3d9d5 commit 3bc0c37

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

src/components/Modal/Modal.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default class Modal extends Component {
2525
onSecondarySubmit: PropTypes.func,
2626
danger: PropTypes.bool,
2727
shouldSubmitOnEnter: PropTypes.bool,
28+
selectorPrimaryFocus: PropTypes.string,
2829
};
2930

3031
static defaultProps = {
@@ -36,6 +37,7 @@ export default class Modal extends Component {
3637
iconDescription: 'close the modal',
3738
modalHeading: '',
3839
modalLabel: '',
40+
selectorPrimaryFocus: '[data-modal-primary-focus]',
3941
};
4042

4143
button = React.createRef();
@@ -63,19 +65,26 @@ export default class Modal extends Component {
6365
}
6466
}
6567

66-
focusButton = () => {
68+
focusButton = evt => {
69+
const primaryFocusElement = evt.currentTarget.querySelector(
70+
this.props.selectorPrimaryFocus
71+
);
72+
if (primaryFocusElement) {
73+
primaryFocusElement.focus();
74+
return;
75+
}
6776
if (this.button) {
6877
this.button.current.focus();
6978
}
7079
};
7180

72-
handleTransitionEnd = () => {
81+
handleTransitionEnd = evt => {
7382
if (
7483
this.outerModal.offsetWidth &&
7584
this.outerModal.offsetHeight &&
7685
this.beingOpen
7786
) {
78-
this.focusButton();
87+
this.focusButton(evt);
7988
this.beingOpen = false;
8089
}
8190
};

src/components/ModalWrapper/ModalWrapper-story.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,60 @@ storiesOf('ModalWrapper', module)
152152
</RadioButtonGroup>
153153
</ModalWrapper>
154154
)
155+
)
156+
.addWithInfo(
157+
'selectorPrimaryFocus',
158+
`
159+
The 'selectorPrimaryFocus' prop can be used to focus on any single element when the modal is opened. The example shows an input field being focused on modal open, rather than the default behavior of focusing on the 'Save' button.
160+
`,
161+
() => (
162+
<ModalWrapper
163+
id="input-modal"
164+
buttonTriggerText="Input Modal"
165+
modalHeading="Modal with inputs and custom focus selector"
166+
handleSubmit={action('onSubmit')}>
167+
<TextInput
168+
id="test2"
169+
placeholder="Hint text here"
170+
label="Text Input:"
171+
data-modal-primary-focus
172+
/>
173+
<br />
174+
<Select id="select-1" labelText="Select">
175+
<SelectItem
176+
disabled
177+
hidden
178+
value="placeholder-item"
179+
text="Pick an option"
180+
/>
181+
<SelectItem value="option-1" text="Option 1" />
182+
<SelectItem value="option-2" text="Option 2" />
183+
<SelectItem value="option-3" text="Option 3" />
184+
</Select>
185+
<br />
186+
<RadioButtonGroup
187+
name="radio-button-group"
188+
defaultSelected="default-selected">
189+
<RadioButton
190+
value="default-selected"
191+
id="radio-1"
192+
labelText="Radio Button label 1"
193+
className="some-class"
194+
/>
195+
<RadioButton
196+
value="standard"
197+
labelText="Radio Button label 2"
198+
id="radio-2"
199+
className="some-class"
200+
/>
201+
<RadioButton
202+
value="standard"
203+
labelText="Radio Button label 3"
204+
id="radio-3"
205+
className="some-class"
206+
disabled
207+
/>
208+
</RadioButtonGroup>
209+
</ModalWrapper>
210+
)
155211
);

src/components/ModalWrapper/__snapshots__/ModalWrapper-test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ exports[`ModalWrapper should render 1`] = `
5252
primaryButtonDisabled={false}
5353
primaryButtonText="Save"
5454
secondaryButtonText="Cancel"
55+
selectorPrimaryFocus="[data-modal-primary-focus]"
5556
>
5657
<div
5758
className="bx--modal bx--modal-tall"
5859
id="modal"
5960
onClick={[Function]}
6061
onKeyDown={[Function]}
6162
role="presentation"
63+
selectorPrimaryFocus="[data-modal-primary-focus]"
6264
tabIndex={-1}
6365
>
6466
<div

0 commit comments

Comments
 (0)