@ -42,22 +42,65 @@ class PrivacyDropdownMenu extends React.PureComponent {
}
}
handle Click = e => {
if ( e . key === 'Escape' ) {
this . props . onClose ( ) ;
} else if ( ! e . key || e . key === 'Enter' ) {
const value = e . currentTarget . getAttribute ( 'data-index' ) ;
e . preventDefault ( ) ;
handle KeyDown = e => {
const { items } = this . props ;
const value = e . currentTarget . getAttribute ( 'data-index' ) ;
const index = items . findIndex ( item => {
return ( item . value === value ) ;
} ) ;
let element ;
switch ( e . key ) {
case 'Escape' :
this . props . onClose ( ) ;
this . props . onChange ( value ) ;
break ;
case 'Enter' :
this . handleClick ( e ) ;
break ;
case 'ArrowDown' :
element = this . node . childNodes [ index + 1 ] ;
if ( element ) {
element . focus ( ) ;
this . props . onChange ( element . getAttribute ( 'data-index' ) ) ;
}
break ;
case 'ArrowUp' :
element = this . node . childNodes [ index - 1 ] ;
if ( element ) {
element . focus ( ) ;
this . props . onChange ( element . getAttribute ( 'data-index' ) ) ;
}
break ;
case 'Home' :
element = this . node . firstChild ;
if ( element ) {
element . focus ( ) ;
this . props . onChange ( element . getAttribute ( 'data-index' ) ) ;
}
break ;
case 'End' :
element = this . node . lastChild ;
if ( element ) {
element . focus ( ) ;
this . props . onChange ( element . getAttribute ( 'data-index' ) ) ;
}
break ;
}
}
handleClick = e => {
const value = e . currentTarget . getAttribute ( 'data-index' ) ;
e . preventDefault ( ) ;
this . props . onClose ( ) ;
this . props . onChange ( value ) ;
}
componentDidMount ( ) {
document . addEventListener ( 'click' , this . handleDocumentClick , false ) ;
document . addEventListener ( 'touchend' , this . handleDocumentClick , listenerOptions ) ;
if ( this . focusedItem ) this . focusedItem . focus ( ) ;
this . setState ( { mounted : true } ) ;
}
@ -70,6 +113,10 @@ class PrivacyDropdownMenu extends React.PureComponent {
this . node = c ;
}
setFocusRef = c => {
this . focusedItem = c ;
}
render ( ) {
const { mounted } = this . state ;
const { style , items , value } = this . props ;
@ -80,9 +127,9 @@ class PrivacyDropdownMenu extends React.PureComponent {
// It should not be transformed when mounting because the resulting
// size will be used to determine the coordinate of the menu by
// react-overlays
< div className = 'privacy-dropdown__dropdown' style = { { ... style , opacity : opacity , transform : mounted ? ` scale( ${ scaleX } , ${ scaleY } ) ` : null } } r ef= { this . setRef } >
< div className = 'privacy-dropdown__dropdown' style = { { ... style , opacity : opacity , transform : mounted ? ` scale( ${ scaleX } , ${ scaleY } ) ` : null } } r ole= 'listbox' r ef= { this . setRef } >
{ items . map ( item => (
< div role = ' butt on' tabIndex = '0' key = { item . value } data - index = { item . value } onKeyDown = { this . handle Click } onClick = { this . handleClick } className = { classNames ( 'privacy-dropdown__option' , { active : item . value === value } ) } >
< div role = ' opti on' tabIndex = '0' key = { item . value } data - index = { item . value } onKeyDown = { this . handle KeyDown } onClick = { this . handleClick } className = { classNames ( 'privacy-dropdown__option' , { active : item . value === value } ) } aria - selected = { item . value === value } ref = { item . value === value ? this . setFocusRef : null } >
< div className = 'privacy-dropdown__option__icon' >
< i className = { ` fa fa-fw fa- ${ item . icon } ` } / >
< / d i v >
@ -147,9 +194,6 @@ export default class PrivacyDropdown extends React.PureComponent {
handleKeyDown = e => {
switch ( e . key ) {
case 'Enter' :
this . handleToggle ( e ) ;
break ;
case 'Escape' :
this . handleClose ( ) ;
break ;