|
| 1 | +import cx from "classnames"; |
| 2 | +import React from "react"; |
| 3 | +import { |
| 4 | + GroupBase, |
| 5 | + MenuProps, |
| 6 | + OptionsOrGroups, |
| 7 | + components, |
| 8 | +} from "react-select"; |
| 9 | +import Btn from "../../Btn"; |
| 10 | +import { CustomGroupBase, OptionTypeBase } from "../types"; |
| 11 | +import css from "./index.module.css"; |
| 12 | + |
| 13 | +export type GroupIndexProps = { |
| 14 | + selectedGroupIndex: number; |
| 15 | + setSelectedGroupIndex: (i: number) => void; |
| 16 | +}; |
| 17 | + |
| 18 | +function Tabs<T, OptionType extends OptionTypeBase<T>>( |
| 19 | + props: { |
| 20 | + options: OptionsOrGroups<OptionType, GroupBase<OptionType>>; |
| 21 | + } & GroupIndexProps, |
| 22 | +) { |
| 23 | + return ( |
| 24 | + <div className={css.menuWithTabs}> |
| 25 | + {props.options.map((option, index) => ( |
| 26 | + <Btn |
| 27 | + key={option.label} |
| 28 | + onClick={() => props.setSelectedGroupIndex(index)} |
| 29 | + className={cx(css.tab, { |
| 30 | + [css.activeTab]: index === props.selectedGroupIndex, |
| 31 | + })} |
| 32 | + > |
| 33 | + {option.label} |
| 34 | + </Btn> |
| 35 | + ))} |
| 36 | + </div> |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +function GroupNoOptions<T, OptionType extends OptionTypeBase<T>>( |
| 41 | + props: { |
| 42 | + options: OptionsOrGroups<OptionType, CustomGroupBase<OptionType>>; |
| 43 | + } & { selectedGroupIndex: number }, |
| 44 | +) { |
| 45 | + if (props.options.length > 0) { |
| 46 | + const activeGroup = props.options[props.selectedGroupIndex]; |
| 47 | + if ("options" in activeGroup) { |
| 48 | + if (activeGroup.options.length > 0) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + return ( |
| 52 | + <div className={css.noOpts}> |
| 53 | + {activeGroup.noOptionsMsg ?? "No options"} |
| 54 | + </div> |
| 55 | + ); |
| 56 | + } |
| 57 | + } |
| 58 | + return <div className={css.noOpts}>No options</div>; |
| 59 | +} |
| 60 | + |
| 61 | +function Footer<T, OptionType extends OptionTypeBase<T>>( |
| 62 | + props: { |
| 63 | + options: OptionsOrGroups<OptionType, CustomGroupBase<OptionType>>; |
| 64 | + } & { selectedGroupIndex: number }, |
| 65 | +) { |
| 66 | + if (props.options.length < props.selectedGroupIndex + 1) return null; |
| 67 | + const activeGroup = props.options[props.selectedGroupIndex]; |
| 68 | + if (!("footer" in activeGroup)) return null; |
| 69 | + return <div className={css.footer}>{activeGroup.footer}</div>; |
| 70 | +} |
| 71 | + |
| 72 | +export default function Menu< |
| 73 | + T, |
| 74 | + OptionType extends OptionTypeBase<T>, |
| 75 | + IsMulti extends boolean, |
| 76 | +>({ |
| 77 | + children, |
| 78 | + ...props |
| 79 | +}: MenuProps<OptionType, IsMulti, CustomGroupBase<OptionType>> & |
| 80 | + GroupIndexProps) { |
| 81 | + return ( |
| 82 | + <components.Menu {...props}> |
| 83 | + <Tabs {...props} options={props.selectProps.options} /> |
| 84 | + {children} |
| 85 | + <GroupNoOptions {...props} options={props.selectProps.options} /> |
| 86 | + <Footer {...props} options={props.selectProps.options} /> |
| 87 | + </components.Menu> |
| 88 | + ); |
| 89 | +} |
0 commit comments