File size: 3,875 Bytes
1e92f2d |
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 |
import ReactSelect, {
ControlProps,
MenuProps,
PlaceholderProps,
ValueContainerProps,
MultiValue,
OptionProps,
} from 'react-select'
import {
controlDiv,
menu,
menuBackground,
option,
placeholderSpan,
} from './Select.css'
export interface SelectProps {
options?: { value: string; label: string }[]
placeholder: string
onChange?: (newValue: MultiValue<{ value: string }>) => void
value: MultiValue<{ value: string }>
}
export const Select = ({
placeholder,
options = [],
onChange,
value,
}: SelectProps) => {
const handleChange = (newValue: MultiValue<{ value: string }>) => {
if (onChange) {
onChange(newValue)
}
}
return (
<ReactSelect
placeholder={placeholder}
options={options}
isClearable={false}
isMulti
isSearchable={false}
hideSelectedOptions={false}
// menuIsOpen
// @ts-ignore
onChange={handleChange}
components={{
// @ts-ignore
Control: SelectControl,
DropdownIndicator: null,
IndicatorSeparator: null,
// @ts-ignore
Menu: SelectMenu,
Placeholder: SelectPlaceholder,
// @ts-ignore
Option: SelectOption,
ValueContainer: props => (
// @ts-ignore
<SelectValueContainer {...props} placeholder={placeholder} />
),
}}
styles={{
container: () => ({
display: 'inline-block',
margin: '0 6px',
'@media(min-width: 768px)': {
position: 'relative',
},
}),
}}
value={value}
/>
)
}
const SelectControl = (props: ControlProps) => {
const { children, isFocused, innerRef, innerProps } = props
return (
<div
className={controlDiv({
isFocused,
})}
// @ts-ignore
ref={innerRef}
{...innerProps}
>
{children}
</div>
)
}
const SelectPlaceholder = ({
children,
}: Pick<PlaceholderProps, 'children'>) => (
<span className={placeholderSpan}>{children}</span>
)
const SelectValueContainer = ({
children,
placeholder,
}: ValueContainerProps & { placeholder: string }) => {
const [placeholderChild, menu] = children as React.ReactElement[]
if (placeholderChild.key !== 'placeholder') {
return (
<div>
<SelectPlaceholder>{placeholder}</SelectPlaceholder>
{menu}
</div>
)
}
return <div>{children}</div>
}
const SelectMenu = (props: MenuProps) => {
const { children, innerRef, innerProps } = props
return (
<>
<div className={menuBackground} />
{/* @ts-ignore */}
<div className={menu} ref={innerRef} {...innerProps}>
{children}
</div>
</>
)
}
const SelectOption = (props: OptionProps) => {
const { children, isFocused, isSelected, innerRef, innerProps } = props
return (
<div
className={option}
// @ts-ignore
ref={innerRef}
// @ts-expect-error - innerProps is not typed
isFocused={isFocused}
style={{ backgroundColor: isFocused ? '#ff6d6d99' : 'transparent' }}
{...innerProps}
>
{children}
{isSelected ? (
<svg
width="20"
height="20"
viewBox="0 0 15 15"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M11.4669 3.72684C11.7558 3.91574 11.8369 4.30308 11.648 4.59198L7.39799 11.092C7.29783 11.2452 7.13556 11.3467 6.95402 11.3699C6.77247 11.3931 6.58989 11.3355 6.45446 11.2124L3.70446 8.71241C3.44905 8.48022 3.43023 8.08494 3.66242 7.82953C3.89461 7.57412 4.28989 7.55529 4.5453 7.78749L6.75292 9.79441L10.6018 3.90792C10.7907 3.61902 11.178 3.53795 11.4669 3.72684Z"
fill="currentColor"
fillRule="evenodd"
clipRule="evenodd"
></path>
</svg>
) : null}
</div>
)
}
|