UNPKG

@wordpress/components

Version:
68 lines (67 loc) 2.1 kB
// packages/components/src/form-token-field/token-input.tsx import clsx from "clsx"; import { forwardRef, useState } from "@wordpress/element"; import { jsx as _jsx } from "react/jsx-runtime"; function UnForwardedTokenInput(props, ref) { const { value, isExpanded, instanceId, selectedSuggestionIndex, className, onChange, onFocus, onBlur, ...restProps } = props; const [hasFocus, setHasFocus] = useState(false); const size = value ? value.length + 1 : 0; const onChangeHandler = (event) => { if (onChange) { onChange({ value: event.target.value }); } }; const onFocusHandler = (e) => { setHasFocus(true); onFocus?.(e); }; const onBlurHandler = (e) => { setHasFocus(false); onBlur?.(e); }; return /* @__PURE__ */ _jsx("input", { ref, id: `components-form-token-input-${instanceId}`, type: "text", ...restProps, value: value || "", onChange: onChangeHandler, onFocus: onFocusHandler, onBlur: onBlurHandler, size, className: clsx(className, "components-form-token-field__input"), autoComplete: "off", role: "combobox", "aria-expanded": isExpanded, "aria-autocomplete": "list", "aria-owns": isExpanded ? `components-form-token-suggestions-${instanceId}` : void 0, "aria-activedescendant": ( // Only add the `aria-activedescendant` attribute when: // - the user is actively interacting with the input (`hasFocus`) // - there is a selected suggestion (`selectedSuggestionIndex !== -1`) // - the list of suggestions are rendered in the DOM (`isExpanded`) hasFocus && selectedSuggestionIndex !== -1 && isExpanded ? `components-form-token-suggestions-${instanceId}-${selectedSuggestionIndex}` : void 0 ), "aria-describedby": `components-form-token-suggestions-howto-${instanceId}` }); } var TokenInput = forwardRef(UnForwardedTokenInput); var token_input_default = TokenInput; export { TokenInput, UnForwardedTokenInput, token_input_default as default }; //# sourceMappingURL=token-input.js.map