-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSelect.tsx
More file actions
280 lines (260 loc) · 7.73 KB
/
Select.tsx
File metadata and controls
280 lines (260 loc) · 7.73 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import React, { forwardRef, useCallback, useMemo, useRef } from "react";
import {
ListRenderItemInfo,
PressableProps,
PressableStateCallbackType,
} from "react-native";
import { useControllableState } from "@chakra-ui/hooks";
import { BottomSheetFlatList, BottomSheetModal } from "@gorhom/bottom-sheet";
import { isUndefined } from "lodash";
import { Text, Touchable } from "../../primitives";
import {
getTextFontFamily,
getTextLineHeight,
useTailwind,
useTheme,
} from "../../theme";
import {
createComponent,
createContext,
cx,
RenderPropType,
styleAdapter,
useOnHover,
} from "../../utils";
import { SelectOption } from "./SelectOption";
import { SelectPrefix } from "./SelectPrefix";
import { SelectSuffix } from "./SelectSuffix";
export type SelectSizes = "sm" | "md" | "lg" | "xl";
export type SelectVariants = "outline" | "subtle" | "underline" | "ghost";
export type ItemData = { value: string; disabled?: boolean; label: string };
function keyExtractor(item: ItemData) {
return `select-item-${item.value}`;
}
export interface SelectProps extends PressableProps {
/**
* The Select Options to render inside the Bottomsheet Flatlist
*/
options: Array<ItemData>;
/**
* Default value of Select when it is uncontrolled
*/
defaultState: string;
/**
* Value of Select when it is controlled
*/
state: string;
/**
* Callback called with the new value when on selecting options from Flatlist
*/
onStateChange: (val: string) => void;
/**
* Placeholder value
* @default "Select Option"
*/
placeholder: string;
/**
* How large should the select be?
* @default md
*/
size: SelectSizes;
/**
* How the select should look?
* @default outline
*/
variant: SelectVariants;
/**
* Prefix for the Select Component
*/
prefix: RenderPropType;
/**
* Suffix for the Select Component
*/
suffix: RenderPropType;
/**
* Set to True, if the value of the Select component is invalid.
*/
invalid: boolean;
/**
* Set to true, if the select is disabled.
*/
disabled: boolean;
/**
* Bottomsheet Snap points
*/
snapPoints: string[];
}
const [SelectGroupProvider, useSelectGroupContext] = createContext({
strict: false,
name: "SelectGroupProvider",
});
export { useSelectGroupContext };
const RNSelect: React.FC<Partial<SelectProps>> = forwardRef<
typeof Touchable,
Partial<SelectProps>
>((props, _ref) => {
const { ts } = useTailwind();
const selectStyle = useTheme("select");
const {
size = "md",
variant = "outline",
prefix,
suffix,
invalid = false,
disabled = false,
defaultState,
state,
onStateChange,
placeholder = "Select option",
options,
snapPoints: _snapPoints,
style,
...otherProps
} = props;
const [prefixWidth, setPrefixWidth] = React.useState(0);
const [suffixWidth, setSuffixWidth] = React.useState(0);
const [selectState, setSelectState] = useControllableState({
defaultValue: defaultState,
value: state,
onChange: onStateChange,
});
const selectRef = useRef();
const { onHoverIn, onHoverOut, hovered } = useOnHover();
const isHovered = useMemo(
() => (hovered.value ? true : false),
[hovered.value],
);
// ref
const bottomSheetModalRef = useRef<BottomSheetModal>(null);
// variables
const snapPoints = useMemo(
() => (_snapPoints ? _snapPoints : ["50%"]),
[_snapPoints],
);
// callbacks
const handlePresentModalPress = useCallback(() => {
bottomSheetModalRef.current?.present();
}, []);
const renderSelectItem = ({ item }: ListRenderItemInfo<ItemData>) => {
const handleSelectItemPress = () => {
setSelectState(item.value);
bottomSheetModalRef.current?.dismiss();
};
const isSelected = selectState === item.value;
return (
<SelectOption
key={`select-item-${item.value}`}
value={item.value}
label={item.label}
size={size}
handlePress={handleSelectItemPress}
isSelected={isSelected}
/>
);
};
return (
<SelectGroupProvider value={{ selectState, setSelectState }}>
<Touchable
// Web Callbacks
onHoverIn={onHoverIn}
onHoverOut={onHoverOut}
// Web Callbacks
onPress={handlePresentModalPress}
style={(touchState: PressableStateCallbackType) => [
ts([
cx(
selectStyle.base?.common,
selectStyle.base?.size[size]?.common,
!prefix ? selectStyle.base?.size[size]?.withoutAddon : "",
selectStyle.base?.variant[variant]?.default,
invalid ? selectStyle.base?.variant[variant]?.invalid : "",
disabled ? selectStyle.base?.variant[variant]?.disabled : "",
touchState.pressed || hovered.value
? selectStyle.base?.variant[variant]?.pressedOrHovered
: "",
),
]),
styleAdapter(style, touchState),
]}
{...otherProps}
disabled={disabled}
ref={selectRef}
>
{(touchState: PressableStateCallbackType) => {
return (
<>
<SelectPrefix
onLayout={event =>
setPrefixWidth(event.nativeEvent.layout.width)
}
size={size}
variant={variant}
disabled={disabled}
invalid={invalid}
prefix={prefix}
isPressedOrHovered={touchState.pressed || isHovered}
isDefaultState={isUndefined(selectState)}
/>
<Text
style={[
ts([
cx(
selectStyle.base?.size[size]?.text,
touchState.pressed || hovered.value
? selectStyle.base?.variant[variant]?.text
?.pressedOrHovered
: disabled
? selectStyle.base?.variant[variant]?.text?.disabled
: isUndefined(selectState)
? selectStyle.base?.variant[variant]?.text?.default
: selectStyle.base?.variant[variant]?.text?.filled,
prefix ? `pl-[${prefixWidth}px]` : "",
`pr-[${suffixWidth}px]`,
),
]),
getTextFontFamily(selectStyle.base?.size[size]?.text),
getTextLineHeight(selectStyle.base?.size[size]?.text),
]}
>
{isUndefined(selectState)
? placeholder
: options?.filter(item => item.value === selectState)[0]
?.label}
</Text>
<SelectSuffix
onLayout={event =>
setSuffixWidth(event.nativeEvent.layout.width)
}
size={size}
variant={variant}
disabled={disabled}
invalid={invalid}
suffix={suffix}
isPressedOrHovered={touchState.pressed || isHovered}
isDefaultState={isUndefined(selectState)}
/>
</>
);
}}
</Touchable>
<BottomSheetModal
ref={bottomSheetModalRef}
index={0}
snapPoints={snapPoints}
style={ts("rounded-t-lg shadow-lg")}
>
<BottomSheetFlatList
data={options}
keyExtractor={keyExtractor}
renderItem={renderSelectItem}
contentContainerStyle={ts("px-4")}
extraData={selectState}
/>
</BottomSheetModal>
</SelectGroupProvider>
);
});
RNSelect.displayName = "RNSelect";
export const Select = createComponent<Partial<SelectProps>>(RNSelect, {
shouldMemo: true,
});