sendou.ink/components/common/MySelect.tsx
Kalle 1589b84c4b
New layout (#427) closes #405
* side layout initial

* add elements to side nav

* side buttons links

* remove clog

* calendar page initial

* position sticky working

* x trends page initial

* new table

* same mode selector

* mobile friendly table

* no underline for nav links

* xsearch

* x trends page outlined

* sr initial

* relocate calendar components

* calendar fix flex

* topnav fancier look

* layout looking good edition

* relocate xtrends

* xtrends remove linecharts

* x trends new

* calender page new

* delete headbanner, new login

* remove calendar stuff from api

* rename stuff in utils

* fix user item margin

* new home page initial

* remove page concept

* no pointer xtrends

* remove xrank from app

* xtrends service

* move fa from app

* move plus

* maps tweaks

* new table for plus history

* navigational sidebar flex tweaks

* builds page

* analyzer

* user page

* free agents

* plans

* remove mx

* tweaks

* change layout to grid

* home page finalized

* mobile nav

* restrict main content width

* tweaks style

* language switcher

* container in css

* sticky nav

* use duplicate icons for now

* change mapsketch width to old

* chara tour vid

* borzoic icons
2021-04-21 17:26:50 +03:00

176 lines
4.1 KiB
TypeScript

import { ChevronDownIcon } from "@chakra-ui/icons";
import { useMyTheme } from "hooks/common";
import { useState } from "react";
import ReactSelect, {
components,
GroupedOptionsType,
OptionsType,
OptionTypeBase,
ValueType,
} from "react-select";
import { SelectComponents } from "react-select/src/components";
interface SelectProps {
options?:
| OptionsType<{
label: string;
value: any;
data?: any;
}>
| GroupedOptionsType<{
label: string;
value: string;
data?: any;
}>;
name?: string;
width?: string;
value?: ValueType<OptionTypeBase, boolean>;
setValue: (value: any) => void;
autoFocus?: boolean;
components?: Partial<SelectComponents<OptionTypeBase, boolean>>;
isClearable?: boolean;
isMulti?: boolean;
isLoading?: boolean;
isDisabled?: boolean;
isSearchable?: boolean;
menuIsOpen?: boolean;
hideMenuBeforeTyping?: boolean;
customFilterOption?: (option: any, rawInput: string) => boolean;
}
const DropdownIndicator = (props: any) => {
const { textColor } = useMyTheme();
return (
<components.DropdownIndicator {...props}>
<ChevronDownIcon fontSize="1.3rem" color={textColor} />
</components.DropdownIndicator>
);
};
const MySelect: React.FC<SelectProps> = ({
options,
components,
value,
setValue,
name,
isClearable = false,
autoFocus = false,
isMulti = false,
isLoading = false,
isDisabled = false,
isSearchable = false,
menuIsOpen = false,
hideMenuBeforeTyping,
customFilterOption,
}) => {
const {
borderColor,
themeColorHex,
bgColor,
themeColorOpaque,
textColor,
} = useMyTheme();
const [inputValue, setInputValue] = useState("");
const handleChange = (selectedOption: any) => {
if (!selectedOption) {
setValue(isMulti ? [] : null);
return;
}
if (Array.isArray(selectedOption)) {
setValue(selectedOption.map((obj) => obj.value));
} else {
setValue(selectedOption?.value);
}
};
const menuIsOpenCheck = () => {
if (menuIsOpen) return true;
if (hideMenuBeforeTyping) {
return !!(inputValue.length >= 3);
}
return undefined;
};
return (
<ReactSelect
className="basic-single"
classNamePrefix="select"
name={name}
value={value}
inputValue={inputValue}
onInputChange={(newValue) => setInputValue(newValue)}
menuIsOpen={menuIsOpenCheck()}
onChange={handleChange}
placeholder={null}
isSearchable={!!isSearchable}
isMulti={!!isMulti}
isLoading={isLoading}
isDisabled={isDisabled}
isClearable={isClearable}
options={options}
filterOption={customFilterOption}
components={
hideMenuBeforeTyping
? {
IndicatorSeparator: () => null,
DropdownIndicator: () => null,
...components,
}
: {
IndicatorSeparator: () => null,
DropdownIndicator,
...components,
}
}
theme={(theme) => ({
...theme,
borderRadius: 5,
colors: {
...theme.colors,
primary25: `${themeColorHex}`,
primary: themeColorHex,
neutral0: bgColor,
neutral5: bgColor,
},
})}
autoFocus={autoFocus}
styles={{
singleValue: (base) => ({
...base,
padding: 5,
borderRadius: 5,
color: textColor,
display: "flex",
}),
input: (base) => ({
...base,
color: textColor,
}),
multiValue: (base) => ({
...base,
background: themeColorHex,
color: "black",
}),
option: (styles, { isFocused }) => {
return {
...styles,
backgroundColor: isFocused ? themeColorOpaque : undefined,
color: textColor,
};
},
menu: (styles) => ({ ...styles, zIndex: 999 }),
control: (base) => ({
...base,
borderColor,
minHeight: "2.5rem",
background: "hsla(0, 0%, 0%, 0)",
}),
}}
/>
);
};
export default MySelect;