import { mergeProps } from "@seed-design/dom-utils"; import { searchInput, type SearchInputVariantProps } from "@seed-design/css/recipes/search-input"; import { TextField } from "@seed-design/react-text-field"; import clsx from "clsx"; import type * as React from "react"; import { createContext, forwardRef, useContext } from "react"; import { createSlotRecipeContext } from "../../utils/createSlotRecipeContext"; import { withScaleFeedback } from "../../utils/withScaleFeedback"; const { withProvider, withContext, useClassNames } = createSlotRecipeContext(searchInput); interface SearchInputSubmitContextValue { onSubmit?: (value: string) => void; blurOnSubmit: boolean; } const SearchInputSubmitContext = createContext({ blurOnSubmit: true, }); //////////////////////////////////////////////////////////////////////////////////// export interface SearchInputRootProps extends SearchInputVariantProps, Omit { /** * Called with the current value when Enter is pressed. * Enter that confirms an IME composition (e.g. Korean input) is ignored. */ onSubmit?: (value: string) => void; /** * Whether to blur the input after submitting, which also dismisses the on-screen keyboard. * @default true */ blurOnSubmit?: boolean; } export const SearchInputRoot = withProvider( forwardRef( ({ onSubmit, blurOnSubmit = true, ...otherProps }, ref) => ( ), ), "root", ); //////////////////////////////////////////////////////////////////////////////////// export interface SearchInputInputProps extends TextField.InputProps {} export const SearchInputInput = forwardRef( ({ className, ...otherProps }, ref) => { const classNames = useClassNames(); const { onSubmit, blurOnSubmit } = useContext(SearchInputSubmitContext); if (!otherProps["aria-label"] && !otherProps["aria-labelledby"]) { console.warn( "SearchInputInput: Please provide `aria-label` or `aria-labelledby` for accessibility.", ); } const mergedProps = mergeProps( { type: "search", enterKeyHint: "search", onKeyDown(event: React.KeyboardEvent) { if (event.key !== "Enter") return; // Enter that confirms an IME composition is not a submit. Safari reports `isComposing: false` // for that Enter but still sets `keyCode` to 229. if (event.nativeEvent.isComposing || event.keyCode === 229) return; if (onSubmit) { // `onSubmit` handles the submit, so a surrounding form must not submit too. // Without `onSubmit`, Enter keeps submitting the form. event.preventDefault(); onSubmit(event.currentTarget.value); } if (blurOnSubmit) { event.currentTarget.blur(); } }, } satisfies TextField.InputProps, otherProps, ); return ( ); }, ); SearchInputInput.displayName = "SearchInputInput"; //////////////////////////////////////////////////////////////////////////////////// export interface SearchInputClearButtonProps extends TextField.ClearButtonProps {} /** * Renders nothing while the value is empty. Keeps the input focused if it was focused. */ export const SearchInputClearButton = withScaleFeedback( withContext( forwardRef((props, ref) => { if (!props["aria-label"] && !props["aria-labelledby"]) { console.warn( "SearchInputClearButton: Please provide `aria-label` or `aria-labelledby` for accessibility.", ); } return ; }), "clearButton", ), );