import { h } from 'preact'; import { useCallback, useEffect, useState } from 'preact/hooks'; export default function TextField({ helpText, keyboardType = 'text', inputRef, label, leadingIcon: LeadingIcon, onBlur, onChangeText, onFocus, readonly, trailingIcon: TrailingIcon, value: propValue = '', ...props }) { const [isFocused, setFocused] = useState(false); const [value, setValue] = useState(propValue); const handleFocus = useCallback( (event) => { setFocused(true); onFocus && onFocus(event); }, [onFocus] ); const handleBlur = useCallback( (event) => { setFocused(false); onBlur && onBlur(event); }, [onBlur] ); const handleChange = useCallback( (event) => { const { value } = event.target; setValue(value); onChangeText && onChangeText(value); }, [onChangeText, setValue] ); // Reset the state if the prop value changes useEffect(() => { if (propValue !== value) { setValue(propValue); } }, [propValue, setValue]); const labelMoved = isFocused || value !== ''; return (