UNPKG

@wordpress/components

Version:
81 lines (77 loc) 2.42 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.TokenInput = void 0; exports.UnForwardedTokenInput = UnForwardedTokenInput; exports.default = void 0; var _clsx = _interopRequireDefault(require("clsx")); var _element = require("@wordpress/element"); var _jsxRuntime = require("react/jsx-runtime"); /** * External dependencies */ /** * WordPress dependencies */ /** * Internal dependencies */ function UnForwardedTokenInput(props, ref) { const { value, isExpanded, instanceId, selectedSuggestionIndex, className, onChange, onFocus, onBlur, ...restProps } = props; const [hasFocus, setHasFocus] = (0, _element.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__*/(0, _jsxRuntime.jsx)("input", { ref: ref, id: `components-form-token-input-${instanceId}`, type: "text", ...restProps, value: value || '', onChange: onChangeHandler, onFocus: onFocusHandler, onBlur: onBlurHandler, size: size, className: (0, _clsx.default)(className, 'components-form-token-field__input'), autoComplete: "off", role: "combobox", "aria-expanded": isExpanded, "aria-autocomplete": "list", "aria-owns": isExpanded ? `components-form-token-suggestions-${instanceId}` : undefined, "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}` : undefined, "aria-describedby": `components-form-token-suggestions-howto-${instanceId}` }); } const TokenInput = exports.TokenInput = (0, _element.forwardRef)(UnForwardedTokenInput); var _default = exports.default = TokenInput; //# sourceMappingURL=token-input.js.map