|
| 1 | +import React, { useState, useEffect, useRef, useMemo } from 'react'; |
| 2 | +import { View, StyleSheet, Text, StyleProp, TextStyle } from 'react-native'; |
| 3 | +import Picker, { PickerProps } from '../../Picker'; |
| 4 | + |
| 5 | +// export interface PickerDate { |
| 6 | +// label?: string; |
| 7 | +// render?: (key: string, record: PickerData, index: number) => React.ReactNode; |
| 8 | +// [key: string]: any; |
| 9 | +// } |
| 10 | + |
| 11 | +enum DateObject { |
| 12 | + year = '年', |
| 13 | + month = '月', |
| 14 | + day = '日', |
| 15 | + hour = '时', |
| 16 | + minute = '分', |
| 17 | + second = '秒', |
| 18 | +} |
| 19 | +export type DateKey = keyof typeof DateObject; |
| 20 | + |
| 21 | +export interface PickerViewProps extends PickerProps { |
| 22 | + title: DateKey; |
| 23 | + showTitle?: boolean; |
| 24 | + titleStyle?: StyleProp<TextStyle>; |
| 25 | + getTypeDate: (index: number, label: string, title: DateKey) => unknown; |
| 26 | + renderTitle?: (text: '年' | '月' | '日' | '时' | '分' | '秒') => React.ReactNode; |
| 27 | +} |
| 28 | + |
| 29 | +const PickerView = (props: PickerViewProps) => { |
| 30 | + const { title, showTitle, titleStyle, renderTitle, getTypeDate, value, ...other } = props; |
| 31 | + const textStyle = StyleSheet.flatten([styles.textStyle, titleStyle]); |
| 32 | + const [state, setState] = useState<number>(0); |
| 33 | + useEffect(() => { |
| 34 | + setState(value || 0); |
| 35 | + }, [value]); |
| 36 | + useEffect(() => { |
| 37 | + getTypeDate(state, other.data?.[state]?.label!, title); |
| 38 | + }, [state]); |
| 39 | + return ( |
| 40 | + <View style={{ flex: 1 }}> |
| 41 | + {showTitle && |
| 42 | + (renderTitle ? ( |
| 43 | + renderTitle(DateObject[title as DateKey]) |
| 44 | + ) : ( |
| 45 | + <View style={styles.viewStyle}> |
| 46 | + <Text style={textStyle}>{DateObject[title as DateKey]}</Text> |
| 47 | + </View> |
| 48 | + ))} |
| 49 | + <Picker {...other} value={state} onChange={(val) => setState(val)} /> |
| 50 | + </View> |
| 51 | + ); |
| 52 | +}; |
| 53 | + |
| 54 | +const styles = StyleSheet.create({ |
| 55 | + viewStyle: { |
| 56 | + height: 50, |
| 57 | + alignItems: 'center', |
| 58 | + justifyContent: 'center', |
| 59 | + borderTopWidth: 1, |
| 60 | + borderTopColor: '#E6E6E6', |
| 61 | + }, |
| 62 | + textStyle: { |
| 63 | + fontSize: 18, |
| 64 | + color: '#000', |
| 65 | + paddingVertical: 0, |
| 66 | + paddingHorizontal: 0, |
| 67 | + }, |
| 68 | +}); |
| 69 | + |
| 70 | +export default PickerView; |
0 commit comments