-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathindex.tsx
More file actions
85 lines (79 loc) · 2.49 KB
/
index.tsx
File metadata and controls
85 lines (79 loc) · 2.49 KB
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
import { ForwardedRef } from 'react';
import {
AsyncCreatableProps,
AsyncProps,
AsyncCreatableSelect as ChakraAsyncCreatableSelect,
AsyncSelect as ChakraAsyncReactSelect,
CreatableSelect as ChakraCreatableReactSelect,
Select as ChakraReactSelect,
CreatableProps,
GroupBase,
Props,
SelectInstance,
} from 'chakra-react-select';
import { match } from 'ts-pattern';
import { fixedForwardRef } from '@/lib/utils';
export type SelectProps<
Option = unknown,
IsMulti extends boolean = false,
Group extends GroupBase<Option> = GroupBase<Option>,
> =
| ({ type?: 'select' } & Props<Option, IsMulti, Group>)
| ({ type: 'creatable' } & CreatableProps<Option, IsMulti, Group>)
| ({ type: 'async' } & AsyncProps<Option, IsMulti, Group>)
| ({ type: 'async-creatable' } & AsyncCreatableProps<Option, IsMulti, Group>);
const SelectComponent = <
Option = unknown,
IsMulti extends boolean = false,
Group extends GroupBase<Option> = GroupBase<Option>,
>(
{ type = 'select', ...props }: SelectProps<Option, IsMulti, Group>,
ref: ForwardedRef<SelectInstance<Option, IsMulti, Group>>
) => {
const Element = match(type)
.with('async-creatable', () => ChakraAsyncCreatableSelect)
.with('async', () => ChakraAsyncReactSelect)
.with('creatable', () => ChakraCreatableReactSelect)
.with('select', () => ChakraReactSelect)
.exhaustive();
return (
<Element
ref={ref}
colorScheme="brand"
selectedOptionColorScheme="brand"
useBasicStyles
styles={{ menuPortal: (provided) => ({ ...provided, zIndex: 9999 }) }}
menuPortalTarget={document.body}
chakraStyles={{
dropdownIndicator: (provided, state) => ({
...provided,
paddingLeft: 0,
paddingRight: 0,
margin: 0,
...props.chakraStyles?.dropdownIndicator?.(provided, state),
}),
control: (provided, state) => ({
...provided,
paddingLeft: 2,
paddingRight: 2,
...props.chakraStyles?.control?.(provided, state),
}),
valueContainer: (provided, state) => ({
...provided,
padding: 0,
pl: 1,
...props.chakraStyles?.valueContainer?.(provided, state),
}),
multiValue: (provided, state) => ({
...provided,
_first: {
ml: -1,
},
...props.chakraStyles?.multiValue?.(provided, state),
}),
}}
{...props}
/>
);
};
export const Select = fixedForwardRef(SelectComponent);