import { memo, useRef, useEffect, useCallback } from 'react'; import { TextInput as TextInputNative, Keyboard, View } from 'react-native'; import type { ModeType, ValidRangeType } from '../DatePickerInline'; import DatePickerInputWithoutModal from '../DatePickerInput/DatePickerInputWithoutModal'; import type { LocalState, LocalStateSingle, LocalStateRange } from './types'; import { datePickerModalEditStyles } from './utils'; type Props = { mode: ModeType; label?: string; startLabel?: string; endLabel?: string; state: LocalState; collapsed: boolean; onChange: (s: LocalState) => any; validRange: ValidRangeType | undefined; // locale: string; }; function CalendarEdit({ mode, state, label = '', startLabel = 'Start', endLabel = 'End', collapsed, onChange, validRange, }: // locale, Props) { const dateInput = useRef(null); const startInput = useRef(null); const endInput = useRef(null); // when switching views focus, or un-focus text input useEffect(() => { // hide open keyboard if (collapsed) { Keyboard.dismiss(); } const inputsToFocus = [dateInput.current, startInput.current].filter( n => n, ) as TextInputNative[]; const inputsToBlur = [dateInput.current, startInput.current, endInput.current].filter( n => n, ) as TextInputNative[]; if (collapsed) { inputsToBlur.forEach(ip => ip.blur()); } else { inputsToFocus.forEach(ip => ip.focus()); } }, [mode, startInput, endInput, dateInput, collapsed]); const onSubmitStartInput = useCallback(() => { if (endInput.current) { endInput.current.focus(); } }, [endInput]); const onSubmitEndInput = useCallback(() => { // TODO: close modal and persist range }, []); const onSubmitInput = useCallback(() => { // TODO: close modal and persist range }, []); const onSingleInputChange = useCallback( (date: Date | null) => onChange({ ...state, date }), [onChange, state], ); const onStartDateChange = useCallback( (startDate: Date | null) => onChange({ ...state, startDate }), [onChange, state], ); const onEndDateChange = useCallback( (endDate: Date | null) => onChange({ ...state, endDate }), [onChange, state], ); return ( <> {mode === 'single' ? ( ) : null} <> {mode === 'range' ? ( ) : null} ); } export default memo(CalendarEdit);