Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | 8x 8x 2x 2x 2x 2x 2x 2x 2x 6x 6x 6x 6x 6x 4x 2x | /*
Copyright (c) 2018-2019 Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
import * as React from 'react';
import {
StyledDropdownContainer,
StyledDropdown,
StyledDropdownListItem,
StyledOptionContent,
} from './styled-components.js';
import {StatefulMenu} from '../menu/index.js';
import type {DropdownPropsT} from './types.js';
import {getOverrides, mergeOverrides} from '../helpers/overrides.js';
export default class SelectDropdown extends React.Component<DropdownPropsT> {
getSharedProps() {
const {
error,
isLoading,
multi,
required,
size,
searchable,
type,
width,
} = this.props;
return {
$error: error,
$isLoading: isLoading,
$multi: multi,
$required: required,
$searchable: searchable,
$size: size,
$type: type,
$width: width,
};
}
// eslint-disable-next-line flowtype/no-weak-types
getItemLabel = (option: {[string]: any}) => {
const {getOptionLabel, overrides = {}, value, valueKey} = this.props;
const [OptionContent, optionContentProps] = getOverrides(
overrides.OptionContent,
StyledOptionContent,
);
let $selected;
Eif (Array.isArray(value)) {
$selected = !!value.find(
selected => selected && selected[valueKey] === option[valueKey],
);
} else {
$selected = value[valueKey] === option[valueKey];
}
const optionSharedProps = {
$selected,
$disabled: option.disabled,
$isHighlighted: option.isHighlighted,
};
return (
<OptionContent
aria-readonly={option.disabled}
aria-selected={$selected}
key={option[valueKey]}
{...this.getSharedProps()}
{...optionSharedProps}
{...optionContentProps}
>
{getOptionLabel({option, optionState: optionSharedProps})}
</OptionContent>
);
};
onMouseDown = (e: Event) => {
e.preventDefault();
};
render() {
// TODO(#185) Add no-results and loading states to menu
const {
maxDropdownHeight,
multi,
noResultsMsg,
onItemSelect,
options = [],
overrides = {},
value,
size,
} = this.props;
const [DropdownContainer, dropdownContainerProps] = getOverrides(
overrides.DropdownContainer,
StyledDropdownContainer,
);
const [ListItem, listItemProps] = getOverrides(
overrides.DropdownListItem,
StyledDropdownListItem,
);
const [OverriddenStatefulMenu, statefulMenuProps] = getOverrides(
overrides.StatefulMenu,
StatefulMenu,
);
return (
<DropdownContainer
ref={this.props.innerRef}
role="listbox"
{...this.getSharedProps()}
{...dropdownContainerProps}
>
<OverriddenStatefulMenu
noResultsMsg={noResultsMsg}
onItemSelect={onItemSelect}
items={options}
size={size}
initialState={{
isFocused: true,
highlightedIndex:
Array.isArray(value) && value.length === 1
? options.findIndex(opt => opt.id === value[0].id)
: 0,
}}
overrides={mergeOverrides(
{
List: {
component: StyledDropdown,
style: p => ({
maxHeight: p.$maxHeight || null,
}),
props: {
$maxHeight: maxDropdownHeight,
'aria-multiselectable': multi,
},
},
Option: {
props: {
getItemLabel: this.getItemLabel,
// figure out why the onClick handler is not
// triggered without this temporary fix
onMouseDown: this.onMouseDown,
overrides: {
ListItem: {
component: ListItem,
props: {...listItemProps, role: 'option'},
// slightly hacky way to handle the list item style overrides
// since the menu component doesn't provide a top level overrides for it
// $FlowFixMe
style: listItemProps.$style,
},
},
},
},
},
{
List: overrides.Dropdown || {},
Option: overrides.DropdownOption || {},
},
)}
{...statefulMenuProps}
/>
</DropdownContainer>
);
}
}
|