@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
102 lines • 6.86 kB
JavaScript
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;
};
import React, { forwardRef, useRef, useState, } from "react";
import { MagnifyingGlassIcon, XMarkIcon } from "@navikt/aksel-icons";
import { Button } from "../../button/index.js";
import { useRenameCSS, useThemeInternal } from "../../theme/Theme.js";
import { BodyShort, ErrorMessage, Label } from "../../typography/index.js";
import { omit } from "../../util/index.js";
import { useMergeRefs } from "../../util/hooks/useMergeRefs.js";
import { useI18n } from "../../util/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 { cn } = useRenameCSS();
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 (
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
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: cn(className, "navds-form-field", `navds-form-field--${size}`, "navds-search", {
"navds-search--error": hasError,
"navds-search--disabled": inputProps.disabled,
"navds-search--with-size": htmlSize,
}), "data-color": dataColor },
React.createElement(Label, { htmlFor: inputProps.id, size: size, className: cn("navds-form-field__label", {
"navds-sr-only": hideLabel,
}) }, label),
!!description && (React.createElement(BodyShort, { className: cn("navds-form-field__description", {
"navds-sr-only": hideLabel,
}), id: inputDescriptionId, size: size, as: "div" }, description)),
React.createElement("div", { className: cn("navds-search__wrapper") },
React.createElement("div", { className: cn("navds-search__wrapper-inner") },
variant === "simple" && (React.createElement(MagnifyingGlassIcon, { "aria-hidden": true, className: cn("navds-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: cn(className, "navds-search__input", `navds-search__input--${variant}`, "navds-text-field__input", "navds-body-short", `navds-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: cn("navds-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 { cn } = useRenameCSS();
const themeContext = useThemeInternal(false);
const translate = useI18n("Search");
return themeContext ? (React.createElement(Button, { className: cn("navds-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" })) : (React.createElement("button", { type: "button", onClick: (event) => handleClear({ trigger: "Click", event }), className: cn("navds-search__button-clear") },
React.createElement("span", { className: cn("navds-sr-only") }, clearButtonLabel || translate("clear")),
React.createElement(XMarkIcon, { "aria-hidden": true })));
}
Search.Button = SearchButton;
export default Search;
//# sourceMappingURL=Search.js.map