zkit-ui 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +267 -0
- package/dist/ComponentLibProvider.d.ts +15 -0
- package/dist/ComponentLibProvider.d.ts.map +1 -0
- package/dist/ComponentLibProvider.js +18 -0
- package/dist/assets/animations/spinner.json +1 -0
- package/dist/assets/icons/check.svg +4 -0
- package/dist/assets/icons/toast/error.webp +0 -0
- package/dist/assets/icons/toast/success.webp +0 -0
- package/dist/assets/icons/toast/warning.webp +0 -0
- package/dist/config.d.ts +16 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +42 -0
- package/dist/i18n/I18nProvider.d.ts +11 -0
- package/dist/i18n/I18nProvider.d.ts.map +1 -0
- package/dist/i18n/I18nProvider.js +84 -0
- package/dist/i18n/locale.d.ts +7 -0
- package/dist/i18n/locale.d.ts.map +1 -0
- package/dist/i18n/locale.js +87 -0
- package/dist/i18n/locales/de.d.ts +51 -0
- package/dist/i18n/locales/de.d.ts.map +1 -0
- package/dist/i18n/locales/de.js +62 -0
- package/dist/i18n/locales/en-US.d.ts +51 -0
- package/dist/i18n/locales/en-US.d.ts.map +1 -0
- package/dist/i18n/locales/en-US.js +62 -0
- package/dist/i18n/locales/index.d.ts +9 -0
- package/dist/i18n/locales/index.d.ts.map +1 -0
- package/dist/i18n/locales/index.js +23 -0
- package/dist/i18n/locales/ja.d.ts +51 -0
- package/dist/i18n/locales/ja.d.ts.map +1 -0
- package/dist/i18n/locales/ja.js +62 -0
- package/dist/i18n/locales/zh-CN.d.ts +51 -0
- package/dist/i18n/locales/zh-CN.d.ts.map +1 -0
- package/dist/i18n/locales/zh-CN.js +62 -0
- package/dist/i18n/locales/zh-TW.d.ts +51 -0
- package/dist/i18n/locales/zh-TW.d.ts.map +1 -0
- package/dist/i18n/locales/zh-TW.js +62 -0
- package/dist/i18n/types.d.ts +11 -0
- package/dist/i18n/types.d.ts.map +1 -0
- package/dist/i18n/types.js +2 -0
- package/dist/i18n/useI18n.d.ts +2 -0
- package/dist/i18n/useI18n.d.ts.map +1 -0
- package/dist/i18n/useI18n.js +41 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +90 -0
- package/dist/services/ActionDialogService/ActionDialog.d.ts +4 -0
- package/dist/services/ActionDialogService/ActionDialog.d.ts.map +1 -0
- package/dist/services/ActionDialogService/ActionDialog.js +531 -0
- package/dist/services/ActionDialogService/index.d.ts +4 -0
- package/dist/services/ActionDialogService/index.d.ts.map +1 -0
- package/dist/services/ActionDialogService/index.js +8 -0
- package/dist/services/ActionDialogService/service.d.ts +7 -0
- package/dist/services/ActionDialogService/service.d.ts.map +1 -0
- package/dist/services/ActionDialogService/service.js +309 -0
- package/dist/services/ActionDialogService/shared.d.ts +17 -0
- package/dist/services/ActionDialogService/shared.d.ts.map +1 -0
- package/dist/services/ActionDialogService/shared.js +167 -0
- package/dist/services/ActionDialogService/types.d.ts +160 -0
- package/dist/services/ActionDialogService/types.d.ts.map +1 -0
- package/dist/services/ActionDialogService/types.js +2 -0
- package/dist/services/CardToastService/index.d.ts +115 -0
- package/dist/services/CardToastService/index.d.ts.map +1 -0
- package/dist/services/CardToastService/index.js +899 -0
- package/dist/services/ImagePreviewService/index.d.ts +179 -0
- package/dist/services/ImagePreviewService/index.d.ts.map +1 -0
- package/dist/services/ImagePreviewService/index.js +1383 -0
- package/dist/services/LoadingService/index.d.ts +146 -0
- package/dist/services/LoadingService/index.d.ts.map +1 -0
- package/dist/services/LoadingService/index.js +827 -0
- package/dist/services/PermissionPurposeDialogService/index.d.ts +99 -0
- package/dist/services/PermissionPurposeDialogService/index.d.ts.map +1 -0
- package/dist/services/PermissionPurposeDialogService/index.js +360 -0
- package/dist/services/PickerService/index.d.ts +128 -0
- package/dist/services/PickerService/index.d.ts.map +1 -0
- package/dist/services/PickerService/index.js +250 -0
- package/dist/theme/ThemeProvider.d.ts +15 -0
- package/dist/theme/ThemeProvider.d.ts.map +1 -0
- package/dist/theme/ThemeProvider.js +61 -0
- package/dist/theme/defaultTheme.d.ts +3 -0
- package/dist/theme/defaultTheme.d.ts.map +1 -0
- package/dist/theme/defaultTheme.js +28 -0
- package/dist/theme/mergeTheme.d.ts +3 -0
- package/dist/theme/mergeTheme.d.ts.map +1 -0
- package/dist/theme/mergeTheme.js +19 -0
- package/dist/theme/types.d.ts +17 -0
- package/dist/theme/types.d.ts.map +1 -0
- package/dist/theme/types.js +2 -0
- package/dist/theme/useTheme.d.ts +2 -0
- package/dist/theme/useTheme.d.ts.map +1 -0
- package/dist/theme/useTheme.js +46 -0
- package/dist/ui/Accordion/index.d.ts +99 -0
- package/dist/ui/Accordion/index.d.ts.map +1 -0
- package/dist/ui/Accordion/index.js +777 -0
- package/dist/ui/AddressCascader/index.d.ts +58 -0
- package/dist/ui/AddressCascader/index.d.ts.map +1 -0
- package/dist/ui/AddressCascader/index.js +710 -0
- package/dist/ui/BetweenTime/index.d.ts +82 -0
- package/dist/ui/BetweenTime/index.d.ts.map +1 -0
- package/dist/ui/BetweenTime/index.js +702 -0
- package/dist/ui/Button/index.d.ts +93 -0
- package/dist/ui/Button/index.d.ts.map +1 -0
- package/dist/ui/Button/index.js +832 -0
- package/dist/ui/Checkbox/index.d.ts +103 -0
- package/dist/ui/Checkbox/index.d.ts.map +1 -0
- package/dist/ui/Checkbox/index.js +694 -0
- package/dist/ui/DatePicker/index.d.ts +5 -0
- package/dist/ui/DatePicker/index.d.ts.map +1 -0
- package/dist/ui/DatePicker/index.js +157 -0
- package/dist/ui/DatePicker/types.d.ts +99 -0
- package/dist/ui/DatePicker/types.d.ts.map +1 -0
- package/dist/ui/DatePicker/types.js +2 -0
- package/dist/ui/DatePicker/utils.d.ts +32 -0
- package/dist/ui/DatePicker/utils.d.ts.map +1 -0
- package/dist/ui/DatePicker/utils.js +316 -0
- package/dist/ui/LinkedScroll/index.d.ts +78 -0
- package/dist/ui/LinkedScroll/index.d.ts.map +1 -0
- package/dist/ui/LinkedScroll/index.js +470 -0
- package/dist/ui/LoadingSpinner/index.d.ts +8 -0
- package/dist/ui/LoadingSpinner/index.d.ts.map +1 -0
- package/dist/ui/LoadingSpinner/index.js +122 -0
- package/dist/ui/Picker/actionBar.d.ts +14 -0
- package/dist/ui/Picker/actionBar.d.ts.map +1 -0
- package/dist/ui/Picker/actionBar.js +28 -0
- package/dist/ui/Picker/index.d.ts +5 -0
- package/dist/ui/Picker/index.d.ts.map +1 -0
- package/dist/ui/Picker/index.js +412 -0
- package/dist/ui/Picker/types.d.ts +80 -0
- package/dist/ui/Picker/types.d.ts.map +1 -0
- package/dist/ui/Picker/types.js +2 -0
- package/dist/ui/Picker/utils.d.ts +27 -0
- package/dist/ui/Picker/utils.d.ts.map +1 -0
- package/dist/ui/Picker/utils.js +182 -0
- package/dist/ui/Radio/index.d.ts +95 -0
- package/dist/ui/Radio/index.d.ts.map +1 -0
- package/dist/ui/Radio/index.js +633 -0
- package/dist/ui/Sheet/index.d.ts +12 -0
- package/dist/ui/Sheet/index.d.ts.map +1 -0
- package/dist/ui/Sheet/index.js +832 -0
- package/dist/ui/Sheet/nativeBottom.d.ts +52 -0
- package/dist/ui/Sheet/nativeBottom.d.ts.map +1 -0
- package/dist/ui/Sheet/nativeBottom.js +442 -0
- package/dist/ui/Sheet/nativeBottom.web.d.ts +18 -0
- package/dist/ui/Sheet/nativeBottom.web.d.ts.map +1 -0
- package/dist/ui/Sheet/nativeBottom.web.js +46 -0
- package/dist/ui/Sheet/slots.d.ts +5 -0
- package/dist/ui/Sheet/slots.d.ts.map +1 -0
- package/dist/ui/Sheet/slots.js +40 -0
- package/dist/ui/Sheet/types.d.ts +131 -0
- package/dist/ui/Sheet/types.d.ts.map +1 -0
- package/dist/ui/Sheet/types.js +2 -0
- package/dist/ui/SliderCaptcha/index.d.ts +57 -0
- package/dist/ui/SliderCaptcha/index.d.ts.map +1 -0
- package/dist/ui/SliderCaptcha/index.js +586 -0
- package/dist/ui/Switch/index.d.ts +72 -0
- package/dist/ui/Switch/index.d.ts.map +1 -0
- package/dist/ui/Switch/index.js +671 -0
- package/dist/ui/Text/index.d.ts +41 -0
- package/dist/ui/Text/index.d.ts.map +1 -0
- package/dist/ui/Text/index.js +325 -0
- package/dist/ui/TextInput/TextInputPrimitive.d.ts +8 -0
- package/dist/ui/TextInput/TextInputPrimitive.d.ts.map +1 -0
- package/dist/ui/TextInput/TextInputPrimitive.js +54 -0
- package/dist/ui/TextInput/index.d.ts +89 -0
- package/dist/ui/TextInput/index.d.ts.map +1 -0
- package/dist/ui/TextInput/index.js +545 -0
- package/dist/ui/WheelColumn/ZKitWheelPickerNativeComponent.d.ts +39 -0
- package/dist/ui/WheelColumn/ZKitWheelPickerNativeComponent.d.ts.map +1 -0
- package/dist/ui/WheelColumn/ZKitWheelPickerNativeComponent.js +60 -0
- package/dist/ui/WheelColumn/constants.d.ts +2 -0
- package/dist/ui/WheelColumn/constants.d.ts.map +1 -0
- package/dist/ui/WheelColumn/constants.js +4 -0
- package/dist/ui/WheelColumn/index.d.ts +59 -0
- package/dist/ui/WheelColumn/index.d.ts.map +1 -0
- package/dist/ui/WheelColumn/index.js +547 -0
- package/ios/ZKitWheelPicker.h +18 -0
- package/ios/ZKitWheelPicker.mm +230 -0
- package/ios/ZKitWheelPickerLabel.h +10 -0
- package/ios/ZKitWheelPickerLabel.mm +31 -0
- package/ios/ZKitWheelPickerManager.h +5 -0
- package/ios/ZKitWheelPickerManager.mm +53 -0
- package/package.json +75 -0
- package/react-native.config.js +10 -0
- package/src/ComponentLibProvider.tsx +56 -0
- package/src/assets/animations/spinner.json +1 -0
- package/src/assets/icons/check.svg +4 -0
- package/src/assets/icons/toast/error.webp +0 -0
- package/src/assets/icons/toast/success.webp +0 -0
- package/src/assets/icons/toast/warning.webp +0 -0
- package/src/config.ts +65 -0
- package/src/i18n/I18nProvider.tsx +69 -0
- package/src/i18n/locale.ts +108 -0
- package/src/i18n/locales/de.ts +67 -0
- package/src/i18n/locales/en-US.ts +67 -0
- package/src/i18n/locales/index.ts +19 -0
- package/src/i18n/locales/ja.ts +67 -0
- package/src/i18n/locales/zh-CN.ts +67 -0
- package/src/i18n/locales/zh-TW.ts +67 -0
- package/src/i18n/types.ts +21 -0
- package/src/i18n/useI18n.ts +7 -0
- package/src/index.ts +395 -0
- package/src/services/ActionDialogService/ActionDialog.tsx +756 -0
- package/src/services/ActionDialogService/README.md +146 -0
- package/src/services/ActionDialogService/index.tsx +39 -0
- package/src/services/ActionDialogService/service.tsx +355 -0
- package/src/services/ActionDialogService/shared.ts +204 -0
- package/src/services/ActionDialogService/types.ts +197 -0
- package/src/services/CardToastService/README.md +128 -0
- package/src/services/CardToastService/index.tsx +1284 -0
- package/src/services/ImagePreviewService/README.md +67 -0
- package/src/services/ImagePreviewService/index.tsx +2148 -0
- package/src/services/LoadingService/README.md +128 -0
- package/src/services/LoadingService/index.tsx +1113 -0
- package/src/services/PermissionPurposeDialogService/README.md +80 -0
- package/src/services/PermissionPurposeDialogService/index.tsx +449 -0
- package/src/services/PickerService/README.md +154 -0
- package/src/services/PickerService/index.tsx +410 -0
- package/src/theme/ThemeProvider.tsx +40 -0
- package/src/theme/defaultTheme.ts +30 -0
- package/src/theme/mergeTheme.ts +16 -0
- package/src/theme/types.ts +36 -0
- package/src/theme/useTheme.ts +10 -0
- package/src/types/svg.d.ts +9 -0
- package/src/types/vant-area-data.d.ts +19 -0
- package/src/ui/Accordion/README.md +67 -0
- package/src/ui/Accordion/index.tsx +1221 -0
- package/src/ui/AddressCascader/README.md +144 -0
- package/src/ui/AddressCascader/index.tsx +1084 -0
- package/src/ui/BetweenTime/README.md +490 -0
- package/src/ui/BetweenTime/index.tsx +1001 -0
- package/src/ui/Button/README.md +186 -0
- package/src/ui/Button/index.tsx +1356 -0
- package/src/ui/Checkbox/README.md +211 -0
- package/src/ui/Checkbox/index.tsx +1231 -0
- package/src/ui/DatePicker/README.md +117 -0
- package/src/ui/DatePicker/index.tsx +295 -0
- package/src/ui/DatePicker/types.ts +115 -0
- package/src/ui/DatePicker/utils.ts +405 -0
- package/src/ui/LinkedScroll/README.md +55 -0
- package/src/ui/LinkedScroll/index.tsx +809 -0
- package/src/ui/LoadingSpinner/index.tsx +104 -0
- package/src/ui/Picker/README.md +190 -0
- package/src/ui/Picker/actionBar.tsx +70 -0
- package/src/ui/Picker/index.tsx +694 -0
- package/src/ui/Picker/types.ts +94 -0
- package/src/ui/Picker/utils.ts +239 -0
- package/src/ui/Radio/README.md +207 -0
- package/src/ui/Radio/index.tsx +1117 -0
- package/src/ui/Sheet/README.md +82 -0
- package/src/ui/Sheet/index.tsx +1163 -0
- package/src/ui/Sheet/nativeBottom.tsx +683 -0
- package/src/ui/Sheet/nativeBottom.web.tsx +40 -0
- package/src/ui/Sheet/slots.tsx +75 -0
- package/src/ui/Sheet/types.ts +195 -0
- package/src/ui/SliderCaptcha/README.md +61 -0
- package/src/ui/SliderCaptcha/index.tsx +816 -0
- package/src/ui/Switch/README.md +143 -0
- package/src/ui/Switch/index.tsx +1092 -0
- package/src/ui/Text/README.md +157 -0
- package/src/ui/Text/index.tsx +450 -0
- package/src/ui/TextInput/README.md +197 -0
- package/src/ui/TextInput/TextInputPrimitive.tsx +44 -0
- package/src/ui/TextInput/index.tsx +942 -0
- package/src/ui/WheelColumn/README.md +104 -0
- package/src/ui/WheelColumn/ZKitWheelPickerNativeComponent.tsx +68 -0
- package/src/ui/WheelColumn/constants.ts +1 -0
- package/src/ui/WheelColumn/index.tsx +826 -0
- package/zkit-ui.podspec +19 -0
|
@@ -0,0 +1,1084 @@
|
|
|
1
|
+
import { Feather } from '@expo/vector-icons';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import {
|
|
4
|
+
Animated,
|
|
5
|
+
Easing,
|
|
6
|
+
Pressable,
|
|
7
|
+
ScrollView,
|
|
8
|
+
StyleSheet,
|
|
9
|
+
useWindowDimensions,
|
|
10
|
+
View,
|
|
11
|
+
type LayoutChangeEvent,
|
|
12
|
+
} from 'react-native';
|
|
13
|
+
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
|
14
|
+
import { sp, wp } from 'zkit-tools';
|
|
15
|
+
import { useI18n } from '../../i18n/useI18n';
|
|
16
|
+
import { useTheme } from '../../theme/useTheme';
|
|
17
|
+
import { Sheet, type SheetOpenChangeDetails } from '../Sheet';
|
|
18
|
+
import { Text } from '../Text';
|
|
19
|
+
import { PickerActionBar, getPickerActionBarBottomInset } from '../Picker/actionBar';
|
|
20
|
+
import { areaList } from '@vant/area-data';
|
|
21
|
+
|
|
22
|
+
export type AddressCascaderHandle = {
|
|
23
|
+
open: () => void;
|
|
24
|
+
close: () => void;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type AddressCascaderOption = {
|
|
28
|
+
value?: string | number;
|
|
29
|
+
text?: string | number;
|
|
30
|
+
label?: string | number;
|
|
31
|
+
title?: string | number;
|
|
32
|
+
name?: string | number;
|
|
33
|
+
id?: string | number;
|
|
34
|
+
code?: string | number;
|
|
35
|
+
key?: string | number;
|
|
36
|
+
disabled?: boolean;
|
|
37
|
+
children?: AddressCascaderOption[];
|
|
38
|
+
[key: string]: unknown;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// 懒加载转换省市区数据。
|
|
42
|
+
let cachedAreaData: AddressCascaderOption[] | null = null;
|
|
43
|
+
|
|
44
|
+
function getAreaData(): AddressCascaderOption[] {
|
|
45
|
+
if (cachedAreaData) return cachedAreaData;
|
|
46
|
+
|
|
47
|
+
const { province_list, city_list, county_list } = areaList;
|
|
48
|
+
|
|
49
|
+
const countiesByCity = new Map<string, AddressCascaderOption[]>();
|
|
50
|
+
for (const [countyCode, countyName] of Object.entries(county_list)) {
|
|
51
|
+
const cityPrefix = countyCode.slice(0, 4);
|
|
52
|
+
const county = { value: countyCode, text: String(countyName) };
|
|
53
|
+
const counties = countiesByCity.get(cityPrefix);
|
|
54
|
+
|
|
55
|
+
if (counties) {
|
|
56
|
+
counties.push(county);
|
|
57
|
+
} else {
|
|
58
|
+
countiesByCity.set(cityPrefix, [county]);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const citiesByProvince = new Map<string, AddressCascaderOption[]>();
|
|
63
|
+
for (const [cityCode, cityName] of Object.entries(city_list)) {
|
|
64
|
+
const provincePrefix = cityCode.slice(0, 2);
|
|
65
|
+
const children = countiesByCity.get(cityCode.slice(0, 4));
|
|
66
|
+
const city: AddressCascaderOption = children?.length
|
|
67
|
+
? { value: cityCode, text: String(cityName), children }
|
|
68
|
+
: { value: cityCode, text: String(cityName) };
|
|
69
|
+
const cities = citiesByProvince.get(provincePrefix);
|
|
70
|
+
|
|
71
|
+
if (cities) {
|
|
72
|
+
cities.push(city);
|
|
73
|
+
} else {
|
|
74
|
+
citiesByProvince.set(provincePrefix, [city]);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const provinces: AddressCascaderOption[] = Object.entries(province_list).map(([provinceCode, provinceName]) => {
|
|
79
|
+
const children = citiesByProvince.get(provinceCode.slice(0, 2));
|
|
80
|
+
return children?.length
|
|
81
|
+
? { value: provinceCode, text: String(provinceName), children }
|
|
82
|
+
: { value: provinceCode, text: String(provinceName) };
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
cachedAreaData = provinces;
|
|
86
|
+
return provinces;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export type AddressCascaderValue = string[];
|
|
90
|
+
|
|
91
|
+
export type AddressCascaderConfirmPayload = {
|
|
92
|
+
value: AddressCascaderValue;
|
|
93
|
+
values: string[];
|
|
94
|
+
label: string;
|
|
95
|
+
labels: string[];
|
|
96
|
+
items: AddressCascaderOption[];
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export type AddressCascaderChangePayload = AddressCascaderConfirmPayload;
|
|
100
|
+
|
|
101
|
+
export type AddressCascaderRenderContext = {
|
|
102
|
+
value: AddressCascaderValue;
|
|
103
|
+
label: string;
|
|
104
|
+
labels: string[];
|
|
105
|
+
items: AddressCascaderOption[];
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export type AddressCascaderProps = {
|
|
109
|
+
list?: AddressCascaderOption[];
|
|
110
|
+
|
|
111
|
+
value?: AddressCascaderValue;
|
|
112
|
+
defaultValue?: AddressCascaderValue;
|
|
113
|
+
onValueChange?: (next: AddressCascaderValue) => void;
|
|
114
|
+
|
|
115
|
+
open?: boolean;
|
|
116
|
+
defaultOpen?: boolean;
|
|
117
|
+
onOpenChange?: (next: boolean) => void;
|
|
118
|
+
onDismissComplete?: () => void;
|
|
119
|
+
|
|
120
|
+
label?: string;
|
|
121
|
+
defaultLabel?: string;
|
|
122
|
+
onLabelChange?: (next: string) => void;
|
|
123
|
+
|
|
124
|
+
title?: string;
|
|
125
|
+
separator?: string;
|
|
126
|
+
levelLabels?: string[];
|
|
127
|
+
|
|
128
|
+
lazyContent?: boolean;
|
|
129
|
+
drawerSize?: string | number;
|
|
130
|
+
disabled?: boolean;
|
|
131
|
+
|
|
132
|
+
onCancel?: () => void;
|
|
133
|
+
onConfirm?: (payload: AddressCascaderConfirmPayload) => void;
|
|
134
|
+
onChange?: (payload: AddressCascaderChangePayload) => void;
|
|
135
|
+
|
|
136
|
+
children?: React.ReactNode | ((ctx: AddressCascaderRenderContext) => React.ReactNode);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
type AddressDraft = {
|
|
140
|
+
value: AddressCascaderValue;
|
|
141
|
+
labels: string[];
|
|
142
|
+
items: AddressCascaderOption[];
|
|
143
|
+
activeLevel: number;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
type AddressOption = {
|
|
147
|
+
item: AddressCascaderOption;
|
|
148
|
+
index: number;
|
|
149
|
+
level: number;
|
|
150
|
+
value: string;
|
|
151
|
+
text: string;
|
|
152
|
+
disabled: boolean;
|
|
153
|
+
hasChildren: boolean;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
type Primitive = string | number;
|
|
157
|
+
|
|
158
|
+
const MAX_LEVELS = 3;
|
|
159
|
+
const LEVELS = Array.from({ length: MAX_LEVELS }, (_, level) => level);
|
|
160
|
+
const LIST_FADE_DURATION = 180;
|
|
161
|
+
const LIST_FADE_START_OPACITY = 0.58;
|
|
162
|
+
const OPTION_ROW_ESTIMATED_HEIGHT = wp(58);
|
|
163
|
+
const SELECTED_OPTION_VIEW_POSITION = 0.18;
|
|
164
|
+
|
|
165
|
+
function clampNumber(n: number, min: number, max: number) {
|
|
166
|
+
return Math.max(min, Math.min(max, n));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function pickPrimitive(node: AddressCascaderOption, keys: string[], fallback?: Primitive): Primitive | undefined {
|
|
170
|
+
for (const key of keys) {
|
|
171
|
+
const v = node?.[key];
|
|
172
|
+
if (typeof v === 'string' || typeof v === 'number') return v;
|
|
173
|
+
}
|
|
174
|
+
return fallback;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function pickValue(node: AddressCascaderOption, fallbackIndex?: number) {
|
|
178
|
+
const fallback = fallbackIndex == null ? undefined : String(fallbackIndex);
|
|
179
|
+
return String(pickPrimitive(node, ['value', 'id', 'code', 'key'], fallback) ?? '');
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function pickText(node: AddressCascaderOption) {
|
|
183
|
+
const v = pickPrimitive(node, ['text', 'label', 'title', 'name']);
|
|
184
|
+
return v == null ? '' : String(v);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function getChildren(node: AddressCascaderOption | undefined) {
|
|
188
|
+
return Array.isArray(node?.children) ? node.children : [];
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function hasNextLevel(node: AddressCascaderOption | undefined, level: number) {
|
|
192
|
+
return level < MAX_LEVELS - 1 && getChildren(node).length > 0;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function buildLabel(labels: string[], separator: string) {
|
|
196
|
+
return labels.filter(Boolean).join(separator);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function resolvePath(
|
|
200
|
+
list: AddressCascaderOption[],
|
|
201
|
+
value: AddressCascaderValue | undefined
|
|
202
|
+
): Omit<AddressDraft, 'activeLevel'> {
|
|
203
|
+
const nextValue: string[] = [];
|
|
204
|
+
const labels: string[] = [];
|
|
205
|
+
const items: AddressCascaderOption[] = [];
|
|
206
|
+
let currentList = Array.isArray(list) ? list : [];
|
|
207
|
+
|
|
208
|
+
for (let level = 0; level < MAX_LEVELS; level += 1) {
|
|
209
|
+
const desired = value?.[level];
|
|
210
|
+
if (desired == null || !currentList.length) break;
|
|
211
|
+
|
|
212
|
+
const idx = currentList.findIndex((item, index) => pickValue(item, index) === String(desired));
|
|
213
|
+
if (idx < 0) break;
|
|
214
|
+
|
|
215
|
+
const item = currentList[idx];
|
|
216
|
+
if (!item) break;
|
|
217
|
+
|
|
218
|
+
nextValue.push(pickValue(item, idx));
|
|
219
|
+
labels.push(pickText(item));
|
|
220
|
+
items.push(item);
|
|
221
|
+
currentList = getChildren(item);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return { value: nextValue, labels, items };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function getActiveLevelFromItems(items: AddressCascaderOption[]) {
|
|
228
|
+
if (!items.length) return 0;
|
|
229
|
+
const lastIndex = Math.min(items.length - 1, MAX_LEVELS - 1);
|
|
230
|
+
const lastItem = items[lastIndex];
|
|
231
|
+
if (hasNextLevel(lastItem, lastIndex) && items.length < MAX_LEVELS) return items.length;
|
|
232
|
+
return lastIndex;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function createDraft(list: AddressCascaderOption[], value: AddressCascaderValue | undefined): AddressDraft {
|
|
236
|
+
const resolved = resolvePath(list, value);
|
|
237
|
+
return {
|
|
238
|
+
...resolved,
|
|
239
|
+
activeLevel: getActiveLevelFromItems(resolved.items),
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function getListForLevel(
|
|
244
|
+
list: AddressCascaderOption[],
|
|
245
|
+
items: AddressCascaderOption[],
|
|
246
|
+
level: number
|
|
247
|
+
) {
|
|
248
|
+
let currentList = Array.isArray(list) ? list : [];
|
|
249
|
+
for (let i = 0; i < level; i += 1) {
|
|
250
|
+
const next = getChildren(items[i]);
|
|
251
|
+
if (!next.length) return [];
|
|
252
|
+
currentList = next;
|
|
253
|
+
}
|
|
254
|
+
return currentList;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function isCompleteDraft(draft: AddressDraft) {
|
|
258
|
+
if (!draft.items.length) return false;
|
|
259
|
+
const lastLevel = draft.items.length - 1;
|
|
260
|
+
const lastItem = draft.items[lastLevel];
|
|
261
|
+
return !hasNextLevel(lastItem, lastLevel);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function composeTrigger(
|
|
265
|
+
children: AddressCascaderProps['children'],
|
|
266
|
+
onPress: () => void,
|
|
267
|
+
disabled: boolean | undefined,
|
|
268
|
+
ctx: AddressCascaderRenderContext
|
|
269
|
+
) {
|
|
270
|
+
if (typeof children === 'function') {
|
|
271
|
+
const node = children(ctx);
|
|
272
|
+
return composeTrigger(node, onPress, disabled, ctx);
|
|
273
|
+
}
|
|
274
|
+
if (React.isValidElement(children)) {
|
|
275
|
+
const child = children as React.ReactElement<{
|
|
276
|
+
onPress?: (...args: unknown[]) => void;
|
|
277
|
+
disabled?: boolean;
|
|
278
|
+
}>;
|
|
279
|
+
const prevOnPress = child.props.onPress;
|
|
280
|
+
if (typeof prevOnPress === 'function') {
|
|
281
|
+
return React.cloneElement(child, {
|
|
282
|
+
onPress: (...args: unknown[]) => {
|
|
283
|
+
prevOnPress(...args);
|
|
284
|
+
onPress();
|
|
285
|
+
},
|
|
286
|
+
disabled: disabled || child.props.disabled,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
return React.cloneElement(child, {
|
|
290
|
+
onPress,
|
|
291
|
+
disabled: disabled || child.props.disabled,
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
return (
|
|
295
|
+
<Pressable onPress={onPress} disabled={disabled}>
|
|
296
|
+
{children}
|
|
297
|
+
</Pressable>
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
type AddressOptionRowProps = {
|
|
302
|
+
option: AddressOption;
|
|
303
|
+
selected: boolean;
|
|
304
|
+
disabled: boolean;
|
|
305
|
+
primaryColor: string;
|
|
306
|
+
onSurfaceColor: string;
|
|
307
|
+
mutedColor: string;
|
|
308
|
+
onPress: (item: AddressCascaderOption, index: number, level: number) => void;
|
|
309
|
+
onLayout?: (event: LayoutChangeEvent) => void;
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
const AddressOptionRow = React.memo(function AddressOptionRow({
|
|
313
|
+
option,
|
|
314
|
+
selected,
|
|
315
|
+
disabled,
|
|
316
|
+
primaryColor,
|
|
317
|
+
onSurfaceColor,
|
|
318
|
+
mutedColor,
|
|
319
|
+
onPress,
|
|
320
|
+
onLayout,
|
|
321
|
+
}: AddressOptionRowProps) {
|
|
322
|
+
const handlePress = React.useCallback(() => {
|
|
323
|
+
onPress(option.item, option.index, option.level);
|
|
324
|
+
}, [onPress, option]);
|
|
325
|
+
|
|
326
|
+
return (
|
|
327
|
+
<Pressable
|
|
328
|
+
accessibilityRole="button"
|
|
329
|
+
disabled={disabled || option.disabled}
|
|
330
|
+
onLayout={onLayout}
|
|
331
|
+
onPress={handlePress}
|
|
332
|
+
style={({ pressed }) => [
|
|
333
|
+
styles.optionRow,
|
|
334
|
+
{
|
|
335
|
+
opacity: option.disabled ? 0.46 : pressed ? 0.78 : 1,
|
|
336
|
+
},
|
|
337
|
+
]}
|
|
338
|
+
>
|
|
339
|
+
<View style={styles.optionMain}>
|
|
340
|
+
<Text
|
|
341
|
+
numberOfLines={2}
|
|
342
|
+
style={[
|
|
343
|
+
styles.optionText,
|
|
344
|
+
{
|
|
345
|
+
color: selected ? primaryColor : onSurfaceColor,
|
|
346
|
+
fontWeight: selected ? '700' : '500',
|
|
347
|
+
},
|
|
348
|
+
]}
|
|
349
|
+
>
|
|
350
|
+
{option.text}
|
|
351
|
+
</Text>
|
|
352
|
+
</View>
|
|
353
|
+
<View style={styles.optionMark}>
|
|
354
|
+
{selected ? (
|
|
355
|
+
<Feather name="check" size={wp(19)} color={primaryColor} />
|
|
356
|
+
) : option.hasChildren ? (
|
|
357
|
+
<Feather name="chevron-right" size={wp(18)} color={mutedColor} />
|
|
358
|
+
) : null}
|
|
359
|
+
</View>
|
|
360
|
+
</Pressable>
|
|
361
|
+
);
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
export const AddressCascader = React.forwardRef<AddressCascaderHandle, AddressCascaderProps>(function AddressCascader({
|
|
365
|
+
list,
|
|
366
|
+
value: valueProp,
|
|
367
|
+
defaultValue,
|
|
368
|
+
onValueChange,
|
|
369
|
+
open: openProp,
|
|
370
|
+
defaultOpen = false,
|
|
371
|
+
onOpenChange,
|
|
372
|
+
onDismissComplete,
|
|
373
|
+
label: labelProp,
|
|
374
|
+
defaultLabel,
|
|
375
|
+
onLabelChange,
|
|
376
|
+
title,
|
|
377
|
+
separator = ' / ',
|
|
378
|
+
levelLabels,
|
|
379
|
+
lazyContent = true,
|
|
380
|
+
drawerSize,
|
|
381
|
+
disabled = false,
|
|
382
|
+
onCancel,
|
|
383
|
+
onConfirm,
|
|
384
|
+
onChange,
|
|
385
|
+
children,
|
|
386
|
+
}, ref) {
|
|
387
|
+
const { t } = useI18n();
|
|
388
|
+
const theme = useTheme();
|
|
389
|
+
const { height: screenH } = useWindowDimensions();
|
|
390
|
+
const insets = useSafeAreaInsets();
|
|
391
|
+
const safeBottom = getPickerActionBarBottomInset(insets.bottom);
|
|
392
|
+
const areaData = React.useMemo(() => list ?? getAreaData(), [list]);
|
|
393
|
+
|
|
394
|
+
const isValueControlled = valueProp !== undefined;
|
|
395
|
+
const [innerValue, setInnerValue] = React.useState<AddressCascaderValue | undefined>(defaultValue);
|
|
396
|
+
const value = valueProp !== undefined ? valueProp : innerValue;
|
|
397
|
+
|
|
398
|
+
const [innerOpen, setInnerOpen] = React.useState(defaultOpen);
|
|
399
|
+
const isOpenControlled = openProp !== undefined;
|
|
400
|
+
const visible = openProp !== undefined ? !!openProp : innerOpen;
|
|
401
|
+
const [contentMounted, setContentMounted] = React.useState(() => !lazyContent || visible);
|
|
402
|
+
|
|
403
|
+
const [innerLabel, setInnerLabel] = React.useState(defaultLabel ?? '');
|
|
404
|
+
const [draft, setDraft] = React.useState(() => createDraft(areaData, value));
|
|
405
|
+
const draftRef = React.useRef(draft);
|
|
406
|
+
draftRef.current = draft;
|
|
407
|
+
|
|
408
|
+
const defaultLevelLabels = React.useMemo(
|
|
409
|
+
() => [
|
|
410
|
+
t('addressCascader.province'),
|
|
411
|
+
t('addressCascader.city'),
|
|
412
|
+
t('addressCascader.district'),
|
|
413
|
+
],
|
|
414
|
+
[t]
|
|
415
|
+
);
|
|
416
|
+
const resolvedLevelLabels = React.useMemo(() => {
|
|
417
|
+
const next = levelLabels?.length ? levelLabels : defaultLevelLabels;
|
|
418
|
+
return Array.from({ length: MAX_LEVELS }, (_, i) => next[i] ?? defaultLevelLabels[i] ?? '');
|
|
419
|
+
}, [defaultLevelLabels, levelLabels]);
|
|
420
|
+
|
|
421
|
+
React.useEffect(() => {
|
|
422
|
+
const nextDraft = createDraft(areaData, value);
|
|
423
|
+
draftRef.current = nextDraft;
|
|
424
|
+
setDraft(nextDraft);
|
|
425
|
+
}, [areaData, value]);
|
|
426
|
+
|
|
427
|
+
React.useEffect(() => {
|
|
428
|
+
if (visible) {
|
|
429
|
+
const nextDraft = createDraft(areaData, value);
|
|
430
|
+
draftRef.current = nextDraft;
|
|
431
|
+
setDraft(nextDraft);
|
|
432
|
+
if (lazyContent) setContentMounted(true);
|
|
433
|
+
}
|
|
434
|
+
// 这里只希望在进入打开态时重置草稿和挂载懒加载内容。
|
|
435
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
436
|
+
}, [visible]);
|
|
437
|
+
|
|
438
|
+
const sheetHeight = React.useMemo(() => {
|
|
439
|
+
const maxHeight = Math.max(wp(320), screenH - Math.max(insets.top, wp(24)));
|
|
440
|
+
const n = typeof drawerSize === 'number' ? drawerSize : drawerSize == null ? undefined : Number.parseFloat(drawerSize);
|
|
441
|
+
if (n != null && Number.isFinite(n) && n > 0) {
|
|
442
|
+
return clampNumber(n, Math.min(wp(360), maxHeight), maxHeight);
|
|
443
|
+
}
|
|
444
|
+
return clampNumber(screenH * 0.72, Math.min(wp(430), maxHeight), maxHeight);
|
|
445
|
+
}, [drawerSize, insets.top, screenH]);
|
|
446
|
+
|
|
447
|
+
const detents = React.useMemo<Array<'auto' | number>>(
|
|
448
|
+
() => [clampNumber(sheetHeight / screenH, 0.1, 0.92)],
|
|
449
|
+
[screenH, sheetHeight]
|
|
450
|
+
);
|
|
451
|
+
|
|
452
|
+
const setCascaderOpen = React.useCallback((nextOpen: boolean) => {
|
|
453
|
+
onOpenChange?.(nextOpen);
|
|
454
|
+
if (!isOpenControlled) setInnerOpen(nextOpen);
|
|
455
|
+
}, [isOpenControlled, onOpenChange]);
|
|
456
|
+
|
|
457
|
+
const close = React.useCallback(() => {
|
|
458
|
+
setCascaderOpen(false);
|
|
459
|
+
}, [setCascaderOpen]);
|
|
460
|
+
|
|
461
|
+
const openPicker = React.useCallback(() => {
|
|
462
|
+
if (disabled) return;
|
|
463
|
+
if (!visible) {
|
|
464
|
+
setCascaderOpen(true);
|
|
465
|
+
}
|
|
466
|
+
const nextDraft = createDraft(areaData, value);
|
|
467
|
+
draftRef.current = nextDraft;
|
|
468
|
+
setDraft(nextDraft);
|
|
469
|
+
if (lazyContent) setContentMounted(true);
|
|
470
|
+
}, [areaData, disabled, lazyContent, setCascaderOpen, value, visible]);
|
|
471
|
+
|
|
472
|
+
React.useImperativeHandle(ref, () => ({
|
|
473
|
+
open: openPicker,
|
|
474
|
+
close,
|
|
475
|
+
}), [close, openPicker]);
|
|
476
|
+
|
|
477
|
+
const committed = React.useMemo(() => resolvePath(areaData, value), [areaData, value]);
|
|
478
|
+
const committedLabel = React.useMemo(() => buildLabel(committed.labels, separator), [committed.labels, separator]);
|
|
479
|
+
const effectiveLabel = labelProp !== undefined ? labelProp : committedLabel || innerLabel;
|
|
480
|
+
const effectiveCtx = React.useMemo<AddressCascaderRenderContext>(
|
|
481
|
+
() => ({
|
|
482
|
+
value: committed.value,
|
|
483
|
+
label: effectiveLabel,
|
|
484
|
+
labels: committed.labels,
|
|
485
|
+
items: committed.items,
|
|
486
|
+
}),
|
|
487
|
+
[committed.items, committed.labels, committed.value, effectiveLabel]
|
|
488
|
+
);
|
|
489
|
+
|
|
490
|
+
const currentList = React.useMemo(
|
|
491
|
+
() => getListForLevel(areaData, draft.items, draft.activeLevel),
|
|
492
|
+
[areaData, draft.activeLevel, draft.items]
|
|
493
|
+
);
|
|
494
|
+
const currentOptions = React.useMemo<AddressOption[]>(
|
|
495
|
+
() =>
|
|
496
|
+
currentList.map((item, index) => ({
|
|
497
|
+
item,
|
|
498
|
+
index,
|
|
499
|
+
level: draft.activeLevel,
|
|
500
|
+
value: pickValue(item, index),
|
|
501
|
+
text: pickText(item),
|
|
502
|
+
disabled: !!item.disabled,
|
|
503
|
+
hasChildren: hasNextLevel(item, draft.activeLevel),
|
|
504
|
+
})),
|
|
505
|
+
[currentList, draft.activeLevel]
|
|
506
|
+
);
|
|
507
|
+
const activeSelectedValue = draft.value[draft.activeLevel];
|
|
508
|
+
const activeSelectedIndex = React.useMemo(
|
|
509
|
+
() => currentOptions.findIndex((item) => item.value === activeSelectedValue),
|
|
510
|
+
[activeSelectedValue, currentOptions]
|
|
511
|
+
);
|
|
512
|
+
const currentOptionsRef = React.useRef(currentOptions);
|
|
513
|
+
const activeSelectedIndexRef = React.useRef(activeSelectedIndex);
|
|
514
|
+
currentOptionsRef.current = currentOptions;
|
|
515
|
+
activeSelectedIndexRef.current = activeSelectedIndex;
|
|
516
|
+
const confirmDisabled = disabled || !isCompleteDraft(draft);
|
|
517
|
+
const stepsScrollRef = React.useRef<ScrollView>(null);
|
|
518
|
+
const optionsScrollRef = React.useRef<ScrollView>(null);
|
|
519
|
+
const optionLayoutYRef = React.useRef(new Map<string, number>());
|
|
520
|
+
const optionsViewportHeightRef = React.useRef(0);
|
|
521
|
+
const pendingOptionScrollFrameRef = React.useRef<number | null>(null);
|
|
522
|
+
const pendingActiveOptionScrollRef = React.useRef(false);
|
|
523
|
+
const listOpacity = React.useRef(new Animated.Value(1)).current;
|
|
524
|
+
const pendingListTransitionFrameRef = React.useRef<number | null>(null);
|
|
525
|
+
const previousActiveLevelRef = React.useRef(draft.activeLevel);
|
|
526
|
+
|
|
527
|
+
const listAnimatedStyle = React.useMemo(
|
|
528
|
+
() => ({
|
|
529
|
+
opacity: listOpacity,
|
|
530
|
+
}),
|
|
531
|
+
[listOpacity]
|
|
532
|
+
);
|
|
533
|
+
|
|
534
|
+
React.useEffect(() => {
|
|
535
|
+
const rafId = requestAnimationFrame(() => {
|
|
536
|
+
if (draft.activeLevel <= 0) {
|
|
537
|
+
stepsScrollRef.current?.scrollTo({ x: 0, animated: true });
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
stepsScrollRef.current?.scrollToEnd({ animated: true });
|
|
541
|
+
});
|
|
542
|
+
return () => cancelAnimationFrame(rafId);
|
|
543
|
+
}, [draft.activeLevel]);
|
|
544
|
+
|
|
545
|
+
const cancelListTransitionFrame = React.useCallback(() => {
|
|
546
|
+
if (pendingListTransitionFrameRef.current == null) return;
|
|
547
|
+
cancelAnimationFrame(pendingListTransitionFrameRef.current);
|
|
548
|
+
pendingListTransitionFrameRef.current = null;
|
|
549
|
+
}, []);
|
|
550
|
+
|
|
551
|
+
const resetListTransition = React.useCallback(() => {
|
|
552
|
+
cancelListTransitionFrame();
|
|
553
|
+
listOpacity.stopAnimation();
|
|
554
|
+
listOpacity.setValue(1);
|
|
555
|
+
}, [cancelListTransitionFrame, listOpacity]);
|
|
556
|
+
|
|
557
|
+
const startListTransition = React.useCallback(
|
|
558
|
+
(fromLevel: number, toLevel: number) => {
|
|
559
|
+
previousActiveLevelRef.current = toLevel;
|
|
560
|
+
|
|
561
|
+
if (!visible || !contentMounted || fromLevel === toLevel) {
|
|
562
|
+
resetListTransition();
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
cancelListTransitionFrame();
|
|
567
|
+
listOpacity.stopAnimation();
|
|
568
|
+
listOpacity.setValue(LIST_FADE_START_OPACITY);
|
|
569
|
+
|
|
570
|
+
pendingListTransitionFrameRef.current = requestAnimationFrame(() => {
|
|
571
|
+
pendingListTransitionFrameRef.current = null;
|
|
572
|
+
Animated.timing(listOpacity, {
|
|
573
|
+
toValue: 1,
|
|
574
|
+
duration: LIST_FADE_DURATION,
|
|
575
|
+
easing: Easing.out(Easing.cubic),
|
|
576
|
+
useNativeDriver: true,
|
|
577
|
+
}).start();
|
|
578
|
+
});
|
|
579
|
+
},
|
|
580
|
+
[
|
|
581
|
+
cancelListTransitionFrame,
|
|
582
|
+
contentMounted,
|
|
583
|
+
listOpacity,
|
|
584
|
+
resetListTransition,
|
|
585
|
+
visible,
|
|
586
|
+
]
|
|
587
|
+
);
|
|
588
|
+
|
|
589
|
+
React.useEffect(() => {
|
|
590
|
+
if (!visible || !contentMounted) {
|
|
591
|
+
previousActiveLevelRef.current = draft.activeLevel;
|
|
592
|
+
resetListTransition();
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
if (previousActiveLevelRef.current !== draft.activeLevel) {
|
|
597
|
+
previousActiveLevelRef.current = draft.activeLevel;
|
|
598
|
+
resetListTransition();
|
|
599
|
+
}
|
|
600
|
+
}, [contentMounted, draft.activeLevel, resetListTransition, visible]);
|
|
601
|
+
|
|
602
|
+
React.useEffect(() => {
|
|
603
|
+
return resetListTransition;
|
|
604
|
+
}, [resetListTransition]);
|
|
605
|
+
|
|
606
|
+
const keyExtractor = React.useCallback(
|
|
607
|
+
(item: AddressOption) => `${item.level}-${item.value}-${item.index}`,
|
|
608
|
+
[]
|
|
609
|
+
);
|
|
610
|
+
|
|
611
|
+
const scrollActiveOptionIntoView = React.useCallback(
|
|
612
|
+
(animated: boolean) => {
|
|
613
|
+
const scrollView = optionsScrollRef.current;
|
|
614
|
+
if (!scrollView) return;
|
|
615
|
+
|
|
616
|
+
const activeIndex = activeSelectedIndexRef.current;
|
|
617
|
+
if (activeIndex <= 0) {
|
|
618
|
+
scrollView.scrollTo({ y: 0, animated });
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
const activeOption = currentOptionsRef.current[activeIndex];
|
|
623
|
+
if (!activeOption) return;
|
|
624
|
+
|
|
625
|
+
const key = keyExtractor(activeOption);
|
|
626
|
+
const layoutY = optionLayoutYRef.current.get(key);
|
|
627
|
+
const viewportHeight = optionsViewportHeightRef.current;
|
|
628
|
+
const fallbackY = activeIndex * OPTION_ROW_ESTIMATED_HEIGHT;
|
|
629
|
+
const measuredY = layoutY ?? fallbackY;
|
|
630
|
+
const viewOffset = viewportHeight > 0 ? viewportHeight * SELECTED_OPTION_VIEW_POSITION : 0;
|
|
631
|
+
|
|
632
|
+
scrollView.scrollTo({
|
|
633
|
+
y: Math.max(0, measuredY - viewOffset),
|
|
634
|
+
animated,
|
|
635
|
+
});
|
|
636
|
+
},
|
|
637
|
+
[keyExtractor]
|
|
638
|
+
);
|
|
639
|
+
|
|
640
|
+
const scheduleActiveOptionScroll = React.useCallback(
|
|
641
|
+
(animated = false) => {
|
|
642
|
+
if (!visible || !contentMounted || !pendingActiveOptionScrollRef.current) return;
|
|
643
|
+
|
|
644
|
+
if (pendingOptionScrollFrameRef.current != null) {
|
|
645
|
+
cancelAnimationFrame(pendingOptionScrollFrameRef.current);
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
pendingOptionScrollFrameRef.current = requestAnimationFrame(() => {
|
|
649
|
+
pendingOptionScrollFrameRef.current = null;
|
|
650
|
+
if (!pendingActiveOptionScrollRef.current) return;
|
|
651
|
+
pendingActiveOptionScrollRef.current = false;
|
|
652
|
+
scrollActiveOptionIntoView(animated);
|
|
653
|
+
});
|
|
654
|
+
},
|
|
655
|
+
[contentMounted, scrollActiveOptionIntoView, visible]
|
|
656
|
+
);
|
|
657
|
+
|
|
658
|
+
React.useEffect(() => {
|
|
659
|
+
optionLayoutYRef.current.clear();
|
|
660
|
+
}, [currentOptions]);
|
|
661
|
+
|
|
662
|
+
React.useEffect(() => {
|
|
663
|
+
if (!visible || !contentMounted) {
|
|
664
|
+
pendingActiveOptionScrollRef.current = false;
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
pendingActiveOptionScrollRef.current = true;
|
|
669
|
+
scheduleActiveOptionScroll(false);
|
|
670
|
+
return () => {
|
|
671
|
+
if (pendingOptionScrollFrameRef.current != null) {
|
|
672
|
+
cancelAnimationFrame(pendingOptionScrollFrameRef.current);
|
|
673
|
+
pendingOptionScrollFrameRef.current = null;
|
|
674
|
+
}
|
|
675
|
+
};
|
|
676
|
+
}, [contentMounted, draft.activeLevel, scheduleActiveOptionScroll, visible]);
|
|
677
|
+
|
|
678
|
+
const emitDraftChange = React.useCallback(
|
|
679
|
+
(nextDraft: AddressDraft) => {
|
|
680
|
+
const label = buildLabel(nextDraft.labels, separator);
|
|
681
|
+
onChange?.({
|
|
682
|
+
value: nextDraft.value,
|
|
683
|
+
values: nextDraft.value,
|
|
684
|
+
label,
|
|
685
|
+
labels: nextDraft.labels,
|
|
686
|
+
items: nextDraft.items,
|
|
687
|
+
});
|
|
688
|
+
},
|
|
689
|
+
[onChange, separator]
|
|
690
|
+
);
|
|
691
|
+
|
|
692
|
+
const handleLevelPress = React.useCallback(
|
|
693
|
+
(level: number) => {
|
|
694
|
+
if (disabled) return;
|
|
695
|
+
const prevDraft = draftRef.current;
|
|
696
|
+
if (level !== 0 && !prevDraft.items[level - 1]) return;
|
|
697
|
+
if (prevDraft.activeLevel === level) return;
|
|
698
|
+
|
|
699
|
+
const nextDraft = { ...prevDraft, activeLevel: level };
|
|
700
|
+
startListTransition(prevDraft.activeLevel, nextDraft.activeLevel);
|
|
701
|
+
draftRef.current = nextDraft;
|
|
702
|
+
setDraft(nextDraft);
|
|
703
|
+
},
|
|
704
|
+
[disabled, startListTransition]
|
|
705
|
+
);
|
|
706
|
+
|
|
707
|
+
const handleItemPress = React.useCallback(
|
|
708
|
+
(item: AddressCascaderOption, index: number, level: number) => {
|
|
709
|
+
if (disabled || item.disabled) return;
|
|
710
|
+
const prevDraft = draftRef.current;
|
|
711
|
+
if (level !== prevDraft.activeLevel) return;
|
|
712
|
+
|
|
713
|
+
const nextItems = prevDraft.items.slice(0, level);
|
|
714
|
+
const nextLabels = prevDraft.labels.slice(0, level);
|
|
715
|
+
const nextValue = prevDraft.value.slice(0, level);
|
|
716
|
+
|
|
717
|
+
nextItems[level] = item;
|
|
718
|
+
nextLabels[level] = pickText(item);
|
|
719
|
+
nextValue[level] = pickValue(item, index);
|
|
720
|
+
|
|
721
|
+
const nextDraft: AddressDraft = {
|
|
722
|
+
value: nextValue,
|
|
723
|
+
labels: nextLabels,
|
|
724
|
+
items: nextItems,
|
|
725
|
+
activeLevel: hasNextLevel(item, level) ? level + 1 : level,
|
|
726
|
+
};
|
|
727
|
+
|
|
728
|
+
if (prevDraft.activeLevel !== nextDraft.activeLevel) {
|
|
729
|
+
startListTransition(prevDraft.activeLevel, nextDraft.activeLevel);
|
|
730
|
+
}
|
|
731
|
+
draftRef.current = nextDraft;
|
|
732
|
+
setDraft(nextDraft);
|
|
733
|
+
emitDraftChange(nextDraft);
|
|
734
|
+
},
|
|
735
|
+
[disabled, emitDraftChange, startListTransition]
|
|
736
|
+
);
|
|
737
|
+
|
|
738
|
+
const handleCancel = React.useCallback(() => {
|
|
739
|
+
onCancel?.();
|
|
740
|
+
close();
|
|
741
|
+
}, [close, onCancel]);
|
|
742
|
+
|
|
743
|
+
const handleConfirm = React.useCallback(() => {
|
|
744
|
+
if (confirmDisabled) return;
|
|
745
|
+
const label = buildLabel(draft.labels, separator);
|
|
746
|
+
const payload: AddressCascaderConfirmPayload = {
|
|
747
|
+
value: draft.value,
|
|
748
|
+
values: draft.value,
|
|
749
|
+
label,
|
|
750
|
+
labels: draft.labels,
|
|
751
|
+
items: draft.items,
|
|
752
|
+
};
|
|
753
|
+
onLabelChange?.(label);
|
|
754
|
+
if (labelProp === undefined) setInnerLabel(label);
|
|
755
|
+
onValueChange?.(draft.value);
|
|
756
|
+
if (!isValueControlled) setInnerValue(draft.value);
|
|
757
|
+
onConfirm?.(payload);
|
|
758
|
+
close();
|
|
759
|
+
}, [
|
|
760
|
+
close,
|
|
761
|
+
confirmDisabled,
|
|
762
|
+
draft.items,
|
|
763
|
+
draft.labels,
|
|
764
|
+
draft.value,
|
|
765
|
+
isValueControlled,
|
|
766
|
+
labelProp,
|
|
767
|
+
onConfirm,
|
|
768
|
+
onLabelChange,
|
|
769
|
+
onValueChange,
|
|
770
|
+
separator,
|
|
771
|
+
]);
|
|
772
|
+
|
|
773
|
+
const handleSheetOpenChange = React.useCallback((nextOpen: boolean, meta: SheetOpenChangeDetails) => {
|
|
774
|
+
if (!nextOpen && visible && meta.reason !== 'api') {
|
|
775
|
+
onCancel?.();
|
|
776
|
+
}
|
|
777
|
+
setCascaderOpen(nextOpen);
|
|
778
|
+
}, [onCancel, setCascaderOpen, visible]);
|
|
779
|
+
|
|
780
|
+
const handleSheetDismissComplete = React.useCallback(() => {
|
|
781
|
+
if (lazyContent) setContentMounted(false);
|
|
782
|
+
onDismissComplete?.();
|
|
783
|
+
}, [lazyContent, onDismissComplete]);
|
|
784
|
+
|
|
785
|
+
const triggerNode = React.useMemo(
|
|
786
|
+
() => (children != null ? composeTrigger(children, openPicker, disabled, effectiveCtx) : null),
|
|
787
|
+
[children, disabled, effectiveCtx, openPicker]
|
|
788
|
+
);
|
|
789
|
+
|
|
790
|
+
const renderStep = React.useCallback(
|
|
791
|
+
(level: number) => {
|
|
792
|
+
const selected = draft.labels[level];
|
|
793
|
+
const isActive = draft.activeLevel === level;
|
|
794
|
+
const enabled = level === 0 || !!draft.items[level - 1];
|
|
795
|
+
return (
|
|
796
|
+
<React.Fragment key={`step-${level}`}>
|
|
797
|
+
{level > 0 ? <Feather name="chevron-right" size={wp(14)} color={theme.colors.muted} /> : null}
|
|
798
|
+
<Pressable
|
|
799
|
+
accessibilityRole="button"
|
|
800
|
+
disabled={!enabled || disabled}
|
|
801
|
+
onPress={() => handleLevelPress(level)}
|
|
802
|
+
style={({ pressed }) => [
|
|
803
|
+
styles.stepPill,
|
|
804
|
+
{
|
|
805
|
+
backgroundColor: isActive ? theme.colors.secondary : theme.colors.surface,
|
|
806
|
+
borderColor: isActive ? theme.colors.secondary : theme.colors.border,
|
|
807
|
+
opacity: enabled ? (pressed ? 0.78 : 1) : 0.45,
|
|
808
|
+
},
|
|
809
|
+
]}
|
|
810
|
+
>
|
|
811
|
+
<Text
|
|
812
|
+
numberOfLines={1}
|
|
813
|
+
style={[
|
|
814
|
+
styles.stepText,
|
|
815
|
+
{ color: isActive || selected ? theme.colors.onSurface : theme.colors.muted },
|
|
816
|
+
]}
|
|
817
|
+
>
|
|
818
|
+
{selected || resolvedLevelLabels[level]}
|
|
819
|
+
</Text>
|
|
820
|
+
</Pressable>
|
|
821
|
+
</React.Fragment>
|
|
822
|
+
);
|
|
823
|
+
},
|
|
824
|
+
[
|
|
825
|
+
disabled,
|
|
826
|
+
draft.activeLevel,
|
|
827
|
+
draft.items,
|
|
828
|
+
draft.labels,
|
|
829
|
+
handleLevelPress,
|
|
830
|
+
resolvedLevelLabels,
|
|
831
|
+
theme.colors.border,
|
|
832
|
+
theme.colors.muted,
|
|
833
|
+
theme.colors.onSurface,
|
|
834
|
+
theme.colors.secondary,
|
|
835
|
+
theme.colors.surface,
|
|
836
|
+
]
|
|
837
|
+
);
|
|
838
|
+
|
|
839
|
+
const handleOptionLayout = React.useCallback(
|
|
840
|
+
(key: string, event: LayoutChangeEvent) => {
|
|
841
|
+
optionLayoutYRef.current.set(key, event.nativeEvent.layout.y);
|
|
842
|
+
scheduleActiveOptionScroll(false);
|
|
843
|
+
},
|
|
844
|
+
[scheduleActiveOptionScroll]
|
|
845
|
+
);
|
|
846
|
+
|
|
847
|
+
const handleOptionsContentSizeChange = React.useCallback(() => {
|
|
848
|
+
scheduleActiveOptionScroll(false);
|
|
849
|
+
}, [scheduleActiveOptionScroll]);
|
|
850
|
+
|
|
851
|
+
const handleOptionsViewportLayout = React.useCallback(
|
|
852
|
+
(event: LayoutChangeEvent) => {
|
|
853
|
+
optionsViewportHeightRef.current = event.nativeEvent.layout.height;
|
|
854
|
+
scheduleActiveOptionScroll(false);
|
|
855
|
+
},
|
|
856
|
+
[scheduleActiveOptionScroll]
|
|
857
|
+
);
|
|
858
|
+
|
|
859
|
+
const renderOption = React.useCallback(
|
|
860
|
+
(item: AddressOption) => {
|
|
861
|
+
const key = keyExtractor(item);
|
|
862
|
+
const selected = item.value === activeSelectedValue;
|
|
863
|
+
return (
|
|
864
|
+
<AddressOptionRow
|
|
865
|
+
option={item}
|
|
866
|
+
selected={selected}
|
|
867
|
+
disabled={disabled}
|
|
868
|
+
primaryColor={theme.colors.primary}
|
|
869
|
+
onSurfaceColor={theme.colors.onSurface}
|
|
870
|
+
mutedColor={theme.colors.muted}
|
|
871
|
+
onPress={handleItemPress}
|
|
872
|
+
onLayout={selected ? (event) => handleOptionLayout(key, event) : undefined}
|
|
873
|
+
/>
|
|
874
|
+
);
|
|
875
|
+
},
|
|
876
|
+
[
|
|
877
|
+
activeSelectedValue,
|
|
878
|
+
disabled,
|
|
879
|
+
handleOptionLayout,
|
|
880
|
+
handleItemPress,
|
|
881
|
+
keyExtractor,
|
|
882
|
+
theme.colors.muted,
|
|
883
|
+
theme.colors.onSurface,
|
|
884
|
+
theme.colors.primary,
|
|
885
|
+
]
|
|
886
|
+
);
|
|
887
|
+
|
|
888
|
+
const renderSeparator = React.useCallback(
|
|
889
|
+
() => <View style={[styles.separatorLine, { backgroundColor: theme.colors.border }]} />,
|
|
890
|
+
[theme.colors.border]
|
|
891
|
+
);
|
|
892
|
+
|
|
893
|
+
const sheetNode = (
|
|
894
|
+
<Sheet
|
|
895
|
+
placement="bottom"
|
|
896
|
+
open={visible}
|
|
897
|
+
onOpenChange={handleSheetOpenChange}
|
|
898
|
+
onCloseComplete={handleSheetDismissComplete}
|
|
899
|
+
detents={detents}
|
|
900
|
+
backgroundColor={theme.colors.surface}
|
|
901
|
+
handle={false}
|
|
902
|
+
draggable={false}
|
|
903
|
+
dismissible={!disabled}
|
|
904
|
+
nativeProps={{ insetAdjustment: 'never' }}
|
|
905
|
+
>
|
|
906
|
+
<View
|
|
907
|
+
style={[
|
|
908
|
+
styles.sheetInner,
|
|
909
|
+
{
|
|
910
|
+
backgroundColor: theme.colors.surface,
|
|
911
|
+
height: sheetHeight,
|
|
912
|
+
paddingBottom: safeBottom,
|
|
913
|
+
},
|
|
914
|
+
]}
|
|
915
|
+
pointerEvents={disabled ? 'none' : 'auto'}
|
|
916
|
+
>
|
|
917
|
+
<View style={styles.header}>
|
|
918
|
+
<Text style={[styles.headerTitle, { color: theme.colors.onSurface }]} numberOfLines={1}>
|
|
919
|
+
{title ?? t('addressCascader.title')}
|
|
920
|
+
</Text>
|
|
921
|
+
</View>
|
|
922
|
+
|
|
923
|
+
{contentMounted ? (
|
|
924
|
+
<>
|
|
925
|
+
<ScrollView
|
|
926
|
+
ref={stepsScrollRef}
|
|
927
|
+
horizontal
|
|
928
|
+
showsHorizontalScrollIndicator={false}
|
|
929
|
+
contentContainerStyle={styles.stepsContent}
|
|
930
|
+
style={styles.stepsScroll}
|
|
931
|
+
>
|
|
932
|
+
{LEVELS.map(renderStep)}
|
|
933
|
+
</ScrollView>
|
|
934
|
+
|
|
935
|
+
<Animated.View style={[styles.listFrame, { borderColor: theme.colors.border }, listAnimatedStyle]}>
|
|
936
|
+
{currentOptions.length > 0 ? (
|
|
937
|
+
<ScrollView
|
|
938
|
+
ref={optionsScrollRef}
|
|
939
|
+
contentContainerStyle={styles.listContent}
|
|
940
|
+
keyboardShouldPersistTaps="handled"
|
|
941
|
+
nestedScrollEnabled
|
|
942
|
+
onContentSizeChange={handleOptionsContentSizeChange}
|
|
943
|
+
onLayout={handleOptionsViewportLayout}
|
|
944
|
+
showsVerticalScrollIndicator={false}
|
|
945
|
+
style={styles.optionsScroll}
|
|
946
|
+
>
|
|
947
|
+
{currentOptions.map((option, index) => {
|
|
948
|
+
const key = keyExtractor(option);
|
|
949
|
+
return (
|
|
950
|
+
<React.Fragment key={key}>
|
|
951
|
+
{renderOption(option)}
|
|
952
|
+
{index < currentOptions.length - 1 ? renderSeparator() : null}
|
|
953
|
+
</React.Fragment>
|
|
954
|
+
);
|
|
955
|
+
})}
|
|
956
|
+
</ScrollView>
|
|
957
|
+
) : (
|
|
958
|
+
<View style={styles.emptyState}>
|
|
959
|
+
<Text style={[styles.emptyText, { color: theme.colors.muted }]}>{t('addressCascader.empty')}</Text>
|
|
960
|
+
</View>
|
|
961
|
+
)}
|
|
962
|
+
</Animated.View>
|
|
963
|
+
</>
|
|
964
|
+
) : (
|
|
965
|
+
<View style={styles.lazyPlaceholder} />
|
|
966
|
+
)}
|
|
967
|
+
|
|
968
|
+
<PickerActionBar
|
|
969
|
+
cancelText={t('picker.cancel')}
|
|
970
|
+
confirmText={t('picker.confirm')}
|
|
971
|
+
onCancel={handleCancel}
|
|
972
|
+
onConfirm={handleConfirm}
|
|
973
|
+
disabled={disabled}
|
|
974
|
+
confirmDisabled={confirmDisabled}
|
|
975
|
+
style={styles.footer}
|
|
976
|
+
/>
|
|
977
|
+
</View>
|
|
978
|
+
</Sheet>
|
|
979
|
+
);
|
|
980
|
+
|
|
981
|
+
return (
|
|
982
|
+
<>
|
|
983
|
+
{triggerNode}
|
|
984
|
+
{sheetNode}
|
|
985
|
+
</>
|
|
986
|
+
);
|
|
987
|
+
});
|
|
988
|
+
|
|
989
|
+
const styles = StyleSheet.create({
|
|
990
|
+
sheetInner: {
|
|
991
|
+
paddingHorizontal: wp(16),
|
|
992
|
+
paddingTop: wp(12),
|
|
993
|
+
width: '100%',
|
|
994
|
+
},
|
|
995
|
+
header: {
|
|
996
|
+
alignItems: 'center',
|
|
997
|
+
height: wp(34),
|
|
998
|
+
justifyContent: 'center',
|
|
999
|
+
},
|
|
1000
|
+
headerTitle: {
|
|
1001
|
+
fontSize: sp(16),
|
|
1002
|
+
fontWeight: '600',
|
|
1003
|
+
},
|
|
1004
|
+
stepsScroll: {
|
|
1005
|
+
alignSelf: 'stretch',
|
|
1006
|
+
flexGrow: 0,
|
|
1007
|
+
marginTop: wp(10),
|
|
1008
|
+
maxHeight: wp(40),
|
|
1009
|
+
width: '100%',
|
|
1010
|
+
},
|
|
1011
|
+
stepsContent: {
|
|
1012
|
+
alignItems: 'center',
|
|
1013
|
+
flexGrow: 1,
|
|
1014
|
+
gap: wp(8),
|
|
1015
|
+
justifyContent: 'center',
|
|
1016
|
+
paddingHorizontal: wp(4),
|
|
1017
|
+
},
|
|
1018
|
+
stepPill: {
|
|
1019
|
+
alignItems: 'center',
|
|
1020
|
+
borderRadius: wp(8),
|
|
1021
|
+
borderWidth: wp(1),
|
|
1022
|
+
justifyContent: 'center',
|
|
1023
|
+
maxWidth: wp(178),
|
|
1024
|
+
minHeight: wp(34),
|
|
1025
|
+
minWidth: wp(76),
|
|
1026
|
+
paddingHorizontal: wp(11),
|
|
1027
|
+
},
|
|
1028
|
+
stepText: {
|
|
1029
|
+
fontSize: sp(14),
|
|
1030
|
+
fontWeight: '700',
|
|
1031
|
+
},
|
|
1032
|
+
listFrame: {
|
|
1033
|
+
borderTopWidth: StyleSheet.hairlineWidth,
|
|
1034
|
+
flex: 1,
|
|
1035
|
+
marginTop: wp(12),
|
|
1036
|
+
overflow: 'hidden',
|
|
1037
|
+
},
|
|
1038
|
+
optionsScroll: {
|
|
1039
|
+
flex: 1,
|
|
1040
|
+
},
|
|
1041
|
+
listContent: {
|
|
1042
|
+
paddingBottom: wp(6),
|
|
1043
|
+
},
|
|
1044
|
+
optionRow: {
|
|
1045
|
+
alignItems: 'center',
|
|
1046
|
+
flexDirection: 'row',
|
|
1047
|
+
minHeight: wp(56),
|
|
1048
|
+
paddingHorizontal: wp(10),
|
|
1049
|
+
paddingVertical: wp(7),
|
|
1050
|
+
},
|
|
1051
|
+
optionMain: {
|
|
1052
|
+
flex: 1,
|
|
1053
|
+
minWidth: 0,
|
|
1054
|
+
paddingRight: wp(12),
|
|
1055
|
+
},
|
|
1056
|
+
optionText: {
|
|
1057
|
+
fontSize: sp(16),
|
|
1058
|
+
lineHeight: sp(22),
|
|
1059
|
+
},
|
|
1060
|
+
optionMark: {
|
|
1061
|
+
alignItems: 'center',
|
|
1062
|
+
justifyContent: 'center',
|
|
1063
|
+
width: wp(28),
|
|
1064
|
+
},
|
|
1065
|
+
separatorLine: {
|
|
1066
|
+
height: StyleSheet.hairlineWidth,
|
|
1067
|
+
},
|
|
1068
|
+
emptyState: {
|
|
1069
|
+
alignItems: 'center',
|
|
1070
|
+
flex: 1,
|
|
1071
|
+
justifyContent: 'center',
|
|
1072
|
+
minHeight: wp(180),
|
|
1073
|
+
},
|
|
1074
|
+
emptyText: {
|
|
1075
|
+
fontSize: sp(14),
|
|
1076
|
+
fontWeight: '600',
|
|
1077
|
+
},
|
|
1078
|
+
lazyPlaceholder: {
|
|
1079
|
+
flex: 1,
|
|
1080
|
+
},
|
|
1081
|
+
footer: {
|
|
1082
|
+
paddingTop: wp(12),
|
|
1083
|
+
},
|
|
1084
|
+
});
|