fix dropdown sort

main
Mike Fix 6 years ago
parent 7a3930cae6
commit 7c7af92bce

@ -57,7 +57,60 @@ class Dropdown extends React.PureComponent {
userInputtedValue = '' userInputtedValue = ''
renderDropdown = ({ render() {
const {
innerRef,
color,
selected,
onChange,
itemWrapper,
icon,
disableInput,
title
} = this.props
const { itemsToShow, inputValue } = this.state
const minWidth = calcMinWidth(itemsToShow)
const labelId = title ? `${title.toLowerCase()}-dropdown` : undefined
return (
<Downshift
ref={innerRef}
inputValue={inputValue}
selectedItem={selected}
defaultHighlightedIndex={itemsToShow.findIndex(it => it === selected)}
itemToString={item => item.name}
onChange={onChange}
onUserAction={this.onUserAction}
labelId={labelId}
>
{renderDropdown({
color,
list: itemsToShow,
selected,
minWidth,
itemWrapper,
icon,
disableInput,
title,
labelId
})}
</Downshift>
)
}
}
const renderDropdown = ({
color,
list,
minWidth,
itemWrapper,
icon,
disableInput,
title,
labelId
}) => ({
isOpen, isOpen,
highlightedIndex, highlightedIndex,
selectedItem, selectedItem,
@ -66,12 +119,6 @@ class Dropdown extends React.PureComponent {
getInputProps, getInputProps,
getItemProps getItemProps
}) => { }) => {
const { list, color, itemWrapper, icon, disableInput, title } = this.props
const { itemsToShow } = this.state
const minWidth = calcMinWidth(itemsToShow)
const labelId = title ? `${title.toLowerCase()}-dropdown` : undefined
return ( return (
<DropdownContainer {...getRootProps({ refKey: 'innerRef' })} minWidth={minWidth}> <DropdownContainer {...getRootProps({ refKey: 'innerRef' })} minWidth={minWidth}>
{title && <VisuallyHidden id={labelId}>{title}</VisuallyHidden>} {title && <VisuallyHidden id={labelId}>{title}</VisuallyHidden>}
@ -87,7 +134,7 @@ class Dropdown extends React.PureComponent {
{selectedItem.name} {selectedItem.name}
</SelectedItem> </SelectedItem>
{isOpen ? ( {isOpen ? (
<List color={color}> <ListItems color={color}>
{list.map((item, index) => ( {list.map((item, index) => (
<ListItem <ListItem
key={index} key={index}
@ -103,35 +150,12 @@ class Dropdown extends React.PureComponent {
{item.name} {item.name}
</ListItem> </ListItem>
))} ))}
</List> </ListItems>
) : null} ) : null}
</DropdownContainer> </DropdownContainer>
) )
} }
render() {
const { innerRef, selected, onChange, title } = this.props
const { itemsToShow, inputValue } = this.state
const labelId = title ? `${title.toLowerCase()}-dropdown` : undefined
return (
<Downshift
ref={innerRef}
inputValue={inputValue}
selectedItem={selected}
defaultHighlightedIndex={itemsToShow.findIndex(it => it === selected)}
itemToString={item => item.name}
onChange={onChange}
onUserAction={this.onUserAction}
labelId={labelId}
>
{this.renderDropdown}
</Downshift>
)
}
}
const DropdownContainer = ({ children, innerRef, minWidth, ...rest }) => { const DropdownContainer = ({ children, innerRef, minWidth, ...rest }) => {
return ( return (
<div {...rest} ref={innerRef} className="dropdown-container"> <div {...rest} ref={innerRef} className="dropdown-container">
@ -174,10 +198,10 @@ const DropdownIcon = ({ children, isOpen }) => {
</style> </style>
</div> </div>
) )
} } else {
return null return null
} }
}
const SelectedItem = ({ const SelectedItem = ({
getToggleButtonProps, getToggleButtonProps,
@ -246,7 +270,7 @@ const SelectedItem = ({
) )
} }
const List = ({ children, color }) => { const ListItems = ({ children, color }) => {
return ( return (
<ul role="listbox" className="dropdown-list"> <ul role="listbox" className="dropdown-list">
{children} {children}

Loading…
Cancel
Save