/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ import React, { useState } from "react"; import { useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import Button from "./Button"; import styles from "./Search.module.scss"; interface Props { id: string; onSubmit?: Function; onClear?: Function; query?: string; results?: number; size: "big" | "small"; lastQuery?: string; placeholder?: string; label?: string; wide?: boolean; } interface FormValues { query: string; } function Search(props: Props) { const { register, handleSubmit, reset } = useForm(); const [lastQuery, setLastQuery] = useState(props.lastQuery || ""); const { t } = useTranslation(); const onSubmit = (values: FormValues) => { if (props.onSubmit) { props.onSubmit(values.query); } }; const onClear = () => { if (props.onClear) { reset(); props.onClear(); } }; let formClasses = ["usa-search"]; let searchBarClasses = ["usa-input"]; let buttonClasses = ["usa-button", "usa-button--base"]; if (props.size === "small") { formClasses.push("usa-search--small"); buttonClasses.push(styles.small); searchBarClasses.push(styles.small); if (props.wide) { searchBarClasses.push(styles.smallwide); } } else { formClasses.push("usa-search--big"); if (props.wide) { formClasses.push(styles.bigwide); } } return ( <>
) => setLastQuery(event.target.value) } />
{props.onClear && props.query && (
{`${props.results} results for "${props.query}"`}
)} ); } export default Search;