import * as React from 'react' import { View, TextInput, TextInputProps, StyleSheet, Platform, } from 'react-native' import { useTheme, TouchableRipple } from 'react-native-paper' import Color from 'color' import { inputTypes, PossibleClockTypes, PossibleInputTypes, useInputColors, } from './timeUtils' interface TimeInputProps extends Omit, 'onFocus'> { value: number clockType: PossibleClockTypes onPress?: (type: PossibleClockTypes) => any pressed: boolean onChanged: (n: number) => any inputType: PossibleInputTypes inputFontSize?: number } function TimeInput( { value, clockType, pressed, onPress, onChanged, inputType, inputFontSize = 57, ...rest }: TimeInputProps, ref: any ) { const [controlledValue, setControlledValue] = React.useState( `${value}` ) const onInnerChange = (text: string) => { setControlledValue(text) if (text !== '' && text !== '0') { onChanged(Number(text)) } } React.useEffect(() => { setControlledValue(`${value}`) }, [value]) const theme = useTheme() const [inputFocused, setInputFocused] = React.useState(false) const highlighted = inputType === inputTypes.picker ? pressed : inputFocused const { color, backgroundColor } = useInputColors(highlighted) let formattedValue = controlledValue if (!inputFocused) { formattedValue = controlledValue.length === 1 ? `0${controlledValue}` : `${controlledValue}` } return ( setInputFocused(true)} onBlur={() => setInputFocused(false)} keyboardAppearance={theme.dark ? 'dark' : 'default'} keyboardType="number-pad" onChangeText={onInnerChange} {...rest} /> {onPress && inputType === inputTypes.picker ? ( onPress(clockType)} borderless={true} > ) : null} ) } const styles = StyleSheet.create({ root: { position: 'relative', height: 80, width: 96 }, input: { textAlign: 'center', textAlignVertical: 'center', width: 96, }, buttonOverlay: { overflow: 'hidden' }, }) export default React.forwardRef(TimeInput)