|
| 1 | +import React, { memo, forwardRef, useId } from "react"; |
| 2 | +import type { InputHTMLAttributes } from "react"; |
| 3 | +import { symToStr } from "tsafe/symToStr"; |
| 4 | +import { assert } from "tsafe/assert"; |
| 5 | +import { createComponentI18nApi } from "./i18n"; |
| 6 | +import type { Equals } from "tsafe"; |
| 7 | +import { cx } from "./tools/cx"; |
| 8 | +import { fr } from "./fr"; |
| 9 | + |
| 10 | +export type SearchBarProps = { |
| 11 | + className?: string; |
| 12 | + /** Default: "Rechercher" (or translation) */ |
| 13 | + label?: string; |
| 14 | + /** Props forwarded to the underlying <input /> element */ |
| 15 | + nativeInputProps?: InputHTMLAttributes<HTMLInputElement>; |
| 16 | + /** Default: false */ |
| 17 | + big?: boolean; |
| 18 | + classes?: Partial<Record<"root" | "label" | "input", string>>; |
| 19 | +}; |
| 20 | + |
| 21 | +/** |
| 22 | + * @see <https://react-dsfr-components.etalab.studio/?path=/docs/components-input> |
| 23 | + * */ |
| 24 | +export const SearchBar = memo( |
| 25 | + forwardRef<HTMLDivElement, SearchBarProps>((props, ref) => { |
| 26 | + const { |
| 27 | + className, |
| 28 | + label: label_props, |
| 29 | + nativeInputProps = {}, |
| 30 | + big = false, |
| 31 | + classes = {}, |
| 32 | + ...rest |
| 33 | + } = props; |
| 34 | + |
| 35 | + assert<Equals<keyof typeof rest, never>>(); |
| 36 | + |
| 37 | + const { t } = useTranslation(); |
| 38 | + |
| 39 | + const label = label_props ?? t("label"); |
| 40 | + |
| 41 | + const inputId = `search-${useId()}-input`; |
| 42 | + |
| 43 | + return ( |
| 44 | + <div |
| 45 | + className={cx( |
| 46 | + fr.cx("fr-search-bar", big && "fr-search-bar--lg"), |
| 47 | + classes.root, |
| 48 | + className |
| 49 | + )} |
| 50 | + role="search" |
| 51 | + ref={ref} |
| 52 | + {...rest} |
| 53 | + > |
| 54 | + <label className={cx(fr.cx("fr-label"), classes.label)} htmlFor={inputId}> |
| 55 | + {label} |
| 56 | + </label> |
| 57 | + <input |
| 58 | + {...nativeInputProps} |
| 59 | + className={cx(fr.cx("fr-input"), classes.input, nativeInputProps.className)} |
| 60 | + placeholder={label} |
| 61 | + type="search" |
| 62 | + id={inputId} |
| 63 | + /> |
| 64 | + <button className="fr-btn" title="Rechercher"> |
| 65 | + {label} |
| 66 | + </button> |
| 67 | + </div> |
| 68 | + ); |
| 69 | + }) |
| 70 | +); |
| 71 | + |
| 72 | +SearchBar.displayName = symToStr({ SearchBar }); |
| 73 | + |
| 74 | +export default SearchBar; |
| 75 | + |
| 76 | +const { useTranslation, addSearchBarTranslations } = createComponentI18nApi({ |
| 77 | + "componentName": symToStr({ SearchBar }), |
| 78 | + "frMessages": { |
| 79 | + /* spell-checker: disable */ |
| 80 | + "label": "Rechercher" |
| 81 | + /* spell-checker: enable */ |
| 82 | + } |
| 83 | +}); |
| 84 | + |
| 85 | +addSearchBarTranslations({ |
| 86 | + "lang": "en", |
| 87 | + "messages": { |
| 88 | + "label": "Search" |
| 89 | + } |
| 90 | +}); |
| 91 | + |
| 92 | +export { addSearchBarTranslations }; |
0 commit comments