-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathNativePicker.ios.tsx
More file actions
180 lines (166 loc) · 4.96 KB
/
NativePicker.ios.tsx
File metadata and controls
180 lines (166 loc) · 4.96 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
import * as React from "react";
import { StyleSheet, Keyboard } from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";
import { Picker as NativePickerComponent } from "@react-native-picker/picker";
import Portal from "../Portal/Portal";
import { Button } from "../Button";
import { useDeepCompareMemo } from "../../utilities";
import {
CommonPickerProps,
SinglePickerProps,
normalizeToPickerOptions,
PickerOption,
} from "./PickerCommon";
import PickerInputContainer from "./PickerInputContainer";
import { ReadTheme, withTheme } from "@draftbit/theme";
import { IconSlot } from "../../interfaces/Icon";
/**
* Duplicated version of NativePicker.tsx for maintaining state inside the Portal container to avoid this issue
* https://github.com/react-native-picker/picker/issues/615
*/
interface PortalPickerContentProps extends IconSlot {
value: string | number | undefined;
options: PickerOption[];
placeholder?: string;
onValueChange?: (value: string | number) => void;
onClose: () => void;
theme: ReadTheme;
autoDismissKeyboard?: boolean;
}
const PortalPickerContent: React.FC<PortalPickerContentProps> = ({
value,
options,
placeholder,
onValueChange,
onClose,
Icon,
theme,
autoDismissKeyboard = true,
}) => {
const pickerRef = React.useRef<NativePickerComponent<string | number>>(null);
// Manage value state inside the Portal to avoid stale state issues across the Portal boundary
const [internalValue, setInternalValue] = React.useState<
string | number | undefined
>(value);
React.useEffect(() => {
setInternalValue(value);
}, [value]);
React.useEffect(() => {
if (autoDismissKeyboard) {
Keyboard.dismiss();
}
}, [autoDismissKeyboard]);
return (
<SafeAreaView
style={[
styles.iosPickerContent,
{ backgroundColor: theme.colors.background.base },
]}
>
<Button
Icon={Icon}
onPress={onClose}
style={[styles.iosButton, { color: theme.colors.branding.primary }]}
title="Close"
/>
<NativePickerComponent
ref={pickerRef}
testID="native-picker-component"
selectedValue={internalValue}
onValueChange={(newValue) => {
setInternalValue(newValue);
if (newValue !== placeholder) {
onValueChange?.(newValue);
} else if (newValue === placeholder) {
onValueChange?.("");
}
}}
style={[
styles.iosNativePicker,
{ backgroundColor: theme.colors.background.base },
]}
onBlur={onClose}
>
{options.map((option) => (
<NativePickerComponent.Item
testID="native-picker-item"
label={option.label.toString()}
value={option.value}
key={option.value}
color={theme.colors.text.strong}
/>
))}
</NativePickerComponent>
</SafeAreaView>
);
};
const NativePicker: React.FC<CommonPickerProps & SinglePickerProps> = ({
options: optionsProp = [],
onValueChange,
Icon,
placeholder,
value,
autoDismissKeyboard = true,
theme,
disabled,
...rest
}) => {
const [pickerVisible, setPickerVisible] = React.useState(false);
const options = useDeepCompareMemo(() => {
const normalizedOptions = normalizeToPickerOptions(optionsProp);
// Underlying Picker component defaults selection to first element when value is not provided (or undefined)
// Placholder must be the 1st option in order to allow selection of the 'actual' 1st option
if (placeholder) {
return [{ label: placeholder, value: placeholder }, ...normalizedOptions];
} else {
return normalizedOptions;
}
}, [placeholder, optionsProp]);
// When no placeholder is provided then first item should be marked selected to reflect underlying Picker internal state
if (!placeholder && options.length && !value && value !== options[0].value) {
onValueChange?.(options[0].value);
}
return (
<PickerInputContainer
testID="native-picker"
Icon={Icon}
placeholder={placeholder}
selectedValue={value}
options={options}
onPress={() => setPickerVisible(!pickerVisible)}
disabled={disabled}
{...rest}
>
{pickerVisible && !disabled && (
<Portal>
<PortalPickerContent
value={value}
options={options}
placeholder={placeholder}
onValueChange={onValueChange}
onClose={() => setPickerVisible(false)}
Icon={Icon}
theme={theme}
autoDismissKeyboard={autoDismissKeyboard}
/>
</Portal>
)}
</PickerInputContainer>
);
};
const styles = StyleSheet.create({
iosNativePicker: {
backgroundColor: "white",
},
iosPickerContent: {
width: "100%",
position: "absolute",
bottom: 0,
backgroundColor: "white",
},
iosButton: {
backgroundColor: "transparent",
borderWidth: 0,
},
});
export default withTheme(NativePicker);