mirror of https://github.com/sgoudham/carbon.git
Add support to keyboard navigation in Dropdowns
The user can navigate through the Themes and Languages selects using the keyboard arrow down and up keys.main
parent
46091f6dc5
commit
83d30234a3
@ -1,134 +1,155 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import enhanceWithClickOutside from 'react-click-outside'
|
import Downshift from 'downshift'
|
||||||
import ArrowDown from './svg/Arrowdown'
|
import ArrowDown from './svg/Arrowdown'
|
||||||
import Checkmark from './svg/Checkmark'
|
import CheckMark from './svg/Checkmark'
|
||||||
import { COLORS } from '../lib/constants'
|
import { COLORS } from '../lib/constants'
|
||||||
|
|
||||||
class Dropdown extends React.Component {
|
const Dropdown = ({ list, selected, onChange }) => {
|
||||||
constructor(props) {
|
return (
|
||||||
super(props)
|
<Downshift
|
||||||
this.state = {
|
render={renderDropdown(list)}
|
||||||
isVisible: false
|
selectedItem={selected}
|
||||||
}
|
defaultHighlightedIndex={list.findIndex(it => it === selected)}
|
||||||
this.select = this.select.bind(this)
|
itemToString={item => item.name}
|
||||||
this.toggle = this.toggle.bind(this)
|
onChange={onChange}
|
||||||
}
|
stateReducer={reduceState(list)}
|
||||||
|
/>
|
||||||
select(item) {
|
)
|
||||||
if (this.props.selected !== item) {
|
}
|
||||||
this.props.onChange(item)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
toggle() {
|
const reduceState = list => (state, changes) => {
|
||||||
this.setState({ isVisible: !this.state.isVisible })
|
switch (changes.type) {
|
||||||
|
case Downshift.stateChangeTypes.keyDownArrowUp:
|
||||||
|
case Downshift.stateChangeTypes.keyDownArrowDown:
|
||||||
|
return { ...changes, selectedItem: list[changes.highlightedIndex] }
|
||||||
|
default:
|
||||||
|
return changes
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
handleClickOutside() {
|
const renderDropdown = list => ({
|
||||||
this.setState({ isVisible: false })
|
isOpen,
|
||||||
}
|
highlightedIndex,
|
||||||
|
setHighlightedIndex,
|
||||||
|
selectHighlightedItem,
|
||||||
|
selectedItem,
|
||||||
|
getRootProps,
|
||||||
|
getButtonProps,
|
||||||
|
getInputProps,
|
||||||
|
getItemProps
|
||||||
|
}) => {
|
||||||
|
return (
|
||||||
|
<DropdownContainer {...getRootProps({ refKey: 'innerRef' })} minWidth={minWidth(list)}>
|
||||||
|
<SelectedItem {...getButtonProps()} {...getInputProps()} isOpen={isOpen}>
|
||||||
|
{selectedItem.name}
|
||||||
|
</SelectedItem>
|
||||||
|
{isOpen ? (
|
||||||
|
<ListItems>
|
||||||
|
{list.map((item, index) => (
|
||||||
|
<ListItem
|
||||||
|
key={index}
|
||||||
|
{...getItemProps({
|
||||||
|
item,
|
||||||
|
isSelected: selectedItem === item,
|
||||||
|
isHighlighted: highlightedIndex === index
|
||||||
|
})}
|
||||||
|
>
|
||||||
|
{item.name}
|
||||||
|
</ListItem>
|
||||||
|
))}
|
||||||
|
</ListItems>
|
||||||
|
) : null}
|
||||||
|
</DropdownContainer>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
renderListItems() {
|
const DropdownContainer = ({ children, innerRef, minWidth, ...rest }) => {
|
||||||
return this.props.list.map((item, i) => (
|
return (
|
||||||
<div className="dropdown-list-item" key={i} onClick={this.select.bind(null, item)}>
|
<div {...rest} ref={innerRef} className="dropdown-container">
|
||||||
<span>{item.name}</span>
|
{children}
|
||||||
{this.props.selected === item ? <Checkmark /> : null}
|
|
||||||
<style jsx>{`
|
<style jsx>{`
|
||||||
.dropdown-list-item {
|
.dropdown-container {
|
||||||
display: flex;
|
min-width: ${minWidth}px;
|
||||||
align-items: center;
|
cursor: pointer;
|
||||||
justify-content: space-between;
|
|
||||||
background: ${COLORS.BLACK};
|
|
||||||
user-select: none;
|
|
||||||
padding: 8px 16px;
|
|
||||||
border-bottom: 1px solid ${COLORS.SECONDARY};
|
|
||||||
}
|
|
||||||
|
|
||||||
.dropdown-list-item:hover {
|
|
||||||
background: ${COLORS.HOVER};
|
|
||||||
}
|
|
||||||
|
|
||||||
.dropdown-list-item:last-of-type {
|
|
||||||
border-bottom: none;
|
|
||||||
border-radius: 0px 0px 2px 2px;
|
|
||||||
}
|
}
|
||||||
`}</style>
|
`}</style>
|
||||||
</div>
|
</div>
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
|
||||||
// find longest list value in number of characters
|
|
||||||
const MIN_WIDTH = this.props.list.reduce(
|
|
||||||
(max, { name }) => (name && name.length > max ? name.length : max),
|
|
||||||
0
|
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const SelectedItem = ({ children, isOpen, ...rest }) => {
|
||||||
return (
|
return (
|
||||||
<div
|
<span {...rest} tabIndex="0" className="dropdown-display">
|
||||||
className="dropdown-container"
|
<span className="dropdown-display-text">{children}</span>
|
||||||
style={{ minWidth: MIN_WIDTH * 14 }}
|
<div className={`dropdown-arrow ${isOpen ? 'is-reverse' : ''}`}>
|
||||||
onClick={this.toggle}
|
|
||||||
>
|
|
||||||
<div className={`dropdown-display ${this.state.isVisible ? 'is-visible' : ''}`}>
|
|
||||||
<span>{this.props.selected ? this.props.selected.name : ''}</span>
|
|
||||||
<div className="arrow-down">
|
|
||||||
<ArrowDown />
|
<ArrowDown />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
<div className="dropdown-list">{this.renderListItems()}</div>
|
|
||||||
<style jsx>{`
|
<style jsx>{`
|
||||||
.arrow-down {
|
|
||||||
position: absolute;
|
|
||||||
right: 16px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.is-visible > .arrow-down {
|
|
||||||
transform: rotate(180deg);
|
|
||||||
}
|
|
||||||
|
|
||||||
.is-visible {
|
|
||||||
border-radius: 3px 3px 0px 0px !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.dropdown-container {
|
|
||||||
height: 100%;
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
.dropdown-display {
|
.dropdown-display {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
border: 1px solid ${COLORS.SECONDARY};
|
border: 1px solid ${COLORS.SECONDARY};
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
user-select: none;
|
|
||||||
padding: 8px 16px;
|
padding: 8px 16px;
|
||||||
display: flex;
|
|
||||||
justify-content: flex-start;
|
|
||||||
align-items: center;
|
|
||||||
position: relative;
|
|
||||||
z-index: 1;
|
|
||||||
}
|
}
|
||||||
|
.dropdown-display-text {
|
||||||
.dropdown-display:hover {
|
flex-grow: 1;
|
||||||
background: ${COLORS.HOVER};
|
|
||||||
}
|
}
|
||||||
|
.dropdown-arrow.is-reverse {
|
||||||
.is-visible + .dropdown-list {
|
transform: rotate(180deg);
|
||||||
display: block;
|
|
||||||
}
|
}
|
||||||
|
`}</style>
|
||||||
|
</span>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const ListItems = ({ children }) => {
|
||||||
|
return (
|
||||||
|
<ul className="dropdown-list">
|
||||||
|
{children}
|
||||||
|
<style jsx>{`
|
||||||
.dropdown-list {
|
.dropdown-list {
|
||||||
display: none;
|
|
||||||
margin-top: -1px;
|
margin-top: -1px;
|
||||||
border: 1px solid ${COLORS.SECONDARY};
|
border: 1px solid ${COLORS.SECONDARY};
|
||||||
border-radius: 0px 0px 3px 3px;
|
border-radius: 0 0 3px 3px;
|
||||||
max-height: 350px;
|
max-height: 350px;
|
||||||
overflow-y: scroll;
|
overflow-y: scroll;
|
||||||
}
|
}
|
||||||
`}</style>
|
`}</style>
|
||||||
</div>
|
</ul>
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const ListItem = ({ children, isHighlighted, isSelected, ...rest }) => {
|
||||||
|
return (
|
||||||
|
<li {...rest} className="dropdown-list-item">
|
||||||
|
<span className="dropdown-list-item-text">{children}</span>
|
||||||
|
{isSelected ? <CheckMark /> : null}
|
||||||
|
<style jsx>{`
|
||||||
|
.dropdown-list-item {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
background: ${isHighlighted ? COLORS.HOVER : COLORS.BLACK};
|
||||||
|
padding: 8px 16px;
|
||||||
|
border-bottom: 1px solid ${COLORS.SECONDARY};
|
||||||
|
}
|
||||||
|
.dropdown-list-item:hover {
|
||||||
|
background: ${COLORS.HOVER};
|
||||||
|
}
|
||||||
|
.dropdown-list-item-text {
|
||||||
|
flex-grow: 1;
|
||||||
}
|
}
|
||||||
|
`}</style>
|
||||||
|
</li>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function minWidth(list) {
|
||||||
|
return list.reduce((max, { name }) => {
|
||||||
|
const wordSize = name.length * 12
|
||||||
|
return wordSize > max ? wordSize : max
|
||||||
|
}, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default enhanceWithClickOutside(Dropdown)
|
export default Dropdown
|
||||||
|
Loading…
Reference in New Issue