import React, { type ComponentType, createContext, type ForwardedRef, forwardRef, memo, type PropsWithChildren, useContext, } from 'react'; import type {StyleProp, TextStyle, ViewStyle} from 'react-native'; import {useMemoObject} from '@rozhkov/react-useful-hooks'; import type {PickerProps} from '../base'; type ContextValue = { itemHeight: number | undefined; visibleItemCount: number | undefined; readOnly: boolean | undefined; enableScrollByTapOnItem: boolean | undefined; scrollEventThrottle: number | undefined; pickerStyle: StyleProp | undefined; itemTextStyle: StyleProp | undefined; overlayItemStyle: StyleProp | undefined; contentContainerStyle: StyleProp | undefined; }; const DatePickerCommonPropsContext = createContext( undefined, ); type DatePickerCommonPropsProviderProps = PropsWithChildren; const DatePickerCommonPropsProvider = ({ children, ...restProps }: DatePickerCommonPropsProviderProps) => { const memoizedValue = useMemoObject(restProps); return ( {children} ); }; export default DatePickerCommonPropsProvider; const useDatePickerCommonProps = () => { const value = useContext(DatePickerCommonPropsContext); if (value === undefined) { throw new Error( 'useDatePickerCommonProps must be called from within DatePickerCommonPropsContext.Provider!', ); } return useContext(DatePickerCommonPropsContext)!; }; type PickedWheelPickerProps = Pick< PickerProps, Exclude | 'style' >; export const withCommonProps = ( WheelPickerComponent: ComponentType, ) => { const WrappedWheelPicker = ( { style: pickerStyleProp, contentContainerStyle: contentContainerStyleProp, itemTextStyle: itemTextStyleProp, overlayItemStyle: overlayItemStyleProp, ...restProps }: PickedWheelPickerProps, forwardedRef: ForwardedRef, ) => { const { pickerStyle: pickerStyleCommon, contentContainerStyle: contentContainerStyleCommon, itemTextStyle: itemTextStyleCommon, overlayItemStyle: overlayItemStyleCommon, ...restCommonProps } = useDatePickerCommonProps(); const style = useMemoObject([pickerStyleCommon, pickerStyleProp]); const contentContainerStyle = useMemoObject([ contentContainerStyleCommon, contentContainerStyleProp, ]); const itemTextStyle = useMemoObject([ itemTextStyleCommon, itemTextStyleProp, ]); const overlayItemStyle = useMemoObject([ overlayItemStyleCommon, overlayItemStyleProp, ]); return ( ); }; WrappedWheelPicker.displayName = `withDateCommonProps(${WheelPickerComponent.displayName})`; return memo(forwardRef(WrappedWheelPicker)) as typeof WheelPickerComponent; };