import { type CSSProperties, Fragment, type InputHTMLAttributes, type SyntheticEvent, useMemo, } from 'react'; import { Dropdown } from './dropdown-button'; import Button from './button'; import type { FranklinStyle } from '../types/common'; import '../styles/components/main-search.scss'; interface InputInternalStyle extends CSSProperties { '--input-padding': string; } export type MainSearchProps = { /** * Action performed upon search submission */ onSubmit: (e: SyntheticEvent) => void; /** * What happens when text is typed in the search box */ onTextChange: (selectedValue: string) => void; /** * The search term */ searchTerm?: string; /** * An object representing the different namespace options */ namespaces?: Record; /** * What happens when a namespace is changed in the dropdown */ onNamespaceChange?: (namespace: string) => void; /** * The selected namespace in the dropdown */ selectedNamespace?: string; /** * A list of objects representing secondary buttons and their actions */ secondaryButtons?: { label: string; action: () => void }[]; } & InputHTMLAttributes; const countCharacters = (items: string[]) => items.reduce((prev, curr) => prev + curr.length, 0); const MainSearch = ({ searchTerm, namespaces = {}, onTextChange, onSubmit, onNamespaceChange, selectedNamespace, secondaryButtons, ...props }: MainSearchProps) => { const style = useMemo( () => ({ '--main-button-color': `var(--fr--color-${selectedNamespace}, var(--fr--color-sea-blue))`, }), [selectedNamespace] ); const inputStyle = useMemo(() => { const count = secondaryButtons && countCharacters(secondaryButtons.map(({ label }) => label)); return count ? { '--input-padding': `${count}ch` } : undefined; }, [secondaryButtons]); const visibleElement = (onClick: () => unknown) => ( ); return (
{Object.keys(namespaces).length > 0 && onNamespaceChange && (
    {Object.keys(namespaces).map((key) => (
  • ))}
)}
onTextChange(e.target.value)} value={searchTerm} style={inputStyle} {...props} /> {secondaryButtons && (
{secondaryButtons.map(({ label, action }, index) => ( {index > 0 && | } ))}
)}
); }; export default MainSearch;