Fix dropdown focus state after a re-open

This commit is contained in:
Oswaldo Acauan 2018-04-16 11:06:29 -03:00
parent 928097c7ad
commit 23d8356dbd
3 changed files with 22 additions and 11 deletions

View File

@ -122,6 +122,7 @@ class Dropdown extends Component {
trigger = React.cloneElement(trigger, { trigger = React.cloneElement(trigger, {
ref: (ref) => { this.trigger = ref; }, ref: (ref) => { this.trigger = ref; },
dropdownIsOpen: this.state.isOpen,
dropdownToggle: this.handleToggle, dropdownToggle: this.handleToggle,
dropdownShow: this.handleShow, dropdownShow: this.handleShow,
dropdownHide: this.handleHide, dropdownHide: this.handleHide,
@ -130,6 +131,7 @@ class Dropdown extends Component {
content = React.cloneElement(content, { content = React.cloneElement(content, {
ref: (ref) => { this.content = ref; }, ref: (ref) => { this.content = ref; },
'aria-expanded': this.state.isOpen, 'aria-expanded': this.state.isOpen,
dropdownIsOpen: this.state.isOpen,
dropdownToggle: this.handleToggle, dropdownToggle: this.handleToggle,
dropdownShow: this.handleShow, dropdownShow: this.handleShow,
dropdownHide: this.handleHide, dropdownHide: this.handleHide,

View File

@ -26,13 +26,15 @@ const defaultProps = {
export default class DropdownContent extends Component { export default class DropdownContent extends Component {
render() { render() {
const { const {
placement, className, children, style, placement, children, className,
dropdownToggle, dropdownShow, dropdownHide, dropdownIsOpen,
...restProps
} = this.props; } = this.props;
const { dropdownToggle, dropdownShow, dropdownHide } = this.props;
const placementName = placement.split(' ').join('-'); const placementName = placement.split(' ').join('-');
const boundChildren = Children.map(children, child => cloneElement(child, { const boundChildren = Children.map(children, child => cloneElement(child, {
dropdownIsOpen,
dropdownToggle, dropdownToggle,
dropdownShow, dropdownShow,
dropdownHide, dropdownHide,
@ -40,10 +42,9 @@ export default class DropdownContent extends Component {
return ( return (
<div <div
style={style}
aria-expanded={this.props['aria-expanded']}
data-test="dropdownContent" data-test="dropdownContent"
className={cx(styles.content, styles[placementName], className)} className={cx(styles.content, styles[placementName], className)}
{...restProps}
> >
<div className={styles.scrollable}> <div className={styles.scrollable}>
{boundChildren} {boundChildren}

View File

@ -33,6 +33,9 @@ const defaultProps = {
export default class DropdownList extends Component { export default class DropdownList extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = {
focusedIndex: false,
};
this.childrenRefs = []; this.childrenRefs = [];
this.menuRefs = []; this.menuRefs = [];
@ -40,22 +43,27 @@ export default class DropdownList extends Component {
this.handleItemClick = this.handleItemClick.bind(this); this.handleItemClick = this.handleItemClick.bind(this);
} }
componentWillMount() {
this.setState({
focusedIndex: false,
});
}
componentDidMount() { componentDidMount() {
this._menu.addEventListener('keydown', event => this.handleItemKeyDown(event)); this._menu.addEventListener('keydown', event => this.handleItemKeyDown(event));
} }
componentDidUpdate() { componentWillReceiveProps(nextProps) {
if (nextProps.dropdownIsOpen === false) {
this.setState({
focusedIndex: false,
});
}
}
componentDidUpdate(prevProps) {
console.log(this.props, prevProps);
const { focusedIndex } = this.state; const { focusedIndex } = this.state;
const children = [].slice.call(this._menu.children); const children = [].slice.call(this._menu.children);
this.menuRefs = children.filter(child => child.getAttribute('role') === 'menuitem'); this.menuRefs = children.filter(child => child.getAttribute('role') === 'menuitem');
console.log(focusedIndex);
const activeRef = this.menuRefs[focusedIndex]; const activeRef = this.menuRefs[focusedIndex];
if (activeRef) { if (activeRef) {