UNPKG

@wordpress/components

Version:
76 lines (69 loc) 2.23 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import { createElement } from "@wordpress/element"; /** * External dependencies */ import classnames from 'classnames'; /** * WordPress dependencies */ import { forwardRef, useState } from '@wordpress/element'; /** * Internal dependencies */ export 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 === null || onFocus === void 0 ? void 0 : onFocus(e); }; const onBlurHandler = e => { setHasFocus(false); onBlur === null || onBlur === void 0 ? void 0 : onBlur(e); }; return createElement("input", _extends({ ref: ref, id: `components-form-token-input-${instanceId}`, type: "text" }, restProps, { value: value || '', onChange: onChangeHandler, onFocus: onFocusHandler, onBlur: onBlurHandler, size: size, className: classnames(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}` })); } export const TokenInput = forwardRef(UnForwardedTokenInput); export default TokenInput; //# sourceMappingURL=token-input.js.map