Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat(modal): add optional return focus after close (#1424)
- Loading branch information
1 parent
a590880
commit 33cfce6
Showing
3 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* eslint react/no-multi-comp: 0, react/prop-types: 0 */ | ||
|
||
import React from 'react'; | ||
import { Button, Modal, ModalBody, ModalFooter, Label, Input, FormGroup, Form } from 'reactstrap'; | ||
|
||
class ModalFocusAfterClose extends React.Component { | ||
constructor() { | ||
super(); | ||
this.state = { | ||
open: false, | ||
focusAfterClose: true | ||
}; | ||
this.toggle = this.toggle.bind(this); | ||
this.handleSelectChange = this.handleSelectChange.bind(this); | ||
} | ||
|
||
toggle() { | ||
this.setState(({ open }) => ({ open: !open })); | ||
} | ||
|
||
handleSelectChange({target: { value }}) { | ||
this.setState({ focusAfterClose: JSON.parse(value) }); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<Form inline onSubmit={(e) => e.preventDefault()}> | ||
<FormGroup> | ||
<Label for="focusAfterClose">Focus After Close</Label> | ||
<Input className="mx-2" type="select" id="focusAfterClose" onChange={this.handleSelectChange}> | ||
<option value="true">Yes</option> | ||
<option value="false">No</option> | ||
</Input> | ||
</FormGroup> | ||
<Button color="danger" onClick={this.toggle}>Open</Button> | ||
</Form> | ||
<Modal returnFocusAfterClose={this.state.focusAfterClose} isOpen={this.state.open}> | ||
<ModalBody> | ||
Observe the "Open" button. It will be focused after close when "returnFocusAfterClose" is true and will not be focused if "returnFocusAfterClose" is false. | ||
</ModalBody> | ||
<ModalFooter> | ||
<Button color="primary" onClick={this.toggle}>Close</Button> | ||
</ModalFooter> | ||
</Modal> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default ModalFocusAfterClose; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters