Skip to content

Commit f64b6bd

Browse files
zxh-994ZhuXionghui
and
ZhuXionghui
authored
fix: 修改时间选择组件dateTimepicker,报错组件segmentedControl、slider、incubatorDialog组件 (#6)
Co-authored-by: ZhuXionghui <n037406@archermind.com>
1 parent 26354d1 commit f64b6bd

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

Diff for: src/components/dateTimePicker/useOldApi.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,41 @@ const useOldApi = (props: OldApiProps) => {
4747
? dateFormatter(value)
4848
: dateFormat && moment
4949
? moment(value).format(dateFormat)
50-
: value.toLocaleDateString();
50+
: formatTimestamp(value).formattedDate;
5151
case 'time':
5252
return timeFormatter
5353
? timeFormatter(value)
5454
: timeFormat && moment
5555
? moment(value).format(timeFormat)
56-
: value.toLocaleTimeString();
56+
: formatTimestamp(value).formattedTime;
5757
}
5858
}
5959
};
6060

6161
return {getStringValue};
6262
};
6363

64+
function formatTimestamp(timestamp: any) {
65+
const ts = typeof timestamp === 'string' ? Number(timestamp) : timestamp;
66+
const date = new Date(ts);
67+
68+
if (isNaN(date.getTime())) {
69+
return { formattedDate: '', formattedTime: '' };
70+
}
71+
// const date = new Date(timestamp);
72+
73+
const year = date.getFullYear();
74+
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始
75+
const day = String(date.getDate()).padStart(2, '0');
76+
77+
const hours = String(date.getHours()).padStart(2, '0');
78+
const minutes = String(date.getMinutes()).padStart(2, '0');
79+
// const seconds = String(date.getSeconds()).padStart(2, '0');
80+
81+
const formattedDate = `${year}/${month}/${day}`;
82+
const formattedTime = `${hours}:${minutes}`;
83+
84+
return { formattedDate, formattedTime };
85+
}
86+
6487
export default useOldApi;

Diff for: src/components/segmentedControl/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ const SegmentedControl = (props: SegmentedControlProps) => {
204204
const isMiddleSelected = !isFirstElementSelected && !isLastElementSelected;
205205
const insetFix = -CONTAINER_BORDER_WIDTH - (!isFirstElementSelected ? segmentDividerWidth : 1);
206206
const widthFix = isMiddleSelected ? 2 * segmentDividerWidth : CONTAINER_BORDER_WIDTH + segmentDividerWidth;
207-
const inset = withTiming(value[animatedSelectedIndex.value].x + insetFix, TIMING_CONFIG);
208-
const width = withTiming(value[animatedSelectedIndex.value].width + widthFix, TIMING_CONFIG);
207+
const inset = withTiming(value[animatedSelectedIndex.value]?.x + insetFix, TIMING_CONFIG);
208+
const width = withTiming(value[animatedSelectedIndex.value]?.width + widthFix, TIMING_CONFIG);
209209
return Constants.isRTL ? {width, height, right: inset} : {width, height, left: inset};
210210
}
211211
return {};

Diff for: src/components/slider/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,10 @@ class Slider extends PureComponent<InternalSliderProps, State> {
367367
const newX = Constants.isRTL && !this.disableRTL ? trackSize.width - nativeEvent.locationX : nativeEvent.locationX;
368368

369369
if (useRange) {
370-
if (this.isDefaultThumbActive() && this._minThumbStyles?.left && newX < this._minThumbStyles?.left) {
370+
if (this.isDefaultThumbActive() && this._minThumbStyles?.left && newX < Number(this._minThumbStyles?.left)) {
371371
// new x is smaller then min but the active thumb is the max
372372
this.setActiveThumb(this.minThumb);
373-
} else if (!this.isDefaultThumbActive() && this._thumbStyles.left && newX > this._thumbStyles.left) {
373+
} else if (!this.isDefaultThumbActive() && this._thumbStyles.left && newX > Number(this._thumbStyles.left)) {
374374
// new x is bigger then max but the active thumb is the min
375375
this.setActiveThumb(this.thumb);
376376
}

Diff for: src/incubator/Dialog/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ const Dialog = (props: DialogProps, ref: ForwardedRef<DialogImperativeMethods>)
214214
const renderDialog = () => (
215215
<GestureDetector gesture={panGesture}>
216216
{/* @ts-expect-error should be fixed in version 3.5 (https://github.com/software-mansion/react-native-reanimated/pull/4881) */}
217-
<View {...containerProps} reanimated style={style} onLayout={onLayout} ref={setRef} testID={testID}>
217+
<View {...containerProps} style={style} onLayout={onLayout} ref={setRef} testID={testID}>
218218
{renderDialogContent()}
219219
</View>
220220
</GestureDetector>
@@ -228,7 +228,7 @@ const Dialog = (props: DialogProps, ref: ForwardedRef<DialogImperativeMethods>)
228228
}, [overlayBackgroundColor]);
229229

230230
const renderOverlayView = () => (
231-
<View testID={`${testID}.overlayFadingBackground`} absF reanimated style={overlayStyle} pointerEvents="none"/>
231+
<View testID={`${testID}.overlayFadingBackground`} absF style={overlayStyle} pointerEvents="none"/>
232232
);
233233

234234
return (

0 commit comments

Comments
 (0)