|
| 1 | +import { |
| 2 | + component, |
| 3 | + html, |
| 4 | + useCallback, |
| 5 | + useMemo, |
| 6 | + useProperty, |
| 7 | +} from '@pionjs/pion'; |
| 8 | +import { nothing } from 'lit-html'; |
| 9 | +import { ifDefined } from 'lit-html/directives/if-defined.js'; |
| 10 | +import '../autocomplete'; |
| 11 | +import { |
| 12 | + Autocomplete, |
| 13 | + observedAttributes, |
| 14 | + Props, |
| 15 | + style, |
| 16 | +} from '../autocomplete/autocomplete'; |
| 17 | +import { ChipProps } from '../autocomplete/chip'; |
| 18 | +import { ItemRendererOpts } from '../listbox/item-renderer'; |
| 19 | +import { WrappedItem } from './types'; |
| 20 | +import { useExcludingSelection } from './use-excluding-selection'; |
| 21 | +import { unwrap } from './utils'; |
| 22 | +import excludingStyle from './style.css'; |
| 23 | + |
| 24 | +const isItemExcluded = <I>(value: WrappedItem<I>[] | undefined, item: I) => |
| 25 | + value?.some((v) => v.item === item && v.excluded); |
| 26 | + |
| 27 | +const excludedState = <I>( |
| 28 | + value: WrappedItem<I>[] | undefined, |
| 29 | + item: I | null, |
| 30 | +) => (item && isItemExcluded(value, item) ? 'excluded' : nothing); |
| 31 | + |
| 32 | +const mkItemRenderer = |
| 33 | + <I>(value?: WrappedItem<I>[]) => |
| 34 | + ( |
| 35 | + item: I, |
| 36 | + i: number, |
| 37 | + { highlight, select, textual, isSelected }: ItemRendererOpts<I>, |
| 38 | + ) => { |
| 39 | + const rendered = textual(item); |
| 40 | + |
| 41 | + return html`<div |
| 42 | + class="item" |
| 43 | + role="option" |
| 44 | + part="option ${excludedState(value, item)}" |
| 45 | + ?aria-selected=${isSelected(item)} |
| 46 | + data-index=${i} |
| 47 | + @mouseenter=${() => highlight(i)} |
| 48 | + @click=${() => select(item)} |
| 49 | + @mousedown=${(e: Event) => e.preventDefault()} |
| 50 | + > |
| 51 | + ${rendered} |
| 52 | + </div> |
| 53 | + <div class="sizer" virtualizer-sizer>${rendered}</div>`; |
| 54 | + }; |
| 55 | + |
| 56 | +const mkChipRenderer = |
| 57 | + <I>(value: WrappedItem<I>[] | undefined, onClear: (item: I | null) => void) => |
| 58 | + ({ |
| 59 | + item, |
| 60 | + content, |
| 61 | + disabled, |
| 62 | + hidden, |
| 63 | + className = 'chip', |
| 64 | + slot, |
| 65 | + }: ChipProps<I>) => |
| 66 | + html`<cosmoz-autocomplete-chip |
| 67 | + class=${ifDefined(className)} |
| 68 | + slot=${ifDefined(slot)} |
| 69 | + part="chip" |
| 70 | + exportparts="chip-text, chip-clear" |
| 71 | + data-state=${excludedState(value, item)} |
| 72 | + ?disabled=${disabled} |
| 73 | + ?hidden=${hidden} |
| 74 | + .onClear=${() => onClear(item)} |
| 75 | + title=${ifDefined(typeof content === 'string' ? content : undefined)} |
| 76 | + > |
| 77 | + ${content} |
| 78 | + </cosmoz-autocomplete-chip>`; |
| 79 | + |
| 80 | +const AutocompleteExcluding = <I>(props: Props<I>) => { |
| 81 | + const { value, setValue, setExcludingValue } = |
| 82 | + useExcludingSelection<I>('value'); |
| 83 | + const [text, setText] = useProperty<string>('text'); |
| 84 | + |
| 85 | + const onClear = useCallback( |
| 86 | + (item: I | null) => |
| 87 | + setValue((values) => values?.filter((i) => i.item !== item)), |
| 88 | + [], |
| 89 | + ); |
| 90 | + |
| 91 | + return Autocomplete({ |
| 92 | + ...props, |
| 93 | + value: useMemo(() => value?.map(unwrap), [value]), |
| 94 | + onChange: useCallback((value: I[]) => { |
| 95 | + setExcludingValue(value); |
| 96 | + }, []), |
| 97 | + text, |
| 98 | + onText: useCallback((text: string) => { |
| 99 | + setText(text); |
| 100 | + }, []), |
| 101 | + itemRenderer: useMemo(() => mkItemRenderer(value), [value]), |
| 102 | + chipRenderer: useMemo( |
| 103 | + () => mkChipRenderer(value, onClear), |
| 104 | + [value, onClear], |
| 105 | + ), |
| 106 | + }); |
| 107 | +}; |
| 108 | + |
| 109 | +customElements.define( |
| 110 | + 'cosmoz-autocomplete-excluding', |
| 111 | + component(AutocompleteExcluding, { |
| 112 | + observedAttributes, |
| 113 | + styleSheets: [style, excludingStyle], |
| 114 | + }), |
| 115 | +); |
0 commit comments