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] ); useEffect(() => { if (propValue !== value) { setValue(propValue); } // DO NOT include `value` }, [propValue, setValue]); // eslint-disable-line react-hooks/exhaustive-deps const labelMoved = isFocused || value !== ''; return (
{helpText ?
{helpText}
: null}
); }