|
| 1 | +import { disposeBatch } from "@mendix/widget-plugin-mobx-kit/disposeBatch"; |
| 2 | +import { useCombobox, UseComboboxProps } from "downshift"; |
| 3 | +import { action, autorun, computed, makeObservable, observable, reaction } from "mobx"; |
| 4 | +import { SearchStore } from "../../stores/SearchStore"; |
| 5 | +import { OptionWithState } from "../../typings/OptionWithState"; |
| 6 | +import { GConstructor } from "../../typings/type-utils"; |
| 7 | + |
| 8 | +export interface FilterStore { |
| 9 | + clear: () => void; |
| 10 | + setSelected: (value: Iterable<string>) => void; |
| 11 | + selected: Set<string>; |
| 12 | + options: OptionWithState[]; |
| 13 | + selectedOptions: OptionWithState[]; |
| 14 | + search: SearchStore; |
| 15 | +} |
| 16 | + |
| 17 | +type BaseController = GConstructor<{ |
| 18 | + filterStore: FilterStore; |
| 19 | + multiselect: boolean; |
| 20 | + setup(): () => void; |
| 21 | +}>; |
| 22 | + |
| 23 | +// eslint-disable-next-line @typescript-eslint/explicit-function-return-type |
| 24 | +export function ComboboxControllerMixin<TBase extends BaseController>(Base: TBase) { |
| 25 | + return class ComboboxControllerMixin extends Base { |
| 26 | + touched = false; |
| 27 | + inputValue = ""; |
| 28 | + inputPlaceholder = ""; |
| 29 | + |
| 30 | + constructor(...args: any[]) { |
| 31 | + super(...args); |
| 32 | + |
| 33 | + makeObservable(this, { |
| 34 | + inputValue: observable, |
| 35 | + setInputValue: action, |
| 36 | + touched: observable, |
| 37 | + setTouched: action, |
| 38 | + selectedIndex: computed, |
| 39 | + selectedOption: computed, |
| 40 | + isEmpty: computed, |
| 41 | + options: computed, |
| 42 | + handleBlur: action, |
| 43 | + handleClear: action |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + setup(): () => void { |
| 48 | + const [add, dispose] = disposeBatch(); |
| 49 | + add(autorun(...this.searchSyncFx())); |
| 50 | + |
| 51 | + // Set input when store state changes |
| 52 | + add(reaction(...this.storeSyncFx())); |
| 53 | + |
| 54 | + add(super.setup()); |
| 55 | + this.setInputValue(this.inputInitValue); |
| 56 | + return dispose; |
| 57 | + } |
| 58 | + |
| 59 | + searchSyncFx(): Parameters<typeof autorun> { |
| 60 | + const effect = (): void => { |
| 61 | + const { touched, inputValue } = this; |
| 62 | + if (touched) { |
| 63 | + this.filterStore.search.setBuffer(inputValue); |
| 64 | + } else { |
| 65 | + this.filterStore.search.clear(); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + return [effect]; |
| 70 | + } |
| 71 | + |
| 72 | + storeSyncFx(): Parameters<typeof reaction> { |
| 73 | + const data = (): string => this.selectedOption?.caption ?? ""; |
| 74 | + const effect = (caption: string): void => { |
| 75 | + if (!this.touched) { |
| 76 | + this.setInputValue(caption); |
| 77 | + } |
| 78 | + }; |
| 79 | + return [data, effect]; |
| 80 | + } |
| 81 | + |
| 82 | + get options(): OptionWithState[] { |
| 83 | + return this.filterStore.options; |
| 84 | + } |
| 85 | + |
| 86 | + get isEmpty(): boolean { |
| 87 | + return this.filterStore.selected.size === 0; |
| 88 | + } |
| 89 | + |
| 90 | + get selectedIndex(): number { |
| 91 | + const index = this.filterStore.options.findIndex(option => option.selected); |
| 92 | + return Math.max(index, 0); |
| 93 | + } |
| 94 | + |
| 95 | + get selectedOption(): OptionWithState | null { |
| 96 | + return this.filterStore.selectedOptions.at(0) ?? null; |
| 97 | + } |
| 98 | + |
| 99 | + get inputInitValue(): string { |
| 100 | + if (this.selectedOption) { |
| 101 | + return this.selectedOption.caption; |
| 102 | + } |
| 103 | + if (this.filterStore.selected.size === 0) { |
| 104 | + return ""; |
| 105 | + } else { |
| 106 | + return "1 item selected (but not applied)"; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + setTouched(value: boolean): void { |
| 111 | + this.touched = value; |
| 112 | + } |
| 113 | + |
| 114 | + setInputValue(value: string): void { |
| 115 | + this.inputValue = value; |
| 116 | + } |
| 117 | + |
| 118 | + handleFocus(event: React.FocusEvent<HTMLInputElement>): void { |
| 119 | + event.target.select(); |
| 120 | + } |
| 121 | + |
| 122 | + handleBlur = (): void => { |
| 123 | + this.setTouched(false); |
| 124 | + this.setInputValue(this.selectedOption?.caption ?? ""); |
| 125 | + this.filterStore.search.clear(); |
| 126 | + }; |
| 127 | + |
| 128 | + handleClear = (): void => { |
| 129 | + this.setTouched(false); |
| 130 | + this.setInputValue(""); |
| 131 | + this.filterStore.clear(); |
| 132 | + }; |
| 133 | + |
| 134 | + useComboboxProps = (): UseComboboxProps<OptionWithState> => { |
| 135 | + const props: UseComboboxProps<OptionWithState> = { |
| 136 | + items: this.filterStore.options, |
| 137 | + itemToKey: item => item?.value, |
| 138 | + itemToString: item => item?.caption ?? "", |
| 139 | + inputValue: this.inputValue, |
| 140 | + defaultHighlightedIndex: this.selectedIndex, |
| 141 | + onInputValueChange: changes => { |
| 142 | + // Blur is handled by handleBlur; |
| 143 | + if (changes.type === useCombobox.stateChangeTypes.InputBlur) { |
| 144 | + return; |
| 145 | + } |
| 146 | + if (changes.type === useCombobox.stateChangeTypes.InputKeyDownEscape) { |
| 147 | + this.handleClear(); |
| 148 | + return; |
| 149 | + } |
| 150 | + if (changes.type === useCombobox.stateChangeTypes.InputChange) { |
| 151 | + this.setTouched(true); |
| 152 | + } |
| 153 | + this.setInputValue(changes.inputValue); |
| 154 | + }, |
| 155 | + onSelectedItemChange: ({ selectedItem, type }) => { |
| 156 | + if ( |
| 157 | + type === useCombobox.stateChangeTypes.InputBlur || |
| 158 | + type === useCombobox.stateChangeTypes.InputKeyDownEscape |
| 159 | + ) { |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + this.setTouched(false); |
| 164 | + this.filterStore.setSelected(selectedItem ? [selectedItem.value] : []); |
| 165 | + }, |
| 166 | + stateReducer(state, { changes }) { |
| 167 | + return { |
| 168 | + ...changes, |
| 169 | + highlightedIndex: changes.inputValue !== state.inputValue ? 0 : changes.highlightedIndex |
| 170 | + }; |
| 171 | + } |
| 172 | + }; |
| 173 | + return props; |
| 174 | + }; |
| 175 | + }; |
| 176 | +} |
0 commit comments