import React from 'react'; import { cx } from './lib/cx'; export type IconProps = { classNames: Partial; }; export type SearchBoxClassNames = { /** * Class names to apply to the root element */ root: string; /** * Class names to apply to the form element */ form: string; /** * Class names to apply to the input element */ input: string; /** * Class names to apply to the submit button */ submit: string; /** * Class names to apply to the reset button */ reset: string; /** * Class names to apply to the loading indicator element */ loadingIndicator: string; /** * Class names to apply to the submit icon */ submitIcon: string; /** * Class names to apply to the reset icon */ resetIcon: string; /** * Class names to apply to the loading icon */ loadingIcon: string; }; export type SearchBoxTranslations = { /** * The alternative text of the submit button. */ submitButtonTitle: string; /** * The alternative text of the reset button. */ resetButtonTitle: string; }; export type SearchBoxProps = Omit< React.ComponentProps<'div'>, 'onSubmit' | 'onReset' | 'onChange' > & Pick, 'onSubmit'> & Required, 'onReset'>> & Pick< React.ComponentProps<'input'>, 'placeholder' | 'onChange' | 'autoFocus' > & { formRef?: React.RefObject; inputRef: React.RefObject; isSearchStalled: boolean; value: string; resetIconComponent?: React.JSXElementConstructor; submitIconComponent?: React.JSXElementConstructor; loadingIconComponent?: React.JSXElementConstructor; classNames?: Partial; translations: SearchBoxTranslations; }; function DefaultSubmitIcon({ classNames }: IconProps) { return ( ); } function DefaultResetIcon({ classNames }: IconProps) { return ( ); } function DefaultLoadingIcon({ classNames }: IconProps) { return ( ); } export function SearchBox({ formRef, inputRef, isSearchStalled, onChange, onReset, onSubmit, placeholder, value, autoFocus, resetIconComponent: ResetIcon = DefaultResetIcon, submitIconComponent: SubmitIcon = DefaultSubmitIcon, loadingIconComponent: LoadingIcon = DefaultLoadingIcon, classNames = {}, translations, ...props }: SearchBoxProps) { function handleSubmit(event: React.FormEvent) { event.preventDefault(); event.stopPropagation(); if (onSubmit) { onSubmit(event); } if (inputRef.current) { inputRef.current.blur(); } } function handleReset(event: React.FormEvent) { event.preventDefault(); event.stopPropagation(); onReset(event); if (inputRef.current) { inputRef.current.focus(); } } return (
); }