UNPKG

@wordpress/components

Version:
1 lines • 3.57 kB
{"version":3,"file":"token-input.cjs","names":["useState","forwardRef"],"sources":["../../src/form-token-field/token-input.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport type { ChangeEvent, ForwardedRef, FocusEventHandler } from 'react';\nimport { forwardRef, useState } from '@wordpress/element';\nimport type { WordPressComponentProps } from '../context';\nimport type { TokenInputProps } from './types';\n\nexport function UnForwardedTokenInput(\n\tprops: WordPressComponentProps< TokenInputProps, 'input', false >,\n\tref: ForwardedRef< HTMLInputElement >\n) {\n\tconst {\n\t\tvalue,\n\t\tisExpanded,\n\t\tinstanceId,\n\t\tselectedSuggestionIndex,\n\t\tclassName,\n\t\tonChange,\n\t\tonFocus,\n\t\tonBlur,\n\t\t'aria-describedby': ariaDescribedBy,\n\t\t...restProps\n\t} = props;\n\n\tconst [ hasFocus, setHasFocus ] = useState( false );\n\n\tconst size = value ? value.length + 1 : 0;\n\tconst suggestionsId = `components-form-token-suggestions-${ instanceId }`;\n\n\tconst onChangeHandler = ( event: ChangeEvent< HTMLInputElement > ) => {\n\t\tif ( onChange ) {\n\t\t\tonChange( {\n\t\t\t\tvalue: event.target.value,\n\t\t\t} );\n\t\t}\n\t};\n\n\tconst onFocusHandler: FocusEventHandler< HTMLInputElement > = ( e ) => {\n\t\tsetHasFocus( true );\n\t\tonFocus?.( e );\n\t};\n\n\tconst onBlurHandler: FocusEventHandler< HTMLInputElement > = ( e ) => {\n\t\tsetHasFocus( false );\n\t\tonBlur?.( e );\n\t};\n\n\treturn (\n\t\t<input\n\t\t\tref={ ref }\n\t\t\tid={ `components-form-token-input-${ instanceId }` }\n\t\t\ttype=\"text\"\n\t\t\t{ ...restProps }\n\t\t\tvalue={ value || '' }\n\t\t\tonChange={ onChangeHandler }\n\t\t\tonFocus={ onFocusHandler }\n\t\t\tonBlur={ onBlurHandler }\n\t\t\tsize={ size }\n\t\t\tclassName={ clsx(\n\t\t\t\tclassName,\n\t\t\t\t'components-form-token-field__input'\n\t\t\t) }\n\t\t\tautoComplete=\"off\"\n\t\t\trole=\"combobox\"\n\t\t\taria-expanded={ isExpanded }\n\t\t\taria-autocomplete=\"list\"\n\t\t\taria-controls={ isExpanded ? suggestionsId : undefined }\n\t\t\taria-activedescendant={\n\t\t\t\t// Only add the `aria-activedescendant` attribute when:\n\t\t\t\t// - the user is actively interacting with the input (`hasFocus`)\n\t\t\t\t// - there is a selected suggestion (`selectedSuggestionIndex !== -1`)\n\t\t\t\t// - the list of suggestions are rendered in the DOM (`isExpanded`)\n\t\t\t\thasFocus && selectedSuggestionIndex !== -1 && isExpanded\n\t\t\t\t\t? `${ suggestionsId }-${ selectedSuggestionIndex }`\n\t\t\t\t\t: undefined\n\t\t\t}\n\t\t\taria-describedby={ ariaDescribedBy }\n\t\t/>\n\t);\n}\n\nexport const TokenInput = forwardRef( UnForwardedTokenInput );\nTokenInput.displayName = 'TokenInput';\n\nexport default TokenInput;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,SAAgB,sBACf,OACA,KACC;CACD,MAAM,EACL,OACA,YACA,YACA,yBACA,WACA,UACA,SACA,QACA,oBAAoB,iBACpB,GAAG,cACA;CAEJ,MAAM,CAAE,UAAU,gBAAA,GAAgBA,mBAAAA,SAAAA,CAAU,KAAM;CAElD,MAAM,OAAO,QAAQ,MAAM,SAAS,IAAI;CACxC,MAAM,gBAAgB,qCAAsC;CAE5D,MAAM,mBAAoB,UAA4C;EACrE,IAAK,UACJ,SAAU,EACT,OAAO,MAAM,OAAO,MACrB,CAAE;CAEJ;CAEA,MAAM,kBAA0D,MAAO;EACtE,YAAa,IAAK;EAClB,UAAW,CAAE;CACd;CAEA,MAAM,iBAAyD,MAAO;EACrE,YAAa,KAAM;EACnB,SAAU,CAAE;CACb;CAEA,OACC,iBAAA,GAAA,kBAAA,IAAA,CAAC,SAAD;EACO;EACN,IAAK,+BAAgC;EACrC,MAAK;EACL,GAAK;EACL,OAAQ,SAAS;EACjB,UAAW;EACX,SAAU;EACV,QAAS;EACF;EACP,YAAA,GAAY,KAAA,QAAA,CACX,WACA,oCACD;EACA,cAAa;EACb,MAAK;EACL,iBAAgB;EAChB,qBAAkB;EAClB,iBAAgB,aAAa,gBAAgB,KAAA;EAC7C,yBAKC,YAAY,4BAA4B,MAAM,aAC3C,GAAI,cAAe,GAAI,4BACvB,KAAA;EAEJ,oBAAmB;CACnB,CAAA;AAEH;AAEA,MAAa,cAAA,GAAaC,mBAAAA,WAAAA,CAAY,qBAAsB;AAC5D,WAAW,cAAc"}