/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ import React, { useState, useEffect, useRef } from "react"; export function TopSiteFormInput({ shouldFocus, validationError: validationErrorProp = false, value = "", onClear, onChange, loading, typeUrl, titleId, placeholderId, errorMessageId, autoFocusOnOpen, }) { const [validationError, setValidationError] = useState(validationErrorProp); const inputRef = useRef(null); const prevShouldFocusRef = useRef(false); useEffect(() => { if (shouldFocus && !prevShouldFocusRef.current && inputRef.current) { inputRef.current.focus(); } prevShouldFocusRef.current = shouldFocus; }, [shouldFocus]); useEffect(() => { setValidationError(validationErrorProp); }, [validationErrorProp]); const onClearIconPress = event => { if (event.key === "Enter") { onClear(); } }; const handleChange = ev => { if (validationError) { setValidationError(false); } onChange(ev); }; const renderLoadingOrCloseButton = () => { const showClearButton = value && onClear; if (loading) { return (