@react-native-oh/react-native-harmony 0.61.16
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/Libraries/ART/ReactNativeART.js +43 -0
- package/Libraries/Animated/Animated.js +154 -0
- package/Libraries/Animated/delegates/createAnimatedComponentDelegate.js +20 -0
- package/Libraries/Animated/src/Easing.js +20 -0
- package/Libraries/Components/AccessibilityInfo/AccessibilityInfo.js +15 -0
- package/Libraries/Components/AppleTV/TVEventHandler.js +13 -0
- package/Libraries/Components/CheckBox/CheckBox.tsx +182 -0
- package/Libraries/Components/DatePickerAndroid/DatePickerAndroid.tsx +32 -0
- package/Libraries/Components/DatePickerIOS/DatePickerIOS.tsx +285 -0
- package/Libraries/Components/MaskedView/MaskedView.tsx +32 -0
- package/Libraries/Components/Picker/Picker.tsx +303 -0
- package/Libraries/Components/PickerIOS/PickerIOS.tsx +175 -0
- package/Libraries/Components/ProgressBarAndroid/ProgressBarAndroid.tsx +156 -0
- package/Libraries/Components/ProgressViewIOS/ProgressViewIOS.tsx +65 -0
- package/Libraries/Components/PushNotificationIOS/PushNotificationIOS.tsx +609 -0
- package/Libraries/Components/RefreshControl/RefreshControl.tsx +54 -0
- package/Libraries/Components/ScrollView/ScrollView.js +34 -0
- package/Libraries/Components/SegmentedControlIOS/SegmentedControlIOS.tsx +79 -0
- package/Libraries/Components/Slider/Slider.tsx +204 -0
- package/Libraries/Components/TextInput/TextInput.js +24 -0
- package/Libraries/Components/TimePickerAndroid/TimePickerAndroid.tsx +20 -0
- package/Libraries/Components/Touchable/TouchableOpacity.js +90 -0
- package/Libraries/Components/View/ViewPropTypes.js +16 -0
- package/Libraries/Components/View/delegate/ViewDelegate.js +49 -0
- package/Libraries/Core/ReactNativeVersion.js +21 -0
- package/Libraries/Core/polyfillPromise.js +18 -0
- package/Libraries/Core/setUpPlatform.js +13 -0
- package/Libraries/DeprecatedPropTypes/DeprecatedColorPropType.js +16 -0
- package/Libraries/DeprecatedPropTypes/DeprecatedEdgeInsetsPropType.js +16 -0
- package/Libraries/DeprecatedPropTypes/DeprecatedImagePropType.js +15 -0
- package/Libraries/DeprecatedPropTypes/DeprecatedImageSourcePropType.js +16 -0
- package/Libraries/DeprecatedPropTypes/DeprecatedImageStylePropTypes.js +15 -0
- package/Libraries/DeprecatedPropTypes/DeprecatedLayoutPropTypes.js +15 -0
- package/Libraries/DeprecatedPropTypes/DeprecatedPointPropType.js +16 -0
- package/Libraries/DeprecatedPropTypes/DeprecatedShadowPropTypesIOS.js +15 -0
- package/Libraries/DeprecatedPropTypes/DeprecatedStyleSheetPropType.js +16 -0
- package/Libraries/DeprecatedPropTypes/DeprecatedTVViewPropTypes.js +15 -0
- package/Libraries/DeprecatedPropTypes/DeprecatedTextInputPropTypes.js +15 -0
- package/Libraries/DeprecatedPropTypes/DeprecatedTextPropTypes.js +16 -0
- package/Libraries/DeprecatedPropTypes/DeprecatedTextStylePropTypes.js +15 -0
- package/Libraries/DeprecatedPropTypes/DeprecatedTransformPropTypes.js +15 -0
- package/Libraries/DeprecatedPropTypes/DeprecatedViewAccessibility.js +15 -0
- package/Libraries/DeprecatedPropTypes/DeprecatedViewPropTypes.js +16 -0
- package/Libraries/DeprecatedPropTypes/DeprecatedViewStylePropTypes.js +15 -0
- package/Libraries/DeprecatedPropTypes/deprecatedCreateStrictShapeTypeChecker.js +20 -0
- package/Libraries/EventEmitter/NativeEventEmitter.js +50 -0
- package/Libraries/Image/Image.tsx +88 -0
- package/Libraries/Promise.js +50 -0
- package/Libraries/Storage/AsyncStorage.ts +216 -0
- package/Libraries/Storage/helpers.ts +36 -0
- package/Libraries/Storage/hooks.ts +19 -0
- package/Libraries/Storage/index.ts +5 -0
- package/Libraries/Storage/types.ts +43 -0
- package/Libraries/StyleSheet/EdgeInsetsPropType.js +15 -0
- package/Libraries/StyleSheet/PointPropType.js +15 -0
- package/Libraries/Utilities/Platform.harmony.ts +68 -0
- package/Libraries/Utilities/setAndForwardRef.js +71 -0
- package/README.dev.md +55 -0
- package/README.md +146 -0
- package/compat/ensurePropTypes.js +45 -0
- package/compat/getNoopPropType.js +42 -0
- package/compat/restoreRemoveListener.js +117 -0
- package/index.js +348 -0
- package/package.json +45 -0
- package/patched-virtualized-list/VirtualizedList.js +87 -0
- package/polyfills/dateSlash.js +89 -0
- package/react_native_harmony.har +0 -0
- package/src/private/specs/AsyncStorage.ts +37 -0
- package/src/private/specs/NativeDatePickerAndroid.ts +27 -0
- package/src/private/specs/NativeTimePickerAndroid.ts +28 -0
- package/src/private/specs/PushNotificationIOS.ts +167 -0
- package/src/private/specs/components/CheckBoxNativeComponent.ts +35 -0
- package/src/private/specs/components/DatePickerNativeComponent.ts +40 -0
- package/src/private/specs/components/MaskedViewNativeComponent.ts +17 -0
- package/src/private/specs/components/PickerIOSNativeComponent.ts +53 -0
- package/src/private/specs/components/PickerNativeComponent.ts +67 -0
- package/src/private/specs/components/ProgressBarNativeComponent.ts +24 -0
- package/src/private/specs/components/ProgressViewNativeComponent.ts +24 -0
- package/src/private/specs/components/ProgressWheelNativeComponent.ts +18 -0
- package/src/private/specs/components/RNSliderNativeComponent.ts +44 -0
- package/src/private/specs/components/SegmentedControlNativeComponent.ts +38 -0
- package/src/private/specs/components/TimePickerNativeComponent.ts +40 -0
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Huawei Technologies Co., Ltd.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Implements the DatePickerIOS component which is removed from newer RN releases.
|
|
10
|
+
* Main functionality is delegated to native components:
|
|
11
|
+
* - [TimePicker](https://developer.huawei.com/consumer/en/doc/harmonyos-references/ts-basic-components-timepicker)
|
|
12
|
+
* - [DatePicker](https://developer.huawei.com/consumer/en/doc/harmonyos-references/ts-basic-components-datepicker)
|
|
13
|
+
*
|
|
14
|
+
* There is no direct equivalent for mode='datetime', so we construct one by rendering the DatePicker and TimePicker side-by-side.
|
|
15
|
+
* A few properties are not supported due to the above native components' limitations, and overall platform limitations.
|
|
16
|
+
*
|
|
17
|
+
* These include:
|
|
18
|
+
*
|
|
19
|
+
* - [minuteInterval](https://reactnative-archive-august-2023.netlify.app/docs/0.61/datepickerios#minuteinterval)
|
|
20
|
+
* TimePicker provides no direct way to set the minute interval. A hand-rolled component can support this easily,
|
|
21
|
+
* but it would take too much effort to have it provide all the other functionalities already present in TimePicker, like localization.
|
|
22
|
+
*
|
|
23
|
+
* - [locale](https://reactnative-archive-august-2023.netlify.app/docs/0.61/datepickerios#locale)
|
|
24
|
+
* Expanding on the previous point, TimePicker uses the system locale and does not allow overriding it.
|
|
25
|
+
* Supporting multiple locales reliably for any component takes considerable effort, and requires active maintenance.
|
|
26
|
+
* Creating a custom TimePicker with this in mind does not seem worthwile.
|
|
27
|
+
*
|
|
28
|
+
* - [timeZoneOffsetInMinutes](https://reactnative-archive-august-2023.netlify.app/docs/0.61/datepickerios#timezoneoffsetinminutes)
|
|
29
|
+
* This ties into mentioned localization issues. Standard JS Date APIs do not provide any direct timezone support,
|
|
30
|
+
* and the [Intl.DateTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat)
|
|
31
|
+
* for HarmonyOS are not stable yet (see e.g. Date..toLocaleDateString() returning 'dateFormat not implemented'. This is squarely outside the scope of implementing DatePickerIOS).
|
|
32
|
+
* Any naive implementation of timezone offsets like adding/subtracting minutes is too brittle to be considered.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import RNDatePickerNative from '../../../src/private/specs/components/DatePickerNativeComponent';
|
|
36
|
+
import RNTimePickerNative from '../../../src/private/specs/components/TimePickerNativeComponent';
|
|
37
|
+
import type {NativeProps as NativePickerProps} from '../../../src/private/specs/components/DatePickerNativeComponent';
|
|
38
|
+
import {forwardRef, useState} from 'react';
|
|
39
|
+
import {ViewProps, NativeMethods, View} from 'react-native';
|
|
40
|
+
import nullthrows from 'nullthrows';
|
|
41
|
+
import {NativeSyntheticEvent} from '@react-native-oh/react-native-harmony';
|
|
42
|
+
|
|
43
|
+
// #region types
|
|
44
|
+
// For developer convenience
|
|
45
|
+
|
|
46
|
+
// NOTE[RNOH]: Removed SyntheticEvent wrapper
|
|
47
|
+
type Event = Readonly<{
|
|
48
|
+
timestamp: number;
|
|
49
|
+
}>;
|
|
50
|
+
|
|
51
|
+
type DateTimePickerEvent = NativeSyntheticEvent<
|
|
52
|
+
Readonly<{
|
|
53
|
+
timestamp: number;
|
|
54
|
+
}>
|
|
55
|
+
>;
|
|
56
|
+
|
|
57
|
+
type Props = Readonly<
|
|
58
|
+
ViewProps & {
|
|
59
|
+
/**
|
|
60
|
+
* The currently selected date.
|
|
61
|
+
*/
|
|
62
|
+
date?: Date;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Provides an initial value that will change when the user starts selecting
|
|
66
|
+
* a date. It is useful for simple use-cases where you do not want to deal
|
|
67
|
+
* with listening to events and updating the date prop to keep the
|
|
68
|
+
* controlled state in sync. The controlled state has known bugs which
|
|
69
|
+
* causes it to go out of sync with native. The initialDate prop is intended
|
|
70
|
+
* to allow you to have native be source of truth.
|
|
71
|
+
*/
|
|
72
|
+
initialDate?: Date;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The date picker locale.
|
|
76
|
+
*/
|
|
77
|
+
locale?: string;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Maximum date.
|
|
81
|
+
*
|
|
82
|
+
* Restricts the range of possible date/time values.
|
|
83
|
+
*/
|
|
84
|
+
maximumDate?: Date;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Minimum date.
|
|
88
|
+
*
|
|
89
|
+
* Restricts the range of possible date/time values.
|
|
90
|
+
*/
|
|
91
|
+
minimumDate?: Date;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The interval at which minutes can be selected.
|
|
95
|
+
*/
|
|
96
|
+
minuteInterval?: 1 | 2 | 3 | 4 | 5 | 6 | 10 | 12 | 15 | 20 | 30;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The date picker mode.
|
|
100
|
+
*/
|
|
101
|
+
mode?: 'date' | 'time' | 'datetime';
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Date change handler.
|
|
105
|
+
*
|
|
106
|
+
* This is called when the user changes the date or time in the UI.
|
|
107
|
+
* The first and only argument is an Event. For getting the date the picker
|
|
108
|
+
* was changed to, use onDateChange instead.
|
|
109
|
+
*/
|
|
110
|
+
onChange?: (event: Event) => void;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Date change handler.
|
|
114
|
+
*
|
|
115
|
+
* This is called when the user changes the date or time in the UI.
|
|
116
|
+
* The first and only argument is a Date object representing the new
|
|
117
|
+
* date and time.
|
|
118
|
+
*/
|
|
119
|
+
onDateChange: (date: Date) => void;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Timezone offset in minutes.
|
|
123
|
+
*
|
|
124
|
+
* By default, the date picker will use the device's timezone. With this
|
|
125
|
+
* parameter, it is possible to force a certain timezone offset. For
|
|
126
|
+
* instance, to show times in Pacific Standard Time, pass -7 * 60.
|
|
127
|
+
*/
|
|
128
|
+
timeZoneOffsetInMinutes?: number;
|
|
129
|
+
}
|
|
130
|
+
>;
|
|
131
|
+
|
|
132
|
+
// #endregion
|
|
133
|
+
|
|
134
|
+
function dateToMilliseconds(date?: Date): number | undefined {
|
|
135
|
+
return date?.getTime();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const DateTimePickerIOS = forwardRef(function DateTimePickerIOS(
|
|
139
|
+
props: NativePickerProps,
|
|
140
|
+
ref: React.Ref<NativeMethods>,
|
|
141
|
+
) {
|
|
142
|
+
const {onChange, date, style, ...nativeProps} = props;
|
|
143
|
+
const [selectedDate, setSelectedDate] = useState(date);
|
|
144
|
+
|
|
145
|
+
// The two pickers are very likely to go out of sync,
|
|
146
|
+
// so when gluing them together we have to make
|
|
147
|
+
// sure the signaled date is synced manually
|
|
148
|
+
const onDateChange = (event: DateTimePickerEvent) => {
|
|
149
|
+
const timestamp = event.nativeEvent.timestamp;
|
|
150
|
+
const pickerDate = new Date(timestamp);
|
|
151
|
+
|
|
152
|
+
const oldDate = new Date(selectedDate);
|
|
153
|
+
const newDate = new Date(
|
|
154
|
+
pickerDate.getFullYear(),
|
|
155
|
+
pickerDate.getMonth(),
|
|
156
|
+
pickerDate.getDay(),
|
|
157
|
+
oldDate.getHours(),
|
|
158
|
+
oldDate.getMinutes(),
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
const correctedTimestampEvent: DateTimePickerEvent = {
|
|
162
|
+
nativeEvent: {
|
|
163
|
+
timestamp: newDate.getTime(),
|
|
164
|
+
...event.nativeEvent,
|
|
165
|
+
},
|
|
166
|
+
...event,
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
setSelectedDate(newDate.getTime());
|
|
170
|
+
onChange && onChange(correctedTimestampEvent);
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const onTimeChange = (event: DateTimePickerEvent) => {
|
|
174
|
+
const timestamp = event.nativeEvent.timestamp;
|
|
175
|
+
const pickerDate = new Date(timestamp);
|
|
176
|
+
|
|
177
|
+
const oldDate = new Date(selectedDate);
|
|
178
|
+
const newDate = new Date(
|
|
179
|
+
oldDate.getFullYear(),
|
|
180
|
+
oldDate.getMonth(),
|
|
181
|
+
oldDate.getDay(),
|
|
182
|
+
pickerDate.getHours(),
|
|
183
|
+
pickerDate.getMinutes(),
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
const correctedTimestampEvent: DateTimePickerEvent = {
|
|
187
|
+
nativeEvent: {
|
|
188
|
+
timestamp: newDate.getTime(),
|
|
189
|
+
...event.nativeEvent,
|
|
190
|
+
},
|
|
191
|
+
...event,
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
setSelectedDate(newDate.getTime());
|
|
195
|
+
onChange && onChange(correctedTimestampEvent);
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
return (
|
|
199
|
+
<View style={{flexDirection: 'row', ...(style as object)}}>
|
|
200
|
+
<RNDatePickerNative
|
|
201
|
+
date={selectedDate}
|
|
202
|
+
ref={ref as any}
|
|
203
|
+
style={{flex: 1}}
|
|
204
|
+
onChange={onDateChange}
|
|
205
|
+
{...nativeProps}
|
|
206
|
+
/>
|
|
207
|
+
<RNTimePickerNative
|
|
208
|
+
date={selectedDate}
|
|
209
|
+
ref={ref as any}
|
|
210
|
+
style={{flex: 1}}
|
|
211
|
+
onChange={onTimeChange}
|
|
212
|
+
{...nativeProps}
|
|
213
|
+
/>
|
|
214
|
+
</View>
|
|
215
|
+
);
|
|
216
|
+
});
|
|
217
|
+
DateTimePickerIOS.displayName = 'DateTimePickerIOS';
|
|
218
|
+
|
|
219
|
+
const DatePickerIOS = forwardRef(
|
|
220
|
+
(props: Props, ref: React.Ref<NativeMethods>) => {
|
|
221
|
+
const {
|
|
222
|
+
date,
|
|
223
|
+
initialDate,
|
|
224
|
+
onChange,
|
|
225
|
+
onDateChange,
|
|
226
|
+
minimumDate,
|
|
227
|
+
maximumDate,
|
|
228
|
+
locale,
|
|
229
|
+
mode,
|
|
230
|
+
style,
|
|
231
|
+
...other
|
|
232
|
+
} = props;
|
|
233
|
+
|
|
234
|
+
const _date: Date = nullthrows(
|
|
235
|
+
date || initialDate,
|
|
236
|
+
'A selected date or initial date should be specified.',
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
const _onChange = (event: DateTimePickerEvent) => {
|
|
240
|
+
const timestamp = event.nativeEvent.timestamp;
|
|
241
|
+
const pickerDate = new Date(timestamp);
|
|
242
|
+
|
|
243
|
+
onDateChange && onDateChange(pickerDate);
|
|
244
|
+
onChange &&
|
|
245
|
+
onChange({
|
|
246
|
+
timestamp: timestamp,
|
|
247
|
+
});
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
const nativeProps = {
|
|
251
|
+
date: dateToMilliseconds(_date),
|
|
252
|
+
maximumDate: dateToMilliseconds(maximumDate),
|
|
253
|
+
minimumDate: dateToMilliseconds(minimumDate),
|
|
254
|
+
locale: locale !== null && locale !== '' ? locale : undefined,
|
|
255
|
+
onChange: _onChange,
|
|
256
|
+
style: {width: '100%', height: '100%'} as object,
|
|
257
|
+
ref: ref as any,
|
|
258
|
+
...other,
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
const picker = (() => {
|
|
262
|
+
switch (mode) {
|
|
263
|
+
case 'time':
|
|
264
|
+
return <RNTimePickerNative {...nativeProps} />;
|
|
265
|
+
case 'datetime':
|
|
266
|
+
return <DateTimePickerIOS {...nativeProps} />;
|
|
267
|
+
case 'date':
|
|
268
|
+
default:
|
|
269
|
+
return <RNDatePickerNative {...nativeProps} />;
|
|
270
|
+
}
|
|
271
|
+
})();
|
|
272
|
+
|
|
273
|
+
// We use the outer wrapping view for general view props and the inner one
|
|
274
|
+
// to enforce a default height for the date picker, same as the RN implementation:
|
|
275
|
+
// https://github.com/facebook/react-native/blob/v0.61.5/Libraries/Components/DatePicker/DatePickerIOS.ios.js#L187
|
|
276
|
+
return (
|
|
277
|
+
<View style={style}>
|
|
278
|
+
<View style={{height: 216, width: '100%'}}>{picker}</View>
|
|
279
|
+
</View>
|
|
280
|
+
);
|
|
281
|
+
},
|
|
282
|
+
);
|
|
283
|
+
DatePickerIOS.displayName = 'DatePickerIOS';
|
|
284
|
+
|
|
285
|
+
export default DatePickerIOS;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import React, { useRef } from "react";
|
|
2
|
+
import { View, StyleSheet } from "react-native";
|
|
3
|
+
import RNMaskedView from "../../../src/private/specs/components/MaskedViewNativeComponent";
|
|
4
|
+
|
|
5
|
+
export type MaskedViewProps = Partial<import("react-native").ViewProps> & {
|
|
6
|
+
children?: React.ReactNode;
|
|
7
|
+
maskElement: React.ReactNode;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export default function MaskedView(props: MaskedViewProps) {
|
|
11
|
+
const { maskElement, children, ...otherViewProps } = props;
|
|
12
|
+
const hasWarnedInvalidRenderMaskRef = useRef(false);
|
|
13
|
+
|
|
14
|
+
if (!React.isValidElement(maskElement)) {
|
|
15
|
+
if (!hasWarnedInvalidRenderMaskRef.current) {
|
|
16
|
+
console.warn(
|
|
17
|
+
"MaskedView: Invalid `maskElement` prop was passed to MaskedView. Expected a React Element. No mask will render."
|
|
18
|
+
);
|
|
19
|
+
hasWarnedInvalidRenderMaskRef.current = true;
|
|
20
|
+
}
|
|
21
|
+
return <View {...otherViewProps}>{children}</View>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return (
|
|
25
|
+
<RNMaskedView {...otherViewProps}>
|
|
26
|
+
<View pointerEvents="none" style={StyleSheet.absoluteFill}>
|
|
27
|
+
{maskElement}
|
|
28
|
+
</View>
|
|
29
|
+
{children}
|
|
30
|
+
</RNMaskedView>
|
|
31
|
+
);
|
|
32
|
+
}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import React from 'react';
|
|
10
|
+
import {
|
|
11
|
+
ColorValue,
|
|
12
|
+
StyleProp,
|
|
13
|
+
TextStyle,
|
|
14
|
+
StyleSheet,
|
|
15
|
+
processColor,
|
|
16
|
+
} from '@react-native-oh/react-native-harmony';
|
|
17
|
+
|
|
18
|
+
import PickerNativeComponent, {
|
|
19
|
+
NativePickerItem,
|
|
20
|
+
} from '../../../src/private/specs/components/PickerNativeComponent';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Default height:
|
|
24
|
+
* - OHOS: 40
|
|
25
|
+
* - Android: 50
|
|
26
|
+
*
|
|
27
|
+
* Android's default height is used on OHOS to avoid layout inconsistencies between platforms.
|
|
28
|
+
* However, on OHOS 40 looks better because rounding creates half of a circle. To eliminate this
|
|
29
|
+
* problem, the default background color was set to transparent,
|
|
30
|
+
* which is the default color also used on Android.
|
|
31
|
+
*
|
|
32
|
+
*/
|
|
33
|
+
const DEFAULT_HEIGHT = 50;
|
|
34
|
+
/**
|
|
35
|
+
* Default radius:
|
|
36
|
+
* - OHOS: 20
|
|
37
|
+
* - Android: 0
|
|
38
|
+
*
|
|
39
|
+
* OHOS uses rounding for Buttons and Picker Items.
|
|
40
|
+
*/
|
|
41
|
+
const DEFAULT_RADIUS = 20;
|
|
42
|
+
const OHOS_NATIVE_BUTTON_COLOR = 'rgb(10,85,236)';
|
|
43
|
+
const RNOH_BUTTON_COLOR_WITH_OPACITY_25 = 'rgba(49,122,255,0.25)';
|
|
44
|
+
|
|
45
|
+
const MODE_DIALOG = 'dialog' as const;
|
|
46
|
+
const MODE_DROPDOWN = 'dropdown' as const;
|
|
47
|
+
|
|
48
|
+
type PickerItemProps = Readonly<{
|
|
49
|
+
/**
|
|
50
|
+
* Text to display for this item.
|
|
51
|
+
*/
|
|
52
|
+
label: string;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The value to be passed to picker's `onValueChange` callback when
|
|
56
|
+
* this item is selected. Can be a string or an integer.
|
|
57
|
+
*/
|
|
58
|
+
value?: number | string;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Color of this item's text.
|
|
62
|
+
*/
|
|
63
|
+
color?: ColorValue;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Used to locate the item in end-to-end tests.
|
|
67
|
+
*/
|
|
68
|
+
testID?: string;
|
|
69
|
+
}>;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Individual selectable item in a Picker.
|
|
73
|
+
*/
|
|
74
|
+
class PickerItem extends React.Component<PickerItemProps> {
|
|
75
|
+
render() {
|
|
76
|
+
// The items are not rendered directly
|
|
77
|
+
throw null;
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
type PickerProps = Readonly<{
|
|
83
|
+
children?: React.ReactNode | React.ReactNode[];
|
|
84
|
+
style?: StyleProp<TextStyle>;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Value matching value of one of the items. Can be a string or an integer.
|
|
88
|
+
*/
|
|
89
|
+
selectedValue?: number | string;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Callback for when an item is selected. This is called with the following parameters:
|
|
93
|
+
* - `itemValue`: the `value` prop of the item that was selected
|
|
94
|
+
* - `itemIndex`: the index of the selected item in this picker
|
|
95
|
+
*/
|
|
96
|
+
onValueChange?: (itemValue: string | number, itemIndex: number) => void;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* If set to false, the picker will be disabled, i.e. the user will not be able to make a
|
|
100
|
+
* selection.
|
|
101
|
+
*/
|
|
102
|
+
enabled?: boolean;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* On Android, specifies how to display the selection items when the user taps on the picker:
|
|
106
|
+
*
|
|
107
|
+
* - 'dialog': Show a modal dialog. This is the default.
|
|
108
|
+
* - 'dropdown': Shows a dropdown anchored to the picker view
|
|
109
|
+
*/
|
|
110
|
+
mode?: 'dialog' | 'dropdown';
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Style to apply to each of the item labels.
|
|
114
|
+
*/
|
|
115
|
+
itemStyle?: StyleProp<TextStyle>;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Style to apply to each of the item labels. (OHOS only)
|
|
119
|
+
*/
|
|
120
|
+
selectedItemStyle?: StyleProp<TextStyle>;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Prompt string for this picker, used on Android in dialog mode as the title of the dialog.
|
|
124
|
+
*/
|
|
125
|
+
prompt?: string;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Used to locate this view in end-to-end tests.
|
|
129
|
+
*/
|
|
130
|
+
testID?: string;
|
|
131
|
+
}>;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Renders the native picker component on iOS and Android. Example:
|
|
135
|
+
*
|
|
136
|
+
* <Picker
|
|
137
|
+
* selectedValue={this.state.language}
|
|
138
|
+
* onValueChange={(itemValue, itemIndex) => this.setState({language: itemValue})}>
|
|
139
|
+
* <Picker.Item label="Java" value="java" />
|
|
140
|
+
* <Picker.Item label="JavaScript" value="js" />
|
|
141
|
+
* </Picker>
|
|
142
|
+
*/
|
|
143
|
+
export default class Picker extends React.Component<PickerProps> {
|
|
144
|
+
/**
|
|
145
|
+
* On Android, display the options in a dialog.
|
|
146
|
+
*/
|
|
147
|
+
static MODE_DIALOG = MODE_DIALOG;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* On Android, display the options in a dropdown (this is the default).
|
|
151
|
+
*/
|
|
152
|
+
static MODE_DROPDOWN = MODE_DROPDOWN;
|
|
153
|
+
|
|
154
|
+
static Item: typeof PickerItem = PickerItem;
|
|
155
|
+
|
|
156
|
+
static defaultProps = {
|
|
157
|
+
mode: MODE_DIALOG,
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
private getPickerItemsAsProp(): NativePickerItem[] {
|
|
161
|
+
return React.Children.toArray(this.props.children).flatMap((child) => {
|
|
162
|
+
if (
|
|
163
|
+
React.isValidElement(child) &&
|
|
164
|
+
typeof child.type === 'function' &&
|
|
165
|
+
child.type.name === PickerItem.name
|
|
166
|
+
) {
|
|
167
|
+
const p = child.props as PickerItemProps;
|
|
168
|
+
return [{ label: p.label, value: p.value } satisfies NativePickerItem];
|
|
169
|
+
}
|
|
170
|
+
return [];
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
override render() {
|
|
175
|
+
const {
|
|
176
|
+
selectedValue,
|
|
177
|
+
enabled,
|
|
178
|
+
mode,
|
|
179
|
+
prompt,
|
|
180
|
+
onValueChange,
|
|
181
|
+
style,
|
|
182
|
+
itemStyle,
|
|
183
|
+
selectedItemStyle,
|
|
184
|
+
testID
|
|
185
|
+
} = this.props;
|
|
186
|
+
|
|
187
|
+
const flatStyle = StyleSheet.flatten(style) ?? {};
|
|
188
|
+
const {
|
|
189
|
+
borderWidth,
|
|
190
|
+
borderBottomWidth,
|
|
191
|
+
borderTopWidth,
|
|
192
|
+
borderRightWidth,
|
|
193
|
+
borderLeftWidth,
|
|
194
|
+
borderStartWidth,
|
|
195
|
+
borderEndWidth,
|
|
196
|
+
borderColor,
|
|
197
|
+
borderRadius,
|
|
198
|
+
borderBottomLeftRadius,
|
|
199
|
+
borderBottomRightRadius,
|
|
200
|
+
borderTopLeftRadius,
|
|
201
|
+
borderTopRightRadius,
|
|
202
|
+
borderBottomStartRadius,
|
|
203
|
+
borderBottomEndRadius,
|
|
204
|
+
borderTopEndRadius,
|
|
205
|
+
borderTopStartRadius,
|
|
206
|
+
borderStyle,
|
|
207
|
+
borderBottomColor,
|
|
208
|
+
borderTopColor,
|
|
209
|
+
borderRightColor,
|
|
210
|
+
borderLeftColor,
|
|
211
|
+
borderStartColor,
|
|
212
|
+
borderEndColor,
|
|
213
|
+
...restStyle
|
|
214
|
+
} = flatStyle;
|
|
215
|
+
const pickerBorderWidth = typeof borderWidth === 'number' ? borderWidth : undefined;
|
|
216
|
+
const pickerBorderTopWidth = typeof borderTopWidth === 'number' ? borderTopWidth : undefined;
|
|
217
|
+
const pickerBorderBottomWidth = typeof borderBottomWidth === 'number' ? borderBottomWidth : undefined;
|
|
218
|
+
const pickerBorderLeftWidth = typeof borderLeftWidth === 'number' ? borderLeftWidth : undefined;
|
|
219
|
+
const pickerBorderRightWidth = typeof borderRightWidth === 'number' ? borderRightWidth : undefined;
|
|
220
|
+
const pickerBorderRadius = typeof borderRadius === 'number' ? borderRadius : DEFAULT_RADIUS;
|
|
221
|
+
const pickerBorderBottomLeftRadius = typeof borderBottomLeftRadius === 'number' ? borderBottomLeftRadius : (typeof borderBottomStartRadius === 'number' ? borderBottomStartRadius : 0);
|
|
222
|
+
const pickerBorderBottomRightRadius = typeof borderBottomRightRadius === 'number' ? borderBottomRightRadius : (typeof borderBottomEndRadius === 'number' ? borderBottomEndRadius : 0);
|
|
223
|
+
const pickerBorderTopLeftRadius = typeof borderTopLeftRadius === 'number' ? borderTopLeftRadius : (typeof borderTopStartRadius === 'number' ? borderTopStartRadius : 0);
|
|
224
|
+
const pickerBorderTopRightRadius = typeof borderTopRightRadius === 'number' ? borderTopRightRadius : (typeof borderTopEndRadius === 'number' ? borderTopEndRadius : 0);
|
|
225
|
+
const pickerBorderColor = processColor(borderColor === undefined ? "white" : borderColor);
|
|
226
|
+
const pickerBorderBottomColor = processColor(borderBottomColor);
|
|
227
|
+
const pickerBorderTopColor = processColor(borderTopColor);
|
|
228
|
+
const pickerBorderRightColor = processColor(borderRightColor);
|
|
229
|
+
const pickerBorderLeftColor = processColor(borderLeftColor);
|
|
230
|
+
const pickerBorderStartColor = processColor(borderStartColor);
|
|
231
|
+
const pickerBorderEndColor = processColor(borderEndColor);
|
|
232
|
+
|
|
233
|
+
const containerStyle = StyleSheet.compose(
|
|
234
|
+
{ height: DEFAULT_HEIGHT, borderRadius: DEFAULT_RADIUS },
|
|
235
|
+
restStyle
|
|
236
|
+
);
|
|
237
|
+
const flatItemStyle = StyleSheet.flatten(itemStyle);
|
|
238
|
+
const flatSelectedItemStyle = StyleSheet.flatten(selectedItemStyle);
|
|
239
|
+
|
|
240
|
+
return (
|
|
241
|
+
<PickerNativeComponent
|
|
242
|
+
testID={testID}
|
|
243
|
+
style={containerStyle}
|
|
244
|
+
selectedValue={selectedValue}
|
|
245
|
+
enabled={enabled}
|
|
246
|
+
mode={mode}
|
|
247
|
+
prompt={prompt}
|
|
248
|
+
items={this.getPickerItemsAsProp()}
|
|
249
|
+
onValueChange={(e) => {
|
|
250
|
+
onValueChange?.(
|
|
251
|
+
e.nativeEvent.itemValue as string | number,
|
|
252
|
+
e.nativeEvent.itemIndex
|
|
253
|
+
);
|
|
254
|
+
}}
|
|
255
|
+
fontColor={processColor(flatStyle?.color ?? 'black') ?? undefined}
|
|
256
|
+
itemFontColor={
|
|
257
|
+
processColor(flatItemStyle?.color ?? 'black') ?? undefined
|
|
258
|
+
}
|
|
259
|
+
itemBackgroundColor={
|
|
260
|
+
processColor(
|
|
261
|
+
flatItemStyle?.backgroundColor ?? 'rgba(255,255,255,0)'
|
|
262
|
+
) ?? undefined
|
|
263
|
+
}
|
|
264
|
+
selectedItemFontColor={
|
|
265
|
+
processColor(
|
|
266
|
+
/**
|
|
267
|
+
* The Native Button Color provides a better contrast than RNOH_BUTTON_COLOR, because it's slightly darker.
|
|
268
|
+
* The color inconsistency between RNOH_BUTTON_COLOR and Native Button Color isn't noticeable in this scenario.
|
|
269
|
+
*/
|
|
270
|
+
flatSelectedItemStyle?.color ?? OHOS_NATIVE_BUTTON_COLOR
|
|
271
|
+
) ?? undefined
|
|
272
|
+
}
|
|
273
|
+
selectedItemBackgroundColor={
|
|
274
|
+
processColor(
|
|
275
|
+
flatSelectedItemStyle?.backgroundColor ??
|
|
276
|
+
/**
|
|
277
|
+
* Reusing RNOH Button color here for consistency between default colors used by RNOH components.
|
|
278
|
+
*/
|
|
279
|
+
RNOH_BUTTON_COLOR_WITH_OPACITY_25
|
|
280
|
+
) ?? undefined
|
|
281
|
+
}
|
|
282
|
+
pickerBorderWidth={pickerBorderWidth}
|
|
283
|
+
pickerBorderTopWidth={pickerBorderTopWidth}
|
|
284
|
+
pickerBorderBottomWidth={pickerBorderBottomWidth}
|
|
285
|
+
pickerBorderLeftWidth={pickerBorderLeftWidth}
|
|
286
|
+
pickerBorderRightWidth={pickerBorderRightWidth}
|
|
287
|
+
pickerBorderColor={pickerBorderColor}
|
|
288
|
+
pickerBorderBottomColor={pickerBorderBottomColor}
|
|
289
|
+
pickerBorderTopColor={pickerBorderTopColor}
|
|
290
|
+
pickerBorderRightColor={pickerBorderRightColor}
|
|
291
|
+
pickerBorderLeftColor={pickerBorderLeftColor}
|
|
292
|
+
pickerBorderStartColor={pickerBorderStartColor}
|
|
293
|
+
pickerBorderEndColor={pickerBorderEndColor}
|
|
294
|
+
pickerBorderRadius={pickerBorderRadius}
|
|
295
|
+
pickerBorderBottomLeftRadius={pickerBorderBottomLeftRadius}
|
|
296
|
+
pickerBorderBottomRightRadius={pickerBorderBottomRightRadius}
|
|
297
|
+
pickerBorderTopLeftRadius={pickerBorderTopLeftRadius}
|
|
298
|
+
pickerBorderTopRightRadius={pickerBorderTopRightRadius}
|
|
299
|
+
pickerBorderStyle={borderStyle}
|
|
300
|
+
/>
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
}
|