UNPKG

@wordpress/components

Version:
8 lines (7 loc) 4.66 kB
{ "version": 3, "sources": ["../../src/autocomplete/get-default-use-items.tsx"], "sourcesContent": ["/**\n * External dependencies\n */\nimport removeAccents from 'remove-accents';\n\n/**\n * WordPress dependencies\n */\nimport { debounce } from '@wordpress/compose';\nimport { useLayoutEffect, useState } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { escapeRegExp } from '../utils/strings';\nfunction filterOptions(search, options = [], maxResults = 10) {\n const filtered = [];\n for (let i = 0; i < options.length; i++) {\n const option = options[i];\n\n // Merge label into keywords.\n let {\n keywords = []\n } = option;\n if ('string' === typeof option.label) {\n keywords = [...keywords, option.label];\n }\n const isMatch = keywords.some(keyword => search.test(removeAccents(keyword)));\n if (!isMatch) {\n continue;\n }\n filtered.push(option);\n\n // Abort early if max reached.\n if (filtered.length === maxResults) {\n break;\n }\n }\n return filtered;\n}\nexport default function getDefaultUseItems(autocompleter) {\n return filterValue => {\n const [items, setItems] = useState([]);\n /*\n * We support both synchronous and asynchronous retrieval of completer options\n * but internally treat all as async so we maintain a single, consistent code path.\n *\n * Because networks can be slow, and the internet is wonderfully unpredictable,\n * we don't want two promises updating the state at once. This ensures that only\n * the most recent promise will act on `optionsData`. This doesn't use the state\n * because `setState` is batched, and so there's no guarantee that setting\n * `activePromise` in the state would result in it actually being in `this.state`\n * before the promise resolves and we check to see if this is the active promise or not.\n */\n useLayoutEffect(() => {\n const {\n options,\n isDebounced\n } = autocompleter;\n const loadOptions = debounce(() => {\n const promise = Promise.resolve(typeof options === 'function' ? options(filterValue) : options).then(optionsData => {\n if (promise.canceled) {\n return;\n }\n const keyedOptions = optionsData.map((optionData, optionIndex) => ({\n key: `${autocompleter.name}-${optionIndex}`,\n value: optionData,\n label: autocompleter.getOptionLabel(optionData),\n keywords: autocompleter.getOptionKeywords ? autocompleter.getOptionKeywords(optionData) : [],\n isDisabled: autocompleter.isOptionDisabled ? autocompleter.isOptionDisabled(optionData) : false\n }));\n\n // Create a regular expression to filter the options.\n const search = new RegExp('(?:\\\\b|\\\\s|^)' + escapeRegExp(filterValue), 'i');\n setItems(filterOptions(search, keyedOptions));\n });\n return promise;\n }, isDebounced ? 250 : 0);\n const promise = loadOptions();\n return () => {\n loadOptions.cancel();\n if (promise) {\n promise.canceled = true;\n }\n };\n }, [filterValue]);\n return [items];\n };\n}"], "mappings": ";AAGA,OAAO,mBAAmB;AAK1B,SAAS,gBAAgB;AACzB,SAAS,iBAAiB,gBAAgB;AAK1C,SAAS,oBAAoB;AAC7B,SAAS,cAAc,QAAQ,UAAU,CAAC,GAAG,aAAa,IAAI;AAC5D,QAAM,WAAW,CAAC;AAClB,WAAS,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;AACvC,UAAM,SAAS,QAAQ,CAAC;AAGxB,QAAI;AAAA,MACF,WAAW,CAAC;AAAA,IACd,IAAI;AACJ,QAAI,aAAa,OAAO,OAAO,OAAO;AACpC,iBAAW,CAAC,GAAG,UAAU,OAAO,KAAK;AAAA,IACvC;AACA,UAAM,UAAU,SAAS,KAAK,aAAW,OAAO,KAAK,cAAc,OAAO,CAAC,CAAC;AAC5E,QAAI,CAAC,SAAS;AACZ;AAAA,IACF;AACA,aAAS,KAAK,MAAM;AAGpB,QAAI,SAAS,WAAW,YAAY;AAClC;AAAA,IACF;AAAA,EACF;AACA,SAAO;AACT;AACe,SAAR,mBAAoC,eAAe;AACxD,SAAO,iBAAe;AACpB,UAAM,CAAC,OAAO,QAAQ,IAAI,SAAS,CAAC,CAAC;AAYrC,oBAAgB,MAAM;AACpB,YAAM;AAAA,QACJ;AAAA,QACA;AAAA,MACF,IAAI;AACJ,YAAM,cAAc,SAAS,MAAM;AACjC,cAAMA,WAAU,QAAQ,QAAQ,OAAO,YAAY,aAAa,QAAQ,WAAW,IAAI,OAAO,EAAE,KAAK,iBAAe;AAClH,cAAIA,SAAQ,UAAU;AACpB;AAAA,UACF;AACA,gBAAM,eAAe,YAAY,IAAI,CAAC,YAAY,iBA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