@ifhover/leaf-ui 0.16.0 → 0.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/browser/modules/select/select.cjs +64 -39
- package/dist/cjs/browser/modules/select/select.cjs.map +1 -1
- package/dist/cjs/browser/modules/shared/selection-content.cjs +96 -17
- package/dist/cjs/browser/modules/shared/selection-content.cjs.map +1 -1
- package/dist/cjs/browser/modules/transfer/transfer.cjs +68 -20
- package/dist/cjs/browser/modules/transfer/transfer.cjs.map +1 -1
- package/dist/cjs/select/select.cjs +64 -39
- package/dist/cjs/select/select.cjs.map +1 -1
- package/dist/cjs/select/select.d.cts.map +1 -1
- package/dist/cjs/shared/selection-content.cjs +96 -17
- package/dist/cjs/shared/selection-content.cjs.map +1 -1
- package/dist/cjs/shared/selection-content.d.cts.map +1 -1
- package/dist/cjs/styles/card.css +1 -1
- package/dist/cjs/styles/colorpicker.css +1 -1
- package/dist/cjs/styles/components/card.css +1 -1
- package/dist/cjs/styles/components/tabs.css +1 -1
- package/dist/cjs/styles/daterangepicker.css +1 -1
- package/dist/cjs/styles/index.css +62 -74
- package/dist/cjs/styles/index.css.map +1 -1
- package/dist/cjs/styles/tabs.css +1 -1
- package/dist/cjs/transfer/transfer.cjs +68 -20
- package/dist/cjs/transfer/transfer.cjs.map +1 -1
- package/dist/cjs/transfer/transfer.d.cts.map +1 -1
- package/dist/esm/browser/modules/select/select.js +64 -39
- package/dist/esm/browser/modules/select/select.js.map +1 -1
- package/dist/esm/browser/modules/shared/selection-content.js +96 -17
- package/dist/esm/browser/modules/shared/selection-content.js.map +1 -1
- package/dist/esm/browser/modules/transfer/transfer.js +69 -21
- package/dist/esm/browser/modules/transfer/transfer.js.map +1 -1
- package/dist/esm/select/select.d.ts.map +1 -1
- package/dist/esm/select/select.js +64 -39
- package/dist/esm/select/select.js.map +1 -1
- package/dist/esm/shared/selection-content.d.ts.map +1 -1
- package/dist/esm/shared/selection-content.js +96 -17
- package/dist/esm/shared/selection-content.js.map +1 -1
- package/dist/esm/styles/card.css +1 -1
- package/dist/esm/styles/colorpicker.css +1 -1
- package/dist/esm/styles/components/card.css +1 -1
- package/dist/esm/styles/components/tabs.css +1 -1
- package/dist/esm/styles/daterangepicker.css +1 -1
- package/dist/esm/styles/index.css +62 -74
- package/dist/esm/styles/index.css.map +1 -1
- package/dist/esm/styles/tabs.css +1 -1
- package/dist/esm/transfer/transfer.d.ts.map +1 -1
- package/dist/esm/transfer/transfer.js +69 -21
- package/dist/esm/transfer/transfer.js.map +1 -1
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared/selection-content.js","sources":["../../../../../src/shared/selection-content.tsx"],"sourcesContent":["import { X } from 'lucide-react';\nimport {\n type CSSProperties,\n type ReactNode,\n useEffect,\n useLayoutEffect,\n useRef,\n useState,\n} from 'react';\nimport { classes } from './classes';\nimport { inertProps } from './inert';\nimport { useListMotion } from './motion';\n\nconst useBrowserLayoutEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;\n\ninterface SelectionItem {\n key: string;\n label: ReactNode;\n text: string;\n remove?: () => void;\n render?: ReactNode;\n}\n\n/** Shared selected-value layout. Measure the real tags, without mounting custom renderers twice. */\nexport function SelectionContent({\n items,\n children,\n className,\n tagClassName,\n tagWrap = true,\n maxTagCount,\n searchable = false,\n query = '',\n removeLabel,\n}: {\n items?: readonly SelectionItem[];\n children?: ReactNode;\n className?: string;\n tagClassName?: string;\n tagWrap?: boolean;\n maxTagCount?: number | 'responsive';\n searchable?: boolean;\n query?: string;\n removeLabel?: string;\n}) {\n const root = useRef<HTMLSpanElement>(null);\n const count = items?.length ?? 0;\n const limit = typeof maxTagCount === 'number' ? Math.max(0, Math.floor(maxTagCount)) : count;\n const singleLine = items !== undefined && (!tagWrap || maxTagCount === 'responsive');\n const [layout, setLayout] = useState({ count, firstWidth: 0, inputWidth: 0 });\n const visibleCount = Math.min(count, limit, singleLine ? layout.count : count);\n const hiddenItems = items?.slice(visibleCount) ?? [];\n const measure = useRef(() => {});\n measure.current = () => {\n const element = root.current;\n if (!singleLine || !element?.clientWidth) return;\n const width = element.clientWidth;\n const gap = Number.parseFloat(getComputedStyle(element).columnGap) || 0;\n // All writes precede the reads. The temporary measurement is restored before paint.\n const tags = Array.from(element.querySelectorAll<HTMLElement>('[data-selection-item]'));\n const hiddenTags = tags.filter((tag) => tag.hidden);\n element.dataset.measuring = '';\n // Normalize/utility styles often enforce [hidden] with !important. Temporarily\n // remove the attribute itself instead of relying on a CSS override.\n for (const tag of hiddenTags) tag.hidden = false;\n const widths = tags.map((tag) => tag.getBoundingClientRect().width);\n const overflowWidth =\n element.querySelector<HTMLElement>('[data-selection-counter]')?.getBoundingClientRect()\n .width ?? 0;\n const queryWidth =\n element.querySelector<HTMLElement>('[data-selection-query]')?.getBoundingClientRect().width ??\n 0;\n for (const tag of hiddenTags) tag.hidden = true;\n delete element.dataset.measuring;\n const inputWidth = children\n ? Math.min(\n Math.max(0, width - (count ? overflowWidth + gap : 0)),\n searchable ? (query ? Math.max(48, queryWidth + 12) : 24) : 1,\n )\n : 0;\n const budget = Math.max(0, width - inputWidth - (children && count ? gap : 0));\n const totalWidth = widths.reduce((sum, value) => sum + value, 0) + Math.max(0, count - 1) * gap;\n let nextCount = 0;\n let firstWidth = 0;\n if (count <= limit && totalWidth <= budget) nextCount = count;\n else if (count === 1 && limit > 0 && budget >= Math.min(widths[0] ?? 0, 48)) {\n nextCount = 1;\n firstWidth = budget;\n } else {\n const tagBudget = Math.max(0, budget - overflowWidth - gap);\n let used = 0;\n for (const tagWidth of widths.slice(0, Math.min(limit, count - 1))) {\n const next = used + (nextCount ? gap : 0) + tagWidth;\n if (next > tagBudget) break;\n used = next;\n nextCount++;\n }\n // Keep the first long label useful, while preserving the remove button and +N.\n if (!nextCount && limit > 0 && count > 1 && tagBudget >= Math.min(widths[0] ?? 0, 48)) {\n nextCount = 1;\n firstWidth = tagBudget;\n }\n }\n setLayout((previous) =>\n previous.count === nextCount &&\n previous.firstWidth === firstWidth &&\n previous.inputWidth === inputWidth\n ? previous\n : { count: nextCount, firstWidth, inputWidth },\n );\n };\n useBrowserLayoutEffect(() => measure.current());\n // biome-ignore lint/correctness/useExhaustiveDependencies: Observe the new tag elements after selection changes.\n useEffect(() => {\n if (!singleLine || !root.current) return;\n const update = () => measure.current();\n const observer = typeof ResizeObserver === 'undefined' ? undefined : new ResizeObserver(update);\n observer?.observe(root.current);\n for (const tag of root.current.querySelectorAll('[data-selection-item]'))\n observer?.observe(tag);\n window.addEventListener('resize', update);\n document.fonts?.addEventListener('loadingdone', update);\n return () => {\n observer?.disconnect();\n window.removeEventListener('resize', update);\n document.fonts?.removeEventListener('loadingdone', update);\n };\n }, [singleLine, count]);\n useListMotion(\n root,\n JSON.stringify(items?.slice(0, visibleCount).map((item) => item.key)),\n '.leaf-selection__tag[data-motion-key]:not([hidden])',\n singleLine ? visibleCount : false,\n );\n return (\n <span\n ref={root}\n className={classes('leaf-selection', className)}\n data-multiple={items !== undefined || undefined}\n data-single-line={singleLine || undefined}\n style={\n singleLine\n ? ({\n '--leaf-selection-first-width': layout.firstWidth\n ? `${layout.firstWidth}px`\n : undefined,\n '--leaf-selection-input-width': `${layout.inputWidth}px`,\n } as CSSProperties)\n : undefined\n }\n >\n {items?.slice(0, singleLine ? count : visibleCount).map((item, index) => (\n <span\n className={classes('leaf-selection__tag', tagClassName)}\n key={item.key}\n data-selection-item=\"\"\n data-motion-key={item.key}\n data-custom={item.render != null || undefined}\n hidden={index >= visibleCount}\n {...inertProps(index >= visibleCount)}\n title={item.text}\n >\n {item.render ?? (\n <>\n <span>{item.label}</span>\n {item.remove && (\n <button\n type=\"button\"\n tabIndex={-1}\n aria-label={`${removeLabel} ${item.text}`}\n onMouseDown={(event) => event.preventDefault()}\n onClick={(event) => {\n event.stopPropagation();\n item.remove?.();\n }}\n >\n <X size={12} aria-hidden=\"true\" />\n </button>\n )}\n </>\n )}\n </span>\n ))}\n {hiddenItems.length > 0 && (\n <span\n className={classes('leaf-selection__tag', tagClassName)}\n title={hiddenItems.map((item) => item.text).join(', ')}\n data-selection-overflow=\"\"\n >\n +{hiddenItems.length}\n <span className=\"leaf-selection__sr\">\n {hiddenItems.map((item) => item.text).join(', ')}\n </span>\n </span>\n )}\n {children && <span className=\"leaf-selection__input\">{children}</span>}\n {singleLine && (\n <span className=\"leaf-selection__measure\" aria-hidden=\"true\">\n <span className={classes('leaf-selection__tag', tagClassName)} data-selection-counter=\"\">\n +{count}\n </span>\n <span data-selection-query=\"\">{query}</span>\n </span>\n )}\n </span>\n );\n}\n"],"names":["useBrowserLayoutEffect","window","useEffect","useLayoutEffect","SelectionContent","items","children","className","tagClassName","tagWrap","maxTagCount","searchable","query","removeLabel","root","useRef","count","limit","Math","singleLine","undefined","layout","setLayout","useState","visibleCount","hiddenItems","measure","element","width","gap","Number","getComputedStyle","tags","Array","hiddenTags","tag","widths","overflowWidth","queryWidth","inputWidth","budget","totalWidth","sum","value","nextCount","firstWidth","tagBudget","used","tagWidth","next","previous","update","observer","ResizeObserver","document","useListMotion","JSON","item","classes","index","inertProps","event","X"],"mappings":";;;;;;;AAaA,MAAMA,yBAAyB,AAAkB,MAAlB,OAAOC,SAAyBC,YAAYC;AAWpE,SAASC,iBAAiB,EAC/BC,KAAK,EACLC,QAAQ,EACRC,SAAS,EACTC,YAAY,EACZC,UAAU,IAAI,EACdC,WAAW,EACXC,aAAa,KAAK,EAClBC,QAAQ,EAAE,EACVC,WAAW,EAWZ;IACC,MAAMC,OAAOC,OAAwB;IACrC,MAAMC,QAAQX,OAAO,UAAU;IAC/B,MAAMY,QAAQ,AAAuB,YAAvB,OAAOP,cAA2BQ,KAAK,GAAG,CAAC,GAAGA,KAAK,KAAK,CAACR,gBAAgBM;IACvF,MAAMG,aAAad,AAAUe,WAAVf,SAAwB,EAACI,WAAWC,AAAgB,iBAAhBA,WAA2B;IAClF,MAAM,CAACW,QAAQC,UAAU,GAAGC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C,SAAS,CAACA;oCACRA,MAAM,eAAe;oCACrBJ,KAAK,MAAM;gCACb;0CAEA,kBAACK,GAACA;oCAAC,MAAM;oCAAI,eAAY;;;;;mBAtB5BL,KAAK,GAAG;YA6BhBhC,YAAY,MAAM,GAAG,KAAK,WAAL,GACpB,KAAC;gBACC,WAAWiC,QAAQ,uBAAuBlD;gBAC1C,OAAOiB,YAAY,GAAG,CAAC,CAACgC,OAASA,KAAK,IAAI,EAAE,IAAI,CAAC;gBACjD,2BAAwB;;oBACzB;oBACGhC,YAAY,MAAM;kCACpB,IAAC;wBAAK,WAAU;kCACbA,YAAY,GAAG,CAAC,CAACgC,OAASA,KAAK,IAAI,EAAE,IAAI,CAAC;;;;YAIhDnD,YAAY,WAAZA,GAAY,IAAC;gBAAK,WAAU;0BAAyBA;;YACrDa,cAAc,WAAdA,GACC,KAAC;gBAAK,WAAU;gBAA0B,eAAY;;kCACpD,KAAC;wBAAK,WAAWuC,QAAQ,uBAAuBlD;wBAAe,0BAAuB;;4BAAG;4BACrFQ;;;kCAEJ,IAAC;wBAAK,wBAAqB;kCAAIJ;;;;;;AAKzC"}
|
|
1
|
+
{"version":3,"file":"shared/selection-content.js","sources":["../../../../../src/shared/selection-content.tsx"],"sourcesContent":["import { X } from 'lucide-react';\nimport {\n type CSSProperties,\n type ReactNode,\n useEffect,\n useLayoutEffect,\n useRef,\n useState,\n} from 'react';\nimport { useLeafConfig } from '../config-provider/context';\nimport { classes } from './classes';\nimport { inertProps } from './inert';\nimport { useListMotion } from './motion';\n\nconst useBrowserLayoutEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;\n\ninterface SelectionItem {\n key: string;\n label: ReactNode;\n text: string;\n remove?: () => void;\n render?: ReactNode;\n}\n\n/** Shared selected-value layout. Measure the real tags, without mounting custom renderers twice. */\nexport function SelectionContent({\n items,\n children,\n className,\n tagClassName,\n tagWrap = true,\n maxTagCount,\n searchable = false,\n query = '',\n removeLabel,\n}: {\n items?: readonly SelectionItem[];\n children?: ReactNode;\n className?: string;\n tagClassName?: string;\n tagWrap?: boolean;\n maxTagCount?: number | 'responsive';\n searchable?: boolean;\n query?: string;\n removeLabel?: string;\n}) {\n const root = useRef<HTMLSpanElement>(null);\n const { theme, density } = useLeafConfig();\n const count = items?.length ?? 0;\n const limit = typeof maxTagCount === 'number' ? Math.max(0, Math.floor(maxTagCount)) : count;\n const singleLine = items !== undefined && (!tagWrap || maxTagCount === 'responsive');\n const [layout, setLayout] = useState({ count, firstWidth: 0, inputWidth: 0 });\n const visibleCount = Math.min(count, limit, singleLine ? layout.count : count);\n const hiddenItems = items?.slice(visibleCount) ?? [];\n const widths = useRef(new Map<HTMLElement, number>());\n const tags = useRef<HTMLElement[]>([]);\n const resizeObserver = useRef<ResizeObserver | null>(null);\n const mutationObserver = useRef<MutationObserver | null>(null);\n const frame = useRef<number | null>(null);\n const invalidateContent = useRef((records: MutationRecord[]) => {\n let changed = false;\n for (const record of records) {\n const target = record.target instanceof Element ? record.target : record.target.parentElement;\n const tag = target?.closest<HTMLElement>('[data-selection-item]');\n if (tag && root.current?.contains(tag)) {\n // Showing tags to measure them, and folding them afterwards, do not change their content.\n if (\n record.type === 'attributes' &&\n record.target === tag &&\n (record.attributeName === 'hidden' || record.attributeName === 'inert')\n )\n continue;\n widths.current.delete(tag);\n } else if (record.type === 'attributes' && record.target === root.current) {\n if (record.attributeName !== 'class') continue;\n widths.current.clear();\n }\n changed = true;\n }\n return changed;\n });\n const measure = useRef(() => {});\n measure.current = () => {\n if (frame.current !== null) cancelAnimationFrame(frame.current);\n frame.current = null;\n invalidateContent.current(mutationObserver.current?.takeRecords() ?? []);\n const element = root.current;\n if (!singleLine || !element?.clientWidth) return;\n const width = element.clientWidth;\n const gap = Number.parseFloat(getComputedStyle(element).columnGap) || 0;\n const nextTags = Array.from(element.querySelectorAll<HTMLElement>('[data-selection-item]'));\n const retained = new Set(nextTags);\n for (const tag of tags.current) {\n if (retained.has(tag)) continue;\n resizeObserver.current?.unobserve(tag);\n widths.current.delete(tag);\n }\n const previous = new Set(tags.current);\n for (const tag of nextTags) if (!previous.has(tag)) resizeObserver.current?.observe(tag);\n tags.current = nextTags;\n const missing = nextTags.filter((tag) => !widths.current.has(tag));\n if (missing.length) {\n // Measure only new/invalidated content, on the real nodes, before paint.\n const hiddenTags = missing.filter((tag) => tag.hidden);\n element.dataset.measuring = '';\n for (const tag of hiddenTags) tag.hidden = false;\n for (const tag of missing) widths.current.set(tag, tag.getBoundingClientRect().width);\n for (const tag of hiddenTags) tag.hidden = true;\n delete element.dataset.measuring;\n }\n const tagWidths = nextTags.map((tag) => widths.current.get(tag) ?? 0);\n const overflowWidth =\n element.querySelector<HTMLElement>('[data-selection-counter]')?.getBoundingClientRect()\n .width ?? 0;\n const queryWidth =\n element.querySelector<HTMLElement>('[data-selection-query]')?.getBoundingClientRect().width ??\n 0;\n const inputWidth = children\n ? Math.min(\n Math.max(0, width - (count ? overflowWidth + gap : 0)),\n searchable ? (query ? Math.max(48, queryWidth + 12) : 24) : 1,\n )\n : 0;\n const budget = Math.max(0, width - inputWidth - (children && count ? gap : 0));\n const totalWidth =\n tagWidths.reduce((sum, value) => sum + value, 0) + Math.max(0, count - 1) * gap;\n let nextCount = 0;\n let firstWidth = 0;\n if (count <= limit && totalWidth <= budget) nextCount = count;\n else if (count === 1 && limit > 0 && budget >= Math.min(tagWidths[0] ?? 0, 48)) {\n nextCount = 1;\n firstWidth = budget;\n } else {\n const tagBudget = Math.max(0, budget - overflowWidth - gap);\n let used = 0;\n for (const tagWidth of tagWidths.slice(0, Math.min(limit, count - 1))) {\n const next = used + (nextCount ? gap : 0) + tagWidth;\n if (next > tagBudget) break;\n used = next;\n nextCount++;\n }\n // Keep the first long label useful, while preserving the remove button and +N.\n if (!nextCount && limit > 0 && count > 1 && tagBudget >= Math.min(tagWidths[0] ?? 0, 48)) {\n nextCount = 1;\n firstWidth = tagBudget;\n }\n }\n setLayout((previous) =>\n previous.count === nextCount &&\n previous.firstWidth === firstWidth &&\n previous.inputWidth === inputWidth\n ? previous\n : { count: nextCount, firstWidth, inputWidth },\n );\n };\n useBrowserLayoutEffect(() => {\n widths.current.clear();\n }, [theme, density, className, tagClassName, singleLine]);\n useBrowserLayoutEffect(() => measure.current());\n useBrowserLayoutEffect(() => {\n if (!singleLine || !root.current) return;\n const schedule = () => {\n if (frame.current === null) frame.current = requestAnimationFrame(() => measure.current());\n };\n const update = () => {\n widths.current.clear();\n schedule();\n };\n // Initial delivery only checks layout; later size changes also invalidate widths.\n const delivered = new WeakSet<Element>();\n const observer =\n typeof ResizeObserver === 'undefined'\n ? null\n : new ResizeObserver((entries) => {\n for (const entry of entries) {\n if (delivered.has(entry.target)) {\n if (\n entry.target instanceof HTMLElement &&\n entry.target.hasAttribute('data-selection-item')\n ) {\n // Folding a tag does not invalidate its natural width. A resized\n // visible tag only invalidates itself, not every hidden selection.\n if (!entry.target.hidden) widths.current.delete(entry.target);\n } else widths.current.clear();\n }\n delivered.add(entry.target);\n }\n schedule();\n });\n resizeObserver.current = observer;\n observer?.observe(root.current);\n for (const tag of tags.current) observer?.observe(tag);\n const counter = root.current.querySelector('[data-selection-counter]');\n if (counter) observer?.observe(counter);\n const mutations =\n typeof MutationObserver === 'undefined'\n ? null\n : new MutationObserver((records) => {\n if (invalidateContent.current(records)) schedule();\n });\n mutationObserver.current = mutations;\n mutations?.observe(root.current, {\n subtree: true,\n childList: true,\n characterData: true,\n attributes: true,\n });\n window.addEventListener('resize', update);\n document.fonts?.addEventListener('loadingdone', update);\n return () => {\n if (frame.current !== null) cancelAnimationFrame(frame.current);\n frame.current = null;\n observer?.disconnect();\n mutations?.disconnect();\n resizeObserver.current = null;\n mutationObserver.current = null;\n window.removeEventListener('resize', update);\n document.fonts?.removeEventListener('loadingdone', update);\n };\n }, [singleLine]);\n useListMotion(\n root,\n JSON.stringify(items?.slice(0, visibleCount).map((item) => item.key)),\n '.leaf-selection__tag[data-motion-key]:not([hidden])',\n singleLine ? visibleCount : false,\n );\n return (\n <span\n ref={root}\n className={classes('leaf-selection', className)}\n data-multiple={items !== undefined || undefined}\n data-single-line={singleLine || undefined}\n style={\n singleLine\n ? ({\n '--leaf-selection-first-width': layout.firstWidth\n ? `${layout.firstWidth}px`\n : undefined,\n '--leaf-selection-input-width': `${layout.inputWidth}px`,\n } as CSSProperties)\n : undefined\n }\n >\n {items?.slice(0, singleLine ? count : visibleCount).map((item, index) => (\n <span\n className={classes('leaf-selection__tag', tagClassName)}\n key={item.key}\n data-selection-item=\"\"\n data-motion-key={item.key}\n data-custom={item.render != null || undefined}\n hidden={index >= visibleCount}\n {...inertProps(index >= visibleCount)}\n title={item.text}\n >\n {item.render ?? (\n <>\n <span>{item.label}</span>\n {item.remove && (\n <button\n type=\"button\"\n tabIndex={-1}\n aria-label={`${removeLabel} ${item.text}`}\n onMouseDown={(event) => event.preventDefault()}\n onClick={(event) => {\n event.stopPropagation();\n item.remove?.();\n }}\n >\n <X size={12} aria-hidden=\"true\" />\n </button>\n )}\n </>\n )}\n </span>\n ))}\n {hiddenItems.length > 0 && (\n <span\n className={classes('leaf-selection__tag', tagClassName)}\n title={hiddenItems.map((item) => item.text).join(', ')}\n data-selection-overflow=\"\"\n >\n +{hiddenItems.length}\n <span className=\"leaf-selection__sr\">\n {hiddenItems.map((item) => item.text).join(', ')}\n </span>\n </span>\n )}\n {children && <span className=\"leaf-selection__input\">{children}</span>}\n {singleLine && (\n <span className=\"leaf-selection__measure\" aria-hidden=\"true\">\n <span className={classes('leaf-selection__tag', tagClassName)} data-selection-counter=\"\">\n +{count}\n </span>\n <span data-selection-query=\"\">{query}</span>\n </span>\n )}\n </span>\n );\n}\n"],"names":["useBrowserLayoutEffect","window","useEffect","useLayoutEffect","SelectionContent","items","children","className","tagClassName","tagWrap","maxTagCount","searchable","query","removeLabel","root","useRef","theme","density","useLeafConfig","count","limit","Math","singleLine","undefined","layout","setLayout","useState","visibleCount","hiddenItems","widths","Map","tags","resizeObserver","mutationObserver","frame","invalidateContent","records","changed","record","target","Element","tag","measure","cancelAnimationFrame","element","width","gap","Number","getComputedStyle","nextTags","Array","retained","Set","previous","missing","hiddenTags","tagWidths","overflowWidth","queryWidth","inputWidth","budget","totalWidth","sum","value","nextCount","firstWidth","tagBudget","used","tagWidth","next","schedule","requestAnimationFrame","update","delivered","WeakSet","observer","ResizeObserver","entries","entry","HTMLElement","counter","mutations","MutationObserver","document","useListMotion","JSON","item","classes","index","inertProps","event","X"],"mappings":";;;;;;;;AAcA,MAAMA,yBAAyB,AAAkB,MAAlB,OAAOC,SAAyBC,YAAYC;AAWpE,SAASC,iBAAiB,EAC/BC,KAAK,EACLC,QAAQ,EACRC,SAAS,EACTC,YAAY,EACZC,UAAU,IAAI,EACdC,WAAW,EACXC,aAAa,KAAK,EAClBC,QAAQ,EAAE,EACVC,WAAW,EAWZ;IACC,MAAMC,OAAOC,OAAwB;IACrC,MAAM,EAAEC,KAAK,EAAEC,OAAO,EAAE,GAAGC;IAC3B,MAAMC,QAAQd,OAAO,UAAU;IAC/B,MAAMe,QAAQ,AAAuB,YAAvB,OAAOV,cAA2BW,KAAK,GAAG,CAAC,GAAGA,KAAK,KAAK,CAACX,gBAAgBS;IACvF,MAAMG,aAAajB,AAAUkB,WAAVlB,SAAwB,EAACI,WAAWC,AAAgB,iBAAhBA,WAA2B;IAClF,MAAM,CAACc,QAAQC,UAAU,GAAGC,SAAS;QAAEP;QAAO,YAAY;QAAG,YAAY;IAAE;IAC3E,MAAMQ,eAAeN,KAAK,GAAG,CAACF,OAAOC,OAAOE,aAAaE,OAAO,KAAK,GAAGL;IACxE,MAAMS,cAAcvB,OAAO,MAAMsB,iBAAiB,EAAE;IACpD,MAAME,SAASd,OAAO,IAAIe;IAC1B,MAAMC,OAAOhB,OAAsB,EAAE;IACrC,MAAMiB,iBAAiBjB,OAA8B;IACrD,MAAMkB,mBAAmBlB,OAAgC;IACzD,MAAMmB,QAAQnB,OAAsB;IACpC,MAAMoB,oBAAoBpB,OAAO,CAACqB;QAChC,IAA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BL,KAAK,GAAG;YA6BhB1D,YAAY,MAAM,GAAG,KAAK,WAAL,GACpB,KAAC;gBACC,WAAW2D,QAAQ,uBAAuB/E;gBAC1C,OAAOoB,YAAY,GAAG,CAAC,CAAC0D,OAASA,KAAK,IAAI,EAAE,IAAI,CAAC;gBACjD,2BAAwB;;oBACzB;oBACG1D,YAAY,MAAM;kCACpB,IAAC;wBAAK,WAAU;kCACbA,YAAY,GAAG,CAAC,CAAC0D,OAASA,KAAK,IAAI,EAAE,IAAI,CAAC;;;;YAIhDhF,YAAY,WAAZA,GAAY,IAAC;gBAAK,WAAU;0BAAyBA;;YACrDgB,cAAc,WAAdA,GACC,KAAC;gBAAK,WAAU;gBAA0B,eAAY;;kCACpD,KAAC;wBAAK,WAAWiE,QAAQ,uBAAuB/E;wBAAe,0BAAuB;;4BAAG;4BACrFW;;;kCAEJ,IAAC;wBAAK,wBAAqB;kCAAIP;;;;;;AAKzC"}
|
|
@@ -3,7 +3,7 @@ import '../../../styles/foundation.css';
|
|
|
3
3
|
import '../../../styles/components/transfer.css';
|
|
4
4
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
5
5
|
import { ArrowLeft, ArrowRight, Search } from "lucide-react";
|
|
6
|
-
import { useId, useRef, useState } from "react";
|
|
6
|
+
import { useId, useMemo, useRef, useState } from "react";
|
|
7
7
|
import { ButtonControl } from "../button/button.js";
|
|
8
8
|
import { CheckboxControl } from "../checkbox/checkbox.js";
|
|
9
9
|
import { useComponentProps } from "../config-provider/component-defaults.js";
|
|
@@ -18,8 +18,9 @@ import { FormValue, useFieldValue } from "../shared/field.js";
|
|
|
18
18
|
import { useListMotion } from "../shared/motion.js";
|
|
19
19
|
import { useText } from "../shared/use-text.js";
|
|
20
20
|
import { VirtualListControl } from "../virtuallist/virtuallist.js";
|
|
21
|
+
const empty = [];
|
|
21
22
|
function Transfer(rawProps) {
|
|
22
|
-
const { items, value, defaultValue =
|
|
23
|
+
const { items, value, defaultValue = empty, onChange, selectedKeys, onSelectionChange, titles, searchable = true, filterOption, renderItem, disabled: disabledProp, height = 180, virtual = false, name, form, required, oneWay = false, className, ...props } = useComponentProps('Transfer', rawProps);
|
|
23
24
|
const { theme, density } = useLeafConfig();
|
|
24
25
|
const field = useFormField();
|
|
25
26
|
const disabled = disabledProp || field?.disabled;
|
|
@@ -34,24 +35,69 @@ function Transfer(rawProps) {
|
|
|
34
35
|
'',
|
|
35
36
|
''
|
|
36
37
|
]);
|
|
37
|
-
|
|
38
|
+
const currentKeys = useMemo(()=>new Set(current), [
|
|
39
|
+
current
|
|
40
|
+
]);
|
|
41
|
+
const checkedKeys = useMemo(()=>new Set(selected), [
|
|
42
|
+
selected
|
|
43
|
+
]);
|
|
44
|
+
const searchableItems = useMemo(()=>items.map((item)=>({
|
|
45
|
+
item,
|
|
46
|
+
text: `${item.searchLabel ?? ('string' == typeof item.label ? item.label : item.key)} ${item.description ?? ''}`.toLocaleLowerCase()
|
|
47
|
+
})), [
|
|
48
|
+
items
|
|
49
|
+
]);
|
|
50
|
+
const panels = useMemo(()=>{
|
|
51
|
+
const lists = [
|
|
52
|
+
[],
|
|
53
|
+
[]
|
|
54
|
+
];
|
|
55
|
+
const normalizedQueries = queries.map((query)=>query.toLocaleLowerCase());
|
|
56
|
+
for (const { item, text } of searchableItems){
|
|
57
|
+
const side = currentKeys.has(item.key) ? 1 : 0;
|
|
58
|
+
if (filterOption ? filterOption(queries[side] ?? '', item) : text.includes(normalizedQueries[side] ?? '')) lists[side].push(item);
|
|
59
|
+
}
|
|
60
|
+
return [
|
|
61
|
+
{
|
|
62
|
+
list: lists[0],
|
|
63
|
+
enabled: lists[0].filter((item)=>!item.disabled)
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
list: lists[1],
|
|
67
|
+
enabled: lists[1].filter((item)=>!item.disabled)
|
|
68
|
+
}
|
|
69
|
+
];
|
|
70
|
+
}, [
|
|
71
|
+
searchableItems,
|
|
72
|
+
currentKeys,
|
|
73
|
+
queries,
|
|
74
|
+
filterOption
|
|
75
|
+
]);
|
|
76
|
+
const motionKey = useMemo(()=>virtual ? void 0 : JSON.stringify([
|
|
77
|
+
current,
|
|
78
|
+
queries,
|
|
79
|
+
items.map((item)=>item.key)
|
|
80
|
+
]), [
|
|
81
|
+
virtual,
|
|
38
82
|
current,
|
|
39
83
|
queries,
|
|
40
|
-
items
|
|
41
|
-
])
|
|
84
|
+
items
|
|
85
|
+
]);
|
|
86
|
+
useListMotion(root, motionKey, virtual ? '' : '.leaf-transfer__item[data-motion-key]');
|
|
42
87
|
const select = (keys)=>{
|
|
43
88
|
if (void 0 === selectedKeys) setInternal(keys);
|
|
44
89
|
onSelectionChange?.(keys);
|
|
45
90
|
};
|
|
46
91
|
const move = (direction)=>{
|
|
47
|
-
const moved = items.filter((item)=>!item.disabled &&
|
|
92
|
+
const moved = items.filter((item)=>!item.disabled && checkedKeys.has(item.key) && currentKeys.has(item.key) === ('left' === direction)).map((item)=>item.key);
|
|
48
93
|
if (!moved.length || disabled) return;
|
|
94
|
+
const movedKeys = new Set(moved);
|
|
49
95
|
const next = 'right' === direction ? [
|
|
50
96
|
...current,
|
|
51
97
|
...moved
|
|
52
|
-
] : current.filter((key)=>!
|
|
98
|
+
] : current.filter((key)=>!movedKeys.has(key));
|
|
53
99
|
setCurrent(next);
|
|
54
|
-
select(selected.filter((key)=>!
|
|
100
|
+
select(selected.filter((key)=>!movedKeys.has(key)));
|
|
55
101
|
onChange?.([
|
|
56
102
|
...next
|
|
57
103
|
], {
|
|
@@ -61,16 +107,15 @@ function Transfer(rawProps) {
|
|
|
61
107
|
};
|
|
62
108
|
const panel = (side)=>{
|
|
63
109
|
const query = queries[side] ?? '';
|
|
64
|
-
const list
|
|
65
|
-
const
|
|
66
|
-
const checked = enabled.filter((item)=>selected.includes(item.key));
|
|
110
|
+
const { list, enabled } = panels[side];
|
|
111
|
+
const checkedCount = enabled.reduce((count, item)=>count + Number(checkedKeys.has(item.key)), 0);
|
|
67
112
|
const row = (item)=>/*#__PURE__*/ jsx("div", {
|
|
68
113
|
className: "leaf-transfer__item",
|
|
69
114
|
"data-motion-key": item.key,
|
|
70
|
-
"data-selected":
|
|
115
|
+
"data-selected": checkedKeys.has(item.key) ? '' : void 0,
|
|
71
116
|
children: /*#__PURE__*/ jsx(CheckboxControl, {
|
|
72
117
|
disabled: disabled || item.disabled,
|
|
73
|
-
checked:
|
|
118
|
+
checked: checkedKeys.has(item.key),
|
|
74
119
|
onChange: (event)=>select(event.target.checked ? [
|
|
75
120
|
...new Set([
|
|
76
121
|
...selected,
|
|
@@ -97,14 +142,17 @@ function Transfer(rawProps) {
|
|
|
97
142
|
/*#__PURE__*/ jsx(CheckboxControl, {
|
|
98
143
|
"aria-label": t('transfer.selectAllVisibleItems'),
|
|
99
144
|
disabled: disabled || !enabled.length,
|
|
100
|
-
checked: enabled.length > 0 &&
|
|
101
|
-
indeterminate:
|
|
102
|
-
onChange: (event)=>
|
|
145
|
+
checked: enabled.length > 0 && checkedCount === enabled.length,
|
|
146
|
+
indeterminate: checkedCount > 0 && checkedCount < enabled.length,
|
|
147
|
+
onChange: (event)=>{
|
|
148
|
+
const enabledKeys = new Set(enabled.map((item)=>item.key));
|
|
149
|
+
select(event.target.checked ? [
|
|
103
150
|
...new Set([
|
|
104
151
|
...selected,
|
|
105
|
-
...
|
|
152
|
+
...enabledKeys
|
|
106
153
|
])
|
|
107
|
-
] : selected.filter((key)=>!
|
|
154
|
+
] : selected.filter((key)=>!enabledKeys.has(key)));
|
|
155
|
+
}
|
|
108
156
|
}),
|
|
109
157
|
/*#__PURE__*/ jsx("span", {
|
|
110
158
|
id: `${id}-${side}`,
|
|
@@ -112,7 +160,7 @@ function Transfer(rawProps) {
|
|
|
112
160
|
}),
|
|
113
161
|
/*#__PURE__*/ jsxs("small", {
|
|
114
162
|
children: [
|
|
115
|
-
|
|
163
|
+
checkedCount ? `${checkedCount} / ` : '',
|
|
116
164
|
list.length
|
|
117
165
|
]
|
|
118
166
|
})
|
|
@@ -183,7 +231,7 @@ function Transfer(rawProps) {
|
|
|
183
231
|
size: 16
|
|
184
232
|
}),
|
|
185
233
|
"aria-label": t('transfer.moveToSelected'),
|
|
186
|
-
disabled: disabled || !items.some((item)=>!item.disabled &&
|
|
234
|
+
disabled: disabled || !items.some((item)=>!item.disabled && checkedKeys.has(item.key) && !currentKeys.has(item.key)),
|
|
187
235
|
onClick: ()=>move('right')
|
|
188
236
|
}),
|
|
189
237
|
!oneWay && /*#__PURE__*/ jsx(ButtonControl, {
|
|
@@ -193,7 +241,7 @@ function Transfer(rawProps) {
|
|
|
193
241
|
size: 16
|
|
194
242
|
}),
|
|
195
243
|
"aria-label": t('transfer.moveToAvailable'),
|
|
196
|
-
disabled: disabled || !items.some((item)=>!item.disabled &&
|
|
244
|
+
disabled: disabled || !items.some((item)=>!item.disabled && checkedKeys.has(item.key) && currentKeys.has(item.key)),
|
|
197
245
|
onClick: ()=>move('left')
|
|
198
246
|
})
|
|
199
247
|
]
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transfer/transfer.js","sources":["../../../../../src/transfer/transfer.tsx"],"sourcesContent":["import { ArrowLeft, ArrowRight, Search } from 'lucide-react';\nimport { type HTMLAttributes, type ReactNode, useId, useRef, useState } from 'react';\nimport { ButtonControl } from '../button/button';\nimport { CheckboxControl as Checkbox } from '../checkbox/checkbox';\nimport { useComponentProps } from '../config-provider/component-defaults';\nimport { useLeafConfig } from '../config-provider/context';\nimport { estimateControlHeight } from '../density';\nimport { FieldScope, useFormField } from '../form/form';\nimport { InputControl } from '../input/input';\nimport { ResultControl as Result } from '../result/result';\nimport { ScrollbarControl } from '../scrollbar/scrollbar';\nimport { classes } from '../shared/classes';\nimport { FormValue, useFieldValue } from '../shared/field';\nimport { useListMotion } from '../shared/motion';\nimport { useText } from '../shared/use-text';\nimport { VirtualListControl as VirtualList } from '../virtuallist/virtuallist';\nexport interface TransferItem {\n key: string;\n label: ReactNode;\n description?: string;\n disabled?: boolean;\n searchLabel?: string;\n}\nexport interface TransferChangeInfo {\n direction: 'left' | 'right';\n movedKeys: string[];\n}\nexport interface TransferProps extends Omit<HTMLAttributes<HTMLDivElement>, 'onChange'> {\n items: readonly TransferItem[];\n value?: readonly string[];\n defaultValue?: readonly string[];\n onChange?: (keys: string[], info: TransferChangeInfo) => void;\n selectedKeys?: readonly string[];\n onSelectionChange?: (keys: string[]) => void;\n titles?: readonly [ReactNode, ReactNode];\n searchable?: boolean;\n filterOption?: (query: string, item: TransferItem) => boolean;\n renderItem?: (item: TransferItem) => ReactNode;\n disabled?: boolean;\n height?: number;\n virtual?: boolean;\n name?: string;\n form?: string;\n required?: boolean;\n oneWay?: boolean;\n}\nexport function Transfer(rawProps: TransferProps) {\n const {\n items,\n value,\n defaultValue = [],\n onChange,\n selectedKeys,\n onSelectionChange,\n titles,\n searchable = true,\n filterOption,\n renderItem,\n disabled: disabledProp,\n height = 180,\n virtual = false,\n name,\n form,\n required,\n oneWay = false,\n className,\n ...props\n } = useComponentProps('Transfer', rawProps);\n const { theme, density } = useLeafConfig();\n const field = useFormField();\n const disabled = disabledProp || field?.disabled;\n const t = useText();\n const id = useId();\n const trigger = useRef<HTMLButtonElement>(null);\n const root = useRef<HTMLDivElement>(null);\n const [current, setCurrent] = useFieldValue(value, defaultValue, trigger, form);\n const [internal, setInternal] = useState<readonly string[]>([]);\n const selected = selectedKeys ?? internal;\n const [queries, setQueries] = useState(['', '']);\n useListMotion(\n root,\n JSON.stringify([current, queries, items.map((item) => item.key)]),\n virtual ? '[data-no-transfer-motion]' : '.leaf-transfer__item[data-motion-key]',\n );\n const select = (keys: string[]) => {\n if (selectedKeys === undefined) setInternal(keys);\n onSelectionChange?.(keys);\n };\n const move = (direction: 'left' | 'right') => {\n const moved = items\n .filter(\n (item) =>\n !item.disabled &&\n selected.includes(item.key) &&\n current.includes(item.key) === (direction === 'left'),\n )\n .map((item) => item.key);\n if (!moved.length || disabled) return;\n const next =\n direction === 'right'\n ? [...current, ...moved]\n : current.filter((key) => !moved.includes(key));\n setCurrent(next);\n select(selected.filter((key) => !moved.includes(key)));\n onChange?.([...next], { direction, movedKeys: moved });\n };\n const panel = (side: 0 | 1) => {\n const query = queries[side] ?? '';\n const list = items.filter(\n (item) =>\n current.includes(item.key) === (side === 1) &&\n (filterOption\n ? filterOption(query, item)\n : `${item.searchLabel ?? (typeof item.label === 'string' ? item.label : item.key)} ${item.description ?? ''}`\n .toLocaleLowerCase()\n .includes(query.toLocaleLowerCase())),\n );\n const enabled = list.filter((item) => !item.disabled);\n const checked = enabled.filter((item) => selected.includes(item.key));\n const row = (item: TransferItem) => (\n <div\n className=\"leaf-transfer__item\"\n data-motion-key={item.key}\n data-selected={selected.includes(item.key) ? '' : undefined}\n >\n <Checkbox\n disabled={disabled || item.disabled}\n checked={selected.includes(item.key)}\n onChange={(event) =>\n select(\n event.target.checked\n ? [...new Set([...selected, item.key])]\n : selected.filter((key) => key !== item.key),\n )\n }\n >\n <span>\n {renderItem?.(item) ?? item.label}\n {item.description && <small>{item.description}</small>}\n </span>\n </Checkbox>\n </div>\n );\n return (\n <section className=\"leaf-transfer__panel\" aria-labelledby={`${id}-${side}`}>\n <header className=\"leaf-transfer__header\">\n <Checkbox\n aria-label={t('transfer.selectAllVisibleItems')}\n disabled={disabled || !enabled.length}\n checked={enabled.length > 0 && checked.length === enabled.length}\n indeterminate={checked.length > 0 && checked.length < enabled.length}\n onChange={(event) =>\n select(\n event.target.checked\n ? [...new Set([...selected, ...enabled.map((item) => item.key)])]\n : selected.filter((key) => !enabled.some((item) => item.key === key)),\n )\n }\n />\n <span id={`${id}-${side}`}>\n {titles?.[side] ?? (side === 0 ? t('transfer.available') : t('transfer.selected'))}\n </span>\n <small>\n {checked.length ? `${checked.length} / ` : ''}\n {list.length}\n </small>\n </header>\n <div className=\"leaf-transfer__body\">\n {searchable && (\n <div className=\"leaf-transfer__search\">\n <InputControl\n size=\"sm\"\n allowClear\n prefix={<Search size={14} />}\n aria-label={\n side === 0\n ? t('transfer.searchAvailableItems')\n : t('transfer.searchSelectedItems')\n }\n value={query}\n onChange={(event) =>\n setQueries((previous) =>\n previous.map((text, index) => (index === side ? event.target.value : text)),\n )\n }\n />\n </div>\n )}\n {virtual ? (\n <VirtualList\n items={list}\n itemKey={(item) => item.key}\n renderItem={row}\n height={height}\n estimateSize={(item) =>\n Math.max(estimateControlHeight(theme, density), item.description ? 42 : 0) + 6\n }\n emptyContent={<Result size=\"sm\" icon={null} title={t('transfer.noItems')} />}\n />\n ) : (\n <ScrollbarControl\n viewportClassName=\"leaf-transfer__list\"\n height={height}\n role=\"presentation\"\n >\n {list.length ? (\n list.map((item) => <div key={item.key}>{row(item)}</div>)\n ) : (\n <Result size=\"sm\" icon={null} title={t('transfer.noItems')} />\n )}\n </ScrollbarControl>\n )}\n </div>\n </section>\n );\n };\n return (\n <div\n {...props}\n ref={root}\n className={classes('leaf-transfer', className)}\n data-disabled={disabled ? '' : undefined}\n >\n <FieldScope>{panel(0)}</FieldScope>\n <div className=\"leaf-transfer__actions\">\n <ButtonControl\n ref={trigger}\n id={props.id ?? field?.id}\n size=\"sm\"\n variant=\"solid\"\n startIcon={<ArrowRight size={16} />}\n aria-label={t('transfer.moveToSelected')}\n disabled={\n disabled ||\n !items.some(\n (item) =>\n !item.disabled && selected.includes(item.key) && !current.includes(item.key),\n )\n }\n onClick={() => move('right')}\n />\n {!oneWay && (\n <ButtonControl\n size=\"sm\"\n variant=\"outline\"\n startIcon={<ArrowLeft size={16} />}\n aria-label={t('transfer.moveToAvailable')}\n disabled={\n disabled ||\n !items.some(\n (item) =>\n !item.disabled && selected.includes(item.key) && current.includes(item.key),\n )\n }\n onClick={() => move('left')}\n />\n )}\n </div>\n <FieldScope>{panel(1)}</FieldScope>\n <FormValue\n name={name}\n form={form}\n value={current.length ? JSON.stringify(current) : ''}\n required={required ?? field?.required}\n disabled={disabled}\n triggerRef={trigger}\n />\n </div>\n );\n}\n"],"names":["Transfer","rawProps","items","value","defaultValue","onChange","selectedKeys","onSelectionChange","titles","searchable","filterOption","renderItem","disabledProp","height","virtual","name","form","required","oneWay","className","props","useComponentProps","theme","density","useLeafConfig","field","useFormField","disabled","t","useText","id","useId","trigger","useRef","root","current","setCurrent","useFieldValue","internal","setInternal","useState","selected","queries","setQueries","useListMotion","JSON","item","select","keys","undefined","move","direction","moved","next","key","panel","side","query","list","enabled","checked","row","Checkbox","event","Set","InputControl","Search","previous","text","index","VirtualList","Math","estimateControlHeight","Result","ScrollbarControl","classes","FieldScope","ButtonControl","ArrowRight","ArrowLeft","FormValue"],"mappings":";;;;;;;;;;;;;;;;;;;;AA8CO,SAASA,SAASC,QAAuB;IAC9C,MAAM,EACJC,KAAK,EACLC,KAAK,EACLC,eAAe,EAAE,EACjBC,QAAQ,EACRC,YAAY,EACZC,iBAAiB,EACjBC,MAAM,EACNC,aAAa,IAAI,EACjBC,YAAY,EACZC,UAAU,EACV,UAAUC,YAAY,EACtBC,SAAS,GAAG,EACZC,UAAU,KAAK,EACfC,IAAI,EACJC,IAAI,EACJC,QAAQ,EACRC,SAAS,KAAK,EACdC,SAAS,EACT,GAAGC,OACJ,GAAGC,kBAAkB,YAAYpB;IAClC,MAAM,EAAEqB,KAAK,EAAEC,OAAO,EAAE,GAAGC;IAC3B,MAAMC,QAAQC;IACd,MAAMC,WAAWf,gBAAgBa,OAAO;IACxC,MAAMG,IAAIC;IACV,MAAMC,KAAKC;IACX,MAAMC,UAAUC,OAA0B;IAC1C,MAAMC,OAAOD,OAAuB;IACpC,MAAM,CAACE,SAASC,WAAW,GAAGC,cAAclC,OAAOC,cAAc4B,SAAShB;IAC1E,MAAM,CAACsB,UAAUC,YAAY,GAAGC,SAA4B,EAAE;IAC9D,MAAMC,WAAWnC,gBAAgBgC;IACjC,MAAM,CAACI,SAASC,WAAW,GAAGH,SAAS;QAAC;QAAI;KAAG;IAC/CI,cACEV,MACAW,KAAK,SAAS,CAAC;QAACV;QAASO;QAASxC,MAAM,GAAG,CAAC,CAAC4C,OAASA,KAAK,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AP,GACE,KAAC;QACE,GAAGR,KAAK;QACT,KAAKc;QACL,WAAWyC,QAAQ,iBAAiBxD;QACpC,iBAAeQ,WAAW,KAAKsB;;0BAE/B,IAAC2B,YAAUA;0BAAErB,MAAM;;0BACnB,KAAC;gBAAI,WAAU;;kCACb,IAACsB,eAAaA;wBACZ,KAAK7C;wBACL,IAAIZ,MAAM,EAAE,IAAIK,OAAO;wBACvB,MAAK;wBACL,SAAQ;wBACR,yBAAW,IAACqD,YAAUA;4BAAC,MAAM;;wBAC7B,cAAYlD,EAAE;wBACd,UACED,YACA,CAACzB,MAAM,IAAI,CACT,CAAC4C,OACC,CAACA,KAAK,QAAQ,IAAIL,SAAS,QAAQ,CAACK,KAAK,GAAG,KAAK,CAACX,QAAQ,QAAQ,CAACW,KAAK,GAAG;wBAGjF,SAAS,IAAMI,KAAK;;oBAErB,CAAChC,UAAU,WAAVA,GACA,IAAC2D,eAAaA;wBACZ,MAAK;wBACL,SAAQ;wBACR,yBAAW,IAACE,WAASA;4BAAC,MAAM;;wBAC5B,cAAYnD,EAAE;wBACd,UACED,YACA,CAACzB,MAAM,IAAI,CACT,CAAC4C,OACC,CAACA,KAAK,QAAQ,IAAIL,SAAS,QAAQ,CAACK,KAAK,GAAG,KAAKX,QAAQ,QAAQ,CAACW,KAAK,GAAG;wBAGhF,SAAS,IAAMI,KAAK;;;;0BAI1B,IAAC0B,YAAUA;0BAAErB,MAAM;;0BACnB,IAACyB,WAASA;gBACR,MAAMjE;gBACN,MAAMC;gBACN,OAAOmB,QAAQ,MAAM,GAAGU,KAAK,SAAS,CAACV,WAAW;gBAClD,UAAUlB,YAAYQ,OAAO;gBAC7B,UAAUE;gBACV,YAAYK;;;;AAIpB"}
|
|
1
|
+
{"version":3,"file":"transfer/transfer.js","sources":["../../../../../src/transfer/transfer.tsx"],"sourcesContent":["import { ArrowLeft, ArrowRight, Search } from 'lucide-react';\nimport { type HTMLAttributes, type ReactNode, useId, useMemo, useRef, useState } from 'react';\nimport { ButtonControl } from '../button/button';\nimport { CheckboxControl as Checkbox } from '../checkbox/checkbox';\nimport { useComponentProps } from '../config-provider/component-defaults';\nimport { useLeafConfig } from '../config-provider/context';\nimport { estimateControlHeight } from '../density';\nimport { FieldScope, useFormField } from '../form/form';\nimport { InputControl } from '../input/input';\nimport { ResultControl as Result } from '../result/result';\nimport { ScrollbarControl } from '../scrollbar/scrollbar';\nimport { classes } from '../shared/classes';\nimport { FormValue, useFieldValue } from '../shared/field';\nimport { useListMotion } from '../shared/motion';\nimport { useText } from '../shared/use-text';\nimport { VirtualListControl as VirtualList } from '../virtuallist/virtuallist';\nexport interface TransferItem {\n key: string;\n label: ReactNode;\n description?: string;\n disabled?: boolean;\n searchLabel?: string;\n}\nexport interface TransferChangeInfo {\n direction: 'left' | 'right';\n movedKeys: string[];\n}\nexport interface TransferProps extends Omit<HTMLAttributes<HTMLDivElement>, 'onChange'> {\n items: readonly TransferItem[];\n value?: readonly string[];\n defaultValue?: readonly string[];\n onChange?: (keys: string[], info: TransferChangeInfo) => void;\n selectedKeys?: readonly string[];\n onSelectionChange?: (keys: string[]) => void;\n titles?: readonly [ReactNode, ReactNode];\n searchable?: boolean;\n filterOption?: (query: string, item: TransferItem) => boolean;\n renderItem?: (item: TransferItem) => ReactNode;\n disabled?: boolean;\n height?: number;\n virtual?: boolean;\n name?: string;\n form?: string;\n required?: boolean;\n oneWay?: boolean;\n}\nconst empty: readonly string[] = [];\nexport function Transfer(rawProps: TransferProps) {\n const {\n items,\n value,\n defaultValue = empty,\n onChange,\n selectedKeys,\n onSelectionChange,\n titles,\n searchable = true,\n filterOption,\n renderItem,\n disabled: disabledProp,\n height = 180,\n virtual = false,\n name,\n form,\n required,\n oneWay = false,\n className,\n ...props\n } = useComponentProps('Transfer', rawProps);\n const { theme, density } = useLeafConfig();\n const field = useFormField();\n const disabled = disabledProp || field?.disabled;\n const t = useText();\n const id = useId();\n const trigger = useRef<HTMLButtonElement>(null);\n const root = useRef<HTMLDivElement>(null);\n const [current, setCurrent] = useFieldValue(value, defaultValue, trigger, form);\n const [internal, setInternal] = useState<readonly string[]>([]);\n const selected = selectedKeys ?? internal;\n const [queries, setQueries] = useState(['', '']);\n const currentKeys = useMemo(() => new Set(current), [current]);\n const checkedKeys = useMemo(() => new Set(selected), [selected]);\n const searchableItems = useMemo(\n () =>\n items.map((item) => ({\n item,\n text: `${item.searchLabel ?? (typeof item.label === 'string' ? item.label : item.key)} ${item.description ?? ''}`.toLocaleLowerCase(),\n })),\n [items],\n );\n const panels = useMemo(() => {\n const lists: [TransferItem[], TransferItem[]] = [[], []];\n const normalizedQueries = queries.map((query) => query.toLocaleLowerCase());\n for (const { item, text } of searchableItems) {\n const side = currentKeys.has(item.key) ? 1 : 0;\n if (\n filterOption\n ? filterOption(queries[side] ?? '', item)\n : text.includes(normalizedQueries[side] ?? '')\n )\n lists[side].push(item);\n }\n return [\n { list: lists[0], enabled: lists[0].filter((item) => !item.disabled) },\n { list: lists[1], enabled: lists[1].filter((item) => !item.disabled) },\n ] as const;\n }, [searchableItems, currentKeys, queries, filterOption]);\n const motionKey = useMemo(\n () => (virtual ? undefined : JSON.stringify([current, queries, items.map((item) => item.key)])),\n [virtual, current, queries, items],\n );\n useListMotion(root, motionKey, virtual ? '' : '.leaf-transfer__item[data-motion-key]');\n const select = (keys: string[]) => {\n if (selectedKeys === undefined) setInternal(keys);\n onSelectionChange?.(keys);\n };\n const move = (direction: 'left' | 'right') => {\n const moved = items\n .filter(\n (item) =>\n !item.disabled &&\n checkedKeys.has(item.key) &&\n currentKeys.has(item.key) === (direction === 'left'),\n )\n .map((item) => item.key);\n if (!moved.length || disabled) return;\n const movedKeys = new Set(moved);\n const next =\n direction === 'right' ? [...current, ...moved] : current.filter((key) => !movedKeys.has(key));\n setCurrent(next);\n select(selected.filter((key) => !movedKeys.has(key)));\n onChange?.([...next], { direction, movedKeys: moved });\n };\n const panel = (side: 0 | 1) => {\n const query = queries[side] ?? '';\n const { list, enabled } = panels[side];\n const checkedCount = enabled.reduce(\n (count, item) => count + Number(checkedKeys.has(item.key)),\n 0,\n );\n const row = (item: TransferItem) => (\n <div\n className=\"leaf-transfer__item\"\n data-motion-key={item.key}\n data-selected={checkedKeys.has(item.key) ? '' : undefined}\n >\n <Checkbox\n disabled={disabled || item.disabled}\n checked={checkedKeys.has(item.key)}\n onChange={(event) =>\n select(\n event.target.checked\n ? [...new Set([...selected, item.key])]\n : selected.filter((key) => key !== item.key),\n )\n }\n >\n <span>\n {renderItem?.(item) ?? item.label}\n {item.description && <small>{item.description}</small>}\n </span>\n </Checkbox>\n </div>\n );\n return (\n <section className=\"leaf-transfer__panel\" aria-labelledby={`${id}-${side}`}>\n <header className=\"leaf-transfer__header\">\n <Checkbox\n aria-label={t('transfer.selectAllVisibleItems')}\n disabled={disabled || !enabled.length}\n checked={enabled.length > 0 && checkedCount === enabled.length}\n indeterminate={checkedCount > 0 && checkedCount < enabled.length}\n onChange={(event) => {\n const enabledKeys = new Set(enabled.map((item) => item.key));\n select(\n event.target.checked\n ? [...new Set([...selected, ...enabledKeys])]\n : selected.filter((key) => !enabledKeys.has(key)),\n );\n }}\n />\n <span id={`${id}-${side}`}>\n {titles?.[side] ?? (side === 0 ? t('transfer.available') : t('transfer.selected'))}\n </span>\n <small>\n {checkedCount ? `${checkedCount} / ` : ''}\n {list.length}\n </small>\n </header>\n <div className=\"leaf-transfer__body\">\n {searchable && (\n <div className=\"leaf-transfer__search\">\n <InputControl\n size=\"sm\"\n allowClear\n prefix={<Search size={14} />}\n aria-label={\n side === 0\n ? t('transfer.searchAvailableItems')\n : t('transfer.searchSelectedItems')\n }\n value={query}\n onChange={(event) =>\n setQueries((previous) =>\n previous.map((text, index) => (index === side ? event.target.value : text)),\n )\n }\n />\n </div>\n )}\n {virtual ? (\n <VirtualList\n items={list}\n itemKey={(item) => item.key}\n renderItem={row}\n height={height}\n estimateSize={(item) =>\n Math.max(estimateControlHeight(theme, density), item.description ? 42 : 0) + 6\n }\n emptyContent={<Result size=\"sm\" icon={null} title={t('transfer.noItems')} />}\n />\n ) : (\n <ScrollbarControl\n viewportClassName=\"leaf-transfer__list\"\n height={height}\n role=\"presentation\"\n >\n {list.length ? (\n list.map((item) => <div key={item.key}>{row(item)}</div>)\n ) : (\n <Result size=\"sm\" icon={null} title={t('transfer.noItems')} />\n )}\n </ScrollbarControl>\n )}\n </div>\n </section>\n );\n };\n return (\n <div\n {...props}\n ref={root}\n className={classes('leaf-transfer', className)}\n data-disabled={disabled ? '' : undefined}\n >\n <FieldScope>{panel(0)}</FieldScope>\n <div className=\"leaf-transfer__actions\">\n <ButtonControl\n ref={trigger}\n id={props.id ?? field?.id}\n size=\"sm\"\n variant=\"solid\"\n startIcon={<ArrowRight size={16} />}\n aria-label={t('transfer.moveToSelected')}\n disabled={\n disabled ||\n !items.some(\n (item) => !item.disabled && checkedKeys.has(item.key) && !currentKeys.has(item.key),\n )\n }\n onClick={() => move('right')}\n />\n {!oneWay && (\n <ButtonControl\n size=\"sm\"\n variant=\"outline\"\n startIcon={<ArrowLeft size={16} />}\n aria-label={t('transfer.moveToAvailable')}\n disabled={\n disabled ||\n !items.some(\n (item) => !item.disabled && checkedKeys.has(item.key) && currentKeys.has(item.key),\n )\n }\n onClick={() => move('left')}\n />\n )}\n </div>\n <FieldScope>{panel(1)}</FieldScope>\n <FormValue\n name={name}\n form={form}\n value={current.length ? JSON.stringify(current) : ''}\n required={required ?? field?.required}\n disabled={disabled}\n triggerRef={trigger}\n />\n </div>\n );\n}\n"],"names":["empty","Transfer","rawProps","items","value","defaultValue","onChange","selectedKeys","onSelectionChange","titles","searchable","filterOption","renderItem","disabledProp","height","virtual","name","form","required","oneWay","className","props","useComponentProps","theme","density","useLeafConfig","field","useFormField","disabled","t","useText","id","useId","trigger","useRef","root","current","setCurrent","useFieldValue","internal","setInternal","useState","selected","queries","setQueries","currentKeys","useMemo","Set","checkedKeys","searchableItems","item","panels","lists","normalizedQueries","query","text","side","motionKey","undefined","JSON","useListMotion","select","keys","move","direction","moved","movedKeys","next","key","panel","list","enabled","checkedCount","count","Number","row","Checkbox","event","enabledKeys","InputControl","Search","previous","index","VirtualList","Math","estimateControlHeight","Result","ScrollbarControl","classes","FieldScope","ButtonControl","ArrowRight","ArrowLeft","FormValue"],"mappings":";;;;;;;;;;;;;;;;;;;;AA8CA,MAAMA,QAA2B,EAAE;AAC5B,SAASC,SAASC,QAAuB;IAC9C,MAAM,EACJC,KAAK,EACLC,KAAK,EACLC,eAAeL,KAAK,EACpBM,QAAQ,EACRC,YAAY,EACZC,iBAAiB,EACjBC,MAAM,EACNC,aAAa,IAAI,EACjBC,YAAY,EACZC,UAAU,EACV,UAAUC,YAAY,EACtBC,SAAS,GAAG,EACZC,UAAU,KAAK,EACfC,IAAI,EACJC,IAAI,EACJC,QAAQ,EACRC,SAAS,KAAK,EACdC,SAAS,EACT,GAAGC,OACJ,GAAGC,kBAAkB,YAAYpB;IAClC,MAAM,EAAEqB,KAAK,EAAEC,OAAO,EAAE,GAAGC;IAC3B,MAAMC,QAAQC;IACd,MAAMC,WAAWf,gBAAgBa,OAAO;IACxC,MAAMG,IAAIC;IACV,MAAMC,KAAKC;IACX,MAAMC,UAAUC,OAA0B;IAC1C,MAAMC,OAAOD,OAAuB;IACpC,MAAM,CAACE,SAASC,WAAW,GAAGC,cAAclC,OAAOC,cAAc4B,SAAShB;IAC1E,MAAM,CAACsB,UAAUC,YAAY,GAAGC,SAA4B,EAAE;IAC9D,MAAMC,WAAWnC,gBAAgBgC;IACjC,MAAM,CAACI,SAASC,WAAW,GAAGH,SAAS;QAAC;QAAI;KAAG;IAC/C,MAAMI,cAAcC,QAAQ,IAAM,IAAIC,IAAIX,UAAU;QAACA;KAAQ;IAC7D,MAAMY,cAAcF,QAAQ,IAAM,IAAIC,IAAIL,WAAW;QAACA;KAAS;IAC/D,MAAMO,kBAAkBH,QACtB,IACE3C,MAAM,GAAG,CAAC,CAAC+C,OAAU;gBACnBA;gBACA,MAAM,GAAGA,KAAK,WAAW,IAA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AAC,KAAK,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE,YAAY,EAAE,KAAK,IAAI,CAAC;CAC/D,CACJ,CAAC;AAOJ,yFAAyF;AACzF,eAAO,MAAM,aAAa,0GAwnBxB,CAAC;AAGH,eAAO,MAAM,MAAM,0GAGjB,CAAC"}
|
|
@@ -28,13 +28,24 @@ const select_SelectControl = /*#__PURE__*/ forwardRef(function(props, forwardedR
|
|
|
28
28
|
]), [
|
|
29
29
|
suppliedOptions
|
|
30
30
|
]);
|
|
31
|
-
const options = useMemo(()=>
|
|
31
|
+
const options = useMemo(()=>{
|
|
32
|
+
if (!created.length) return baseOptions;
|
|
33
|
+
const baseValues = new Set(baseOptions.map((option)=>option.value));
|
|
34
|
+
return [
|
|
32
35
|
...baseOptions,
|
|
33
|
-
...created.filter((entry)=>!
|
|
34
|
-
]
|
|
36
|
+
...created.filter((entry)=>!baseValues.has(entry.value))
|
|
37
|
+
];
|
|
38
|
+
}, [
|
|
35
39
|
baseOptions,
|
|
36
40
|
created
|
|
37
41
|
]);
|
|
42
|
+
const optionIndex = useMemo(()=>{
|
|
43
|
+
const index = new Map();
|
|
44
|
+
for (const option of options)if (!index.has(option.value)) index.set(option.value, option);
|
|
45
|
+
return index;
|
|
46
|
+
}, [
|
|
47
|
+
options
|
|
48
|
+
]);
|
|
38
49
|
const optionGroups = useMemo(()=>new Map(suppliedOptions.flatMap((entry)=>'options' in entry ? entry.options.map((option)=>[
|
|
39
50
|
option.value,
|
|
40
51
|
entry.label
|
|
@@ -54,15 +65,20 @@ const select_SelectControl = /*#__PURE__*/ forwardRef(function(props, forwardedR
|
|
|
54
65
|
const merged = useMergedRef(input, forwardedRef);
|
|
55
66
|
const panelId = `${useId()}-listbox`;
|
|
56
67
|
const [selected, setSelected] = useFieldValue(value, defaultValue ?? (multiple ? empty : ''), input, form);
|
|
57
|
-
const values = 'string' == typeof selected ? selected ? [
|
|
68
|
+
const values = useMemo(()=>'string' == typeof selected ? selected ? [
|
|
69
|
+
selected
|
|
70
|
+
] : [] : selected, [
|
|
58
71
|
selected
|
|
59
|
-
]
|
|
72
|
+
]);
|
|
73
|
+
const selectedValues = useMemo(()=>new Set(values), [
|
|
74
|
+
values
|
|
75
|
+
]);
|
|
60
76
|
const atLimit = multiple && void 0 !== maxCount && values.length >= Math.max(0, Math.floor(maxCount));
|
|
61
|
-
const unavailable = (option)=>option.disabled || atLimit && !
|
|
77
|
+
const unavailable = (option)=>option.disabled || atLimit && !selectedValues.has(option.value);
|
|
62
78
|
const selectedCache = useRef(new Map());
|
|
63
79
|
const nextCache = new Map();
|
|
64
80
|
for (const key of values){
|
|
65
|
-
const record =
|
|
81
|
+
const record = optionIndex.get(key) ?? selectedCache.current.get(key);
|
|
66
82
|
if (record) nextCache.set(key, record);
|
|
67
83
|
}
|
|
68
84
|
selectedCache.current = nextCache;
|
|
@@ -74,16 +90,28 @@ const select_SelectControl = /*#__PURE__*/ forwardRef(function(props, forwardedR
|
|
|
74
90
|
const searchQuery = query ?? '';
|
|
75
91
|
const [open, setOpen] = usePopupState(disabled || inputProps.readOnly, onOpenChange, controlledOpen, defaultOpen);
|
|
76
92
|
const [highlighted, setHighlighted] = useState(-1);
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
93
|
+
const { createOption, filtered } = useMemo(()=>{
|
|
94
|
+
const normalizedQuery = searchQuery.toLocaleLowerCase();
|
|
95
|
+
const matched = searchQuery && showSearch && false !== filterOption ? options.filter((option)=>'function' == typeof filterOption ? filterOption(searchQuery, option) : searchText(option).toLocaleLowerCase().includes(normalizedQuery)) : options;
|
|
96
|
+
const newValue = searchQuery.trim();
|
|
97
|
+
const createOption = allowCreate && newValue && !options.some((option)=>option.value === newValue || searchText(option) === newValue) ? {
|
|
98
|
+
value: newValue,
|
|
99
|
+
label: newValue
|
|
100
|
+
} : null;
|
|
101
|
+
return {
|
|
102
|
+
createOption,
|
|
103
|
+
filtered: createOption ? [
|
|
104
|
+
...matched,
|
|
105
|
+
createOption
|
|
106
|
+
] : matched
|
|
107
|
+
};
|
|
108
|
+
}, [
|
|
109
|
+
options,
|
|
110
|
+
searchQuery,
|
|
111
|
+
showSearch,
|
|
112
|
+
filterOption,
|
|
113
|
+
allowCreate
|
|
114
|
+
]);
|
|
87
115
|
const rows = useMemo(()=>{
|
|
88
116
|
const entries = [];
|
|
89
117
|
let previousGroup;
|
|
@@ -151,26 +179,23 @@ const select_SelectControl = /*#__PURE__*/ forwardRef(function(props, forwardedR
|
|
|
151
179
|
const change = (next)=>{
|
|
152
180
|
if (disabled || inputProps.readOnly) return;
|
|
153
181
|
setSelected(multiple ? next : next[0] ?? '');
|
|
154
|
-
if (props.multiple)
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
createOption
|
|
167
|
-
] : []
|
|
168
|
-
].find((option)=>option.value === next[0]));
|
|
182
|
+
if (props.multiple) {
|
|
183
|
+
const nextValues = new Set(next);
|
|
184
|
+
props.onChange?.([
|
|
185
|
+
...next
|
|
186
|
+
], [
|
|
187
|
+
...selectedOptions,
|
|
188
|
+
...options.filter((option)=>!selectedValues.has(option.value)),
|
|
189
|
+
...createOption ? [
|
|
190
|
+
createOption
|
|
191
|
+
] : []
|
|
192
|
+
].filter((option)=>nextValues.has(option.value)));
|
|
193
|
+
} else props.onChange?.(next[0] ?? '', void 0 === next[0] ? void 0 : optionIndex.get(next[0]) ?? (createOption?.value === next[0] ? createOption : void 0));
|
|
169
194
|
};
|
|
170
|
-
const selectionIndex = ()=>filtered.findIndex((option)=>
|
|
195
|
+
const selectionIndex = ()=>filtered.findIndex((option)=>selectedValues.has(option.value) && !unavailable(option));
|
|
171
196
|
const openMenu = ()=>{
|
|
172
197
|
if (disabled || inputProps.readOnly) return;
|
|
173
|
-
const active = filtered.findIndex((option)=>
|
|
198
|
+
const active = filtered.findIndex((option)=>selectedValues.has(option.value) && !unavailable(option));
|
|
174
199
|
setHighlighted(active >= 0 ? active : filtered.findIndex((option)=>!unavailable(option)));
|
|
175
200
|
setOpen(true);
|
|
176
201
|
};
|
|
@@ -189,7 +214,7 @@ const select_SelectControl = /*#__PURE__*/ forwardRef(function(props, forwardedR
|
|
|
189
214
|
]);
|
|
190
215
|
onCreate?.(option);
|
|
191
216
|
}
|
|
192
|
-
change(multiple ?
|
|
217
|
+
change(multiple ? selectedValues.has(option.value) ? values.filter((value)=>value !== option.value) : [
|
|
193
218
|
...values,
|
|
194
219
|
option.value
|
|
195
220
|
] : [
|
|
@@ -217,11 +242,11 @@ const select_SelectControl = /*#__PURE__*/ forwardRef(function(props, forwardedR
|
|
|
217
242
|
"aria-setsize": filtered.length,
|
|
218
243
|
type: "button",
|
|
219
244
|
role: "option",
|
|
220
|
-
"aria-selected":
|
|
221
|
-
"aria-disabled":
|
|
245
|
+
"aria-selected": selectedValues.has(option.value),
|
|
246
|
+
"aria-disabled": unavailable(option) || void 0,
|
|
222
247
|
"data-highlighted": highlighted === index || void 0,
|
|
223
248
|
className: "leaf-floating__option",
|
|
224
|
-
disabled:
|
|
249
|
+
disabled: unavailable(option),
|
|
225
250
|
tabIndex: -1,
|
|
226
251
|
onMouseEnter: ()=>{
|
|
227
252
|
if (!unavailable(option)) setHighlighted(index);
|
|
@@ -388,7 +413,7 @@ const select_SelectControl = /*#__PURE__*/ forwardRef(function(props, forwardedR
|
|
|
388
413
|
else if ('Backspace' === event.key && multiple && !searchQuery && !inputProps.readOnly) {
|
|
389
414
|
const last = [
|
|
390
415
|
...values
|
|
391
|
-
].reverse().find((value)=>!
|
|
416
|
+
].reverse().find((value)=>!optionIndex.get(value)?.disabled);
|
|
392
417
|
if (last) {
|
|
393
418
|
event.preventDefault();
|
|
394
419
|
change(values.filter((value)=>value !== last));
|