UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

98 lines 6.43 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; /* eslint-disable jsx-a11y/no-static-element-interactions */ import React, { forwardRef, useRef, useState, } from "react"; import { MagnifyingGlassIcon, XMarkIcon } from "@navikt/aksel-icons"; import { Button } from "../../button/index.js"; import { BodyShort, ErrorMessage, Label } from "../../typography/index.js"; import { omit } from "../../utils-external/index.js"; import { cl } from "../../utils/helpers/index.js"; import { useMergeRefs } from "../../utils/hooks/index.js"; import { useI18n } from "../../utils/i18n/i18n.hooks.js"; import { useFormField } from "../useFormField.js"; import SearchButton from "./SearchButton.js"; import { SearchContext } from "./context.js"; /** * A component that displays a search input field. * * @see [📝 Documentation](https://aksel.nav.no/komponenter/core/search) * @see 🏷️ {@link SearchProps} * * @example * ```jsx * <form role="search"> * <Search label="Søk i alle Nav sine sider" variant="primary" /> * </form> * ``` */ export const Search = forwardRef((props, ref) => { const { inputProps, size = "medium", inputDescriptionId, errorId, showErrorMsg, hasError, } = useFormField(props, "searchfield"); const { className, hideLabel = true, label, description, value, clearButtonLabel, onClear, clearButton = true, children, variant = "primary", defaultValue, onChange, onSearchClick, htmlSize, "data-color": dataColor } = props, rest = __rest(props, ["className", "hideLabel", "label", "description", "value", "clearButtonLabel", "onClear", "clearButton", "children", "variant", "defaultValue", "onChange", "onSearchClick", "htmlSize", "data-color"]); const searchRef = useRef(null); const mergedRef = useMergeRefs(searchRef, ref); const [internalValue, setInternalValue] = useState(defaultValue !== null && defaultValue !== void 0 ? defaultValue : ""); const handleChange = (newValue) => { value === undefined && setInternalValue(newValue); onChange === null || onChange === void 0 ? void 0 : onChange(newValue); }; const handleClear = (clearEvent) => { var _a, _b; onClear === null || onClear === void 0 ? void 0 : onClear(clearEvent); handleChange(""); (_b = (_a = searchRef.current) === null || _a === void 0 ? void 0 : _a.focus) === null || _b === void 0 ? void 0 : _b.call(_a); }; const handleClick = () => { onSearchClick === null || onSearchClick === void 0 ? void 0 : onSearchClick(`${value !== null && value !== void 0 ? value : internalValue}`); }; const showClearButton = clearButton && !inputProps.disabled && (value !== null && value !== void 0 ? value : internalValue); return ( // biome-ignore lint/a11y/noStaticElementInteractions: Escape key handler for clearing input React.createElement("div", { onKeyDown: (event) => { var _a; if (event.key !== "Escape") { return; } ((_a = searchRef.current) === null || _a === void 0 ? void 0 : _a.value) && event.preventDefault(); handleClear({ trigger: "Escape", event }); }, className: cl(className, "aksel-form-field", `aksel-form-field--${size}`, "aksel-search", { "aksel-search--error": hasError, "aksel-search--disabled": inputProps.disabled, "aksel-search--with-size": htmlSize, }), "data-color": dataColor }, React.createElement(Label, { htmlFor: inputProps.id, size: size, className: cl("aksel-form-field__label", { "aksel-sr-only": hideLabel, }) }, label), !!description && (React.createElement(BodyShort, { className: cl("aksel-form-field__description", { "aksel-sr-only": hideLabel, }), id: inputDescriptionId, size: size, as: "div" }, description)), React.createElement("div", { className: "aksel-search__wrapper" }, React.createElement("div", { className: "aksel-search__wrapper-inner" }, variant === "simple" && (React.createElement(MagnifyingGlassIcon, { "aria-hidden": true, className: "aksel-search__search-icon" })), React.createElement("input", Object.assign({ ref: mergedRef }, omit(rest, ["error", "errorId", "size", "readOnly"]), inputProps, { value: value !== null && value !== void 0 ? value : internalValue, onChange: (e) => handleChange(e.target.value), type: "search", className: cl(className, "aksel-search__input", `aksel-search__input--${variant}`, "aksel-text-field__input", "aksel-body-short", `aksel-body-short--${size}`) }, (htmlSize ? { size: Number(htmlSize) } : {}))), showClearButton && (React.createElement(ClearButton, { handleClear: handleClear, size: size, clearButtonLabel: clearButtonLabel }))), React.createElement(SearchContext.Provider, { value: { size, disabled: inputProps.disabled, variant, handleClick, } }, children ? children : variant !== "simple" && React.createElement(SearchButton, { "data-color": dataColor }))), React.createElement("div", { className: "aksel-form-field__error", id: errorId, "aria-relevant": "additions removals", "aria-live": "polite" }, showErrorMsg && (React.createElement(ErrorMessage, { size: size, showIcon: true }, props.error))))); }); function ClearButton({ size, clearButtonLabel, handleClear, }) { const translate = useI18n("Search"); return (React.createElement(Button, { className: "aksel-search__button-clear", variant: "tertiary", "data-color": "neutral", size: size === "medium" ? "small" : "xsmall", icon: React.createElement(XMarkIcon, { "aria-hidden": true }), title: clearButtonLabel || translate("clear"), onClick: (event) => handleClear({ trigger: "Click", event }), type: "button" })); } Search.Button = SearchButton; export default Search; //# sourceMappingURL=Search.js.map