| 2874 | } |
| 2875 | |
| 2876 | const processSpace = <A>( |
| 2877 | state: MultiSelectState, |
| 2878 | options: SelectOptionsReq<A> |
| 2879 | ) => { |
| 2880 | const selectedIndices = new Set(state.selectedIndices) |
| 2881 | if (state.index === 0) { |
| 2882 | const selectableCount = options.choices.filter((choice) => !choice.disabled).length |
| 2883 | const selectedCount = Array.from(state.selectedIndices).filter((index) => !options.choices[index].disabled).length |
| 2884 | if (selectedCount === selectableCount) { |
| 2885 | selectedIndices.clear() |
| 2886 | } else { |
| 2887 | for (let i = 0; i < options.choices.length; i++) { |
| 2888 | if (!options.choices[i].disabled) { |
| 2889 | selectedIndices.add(i) |
| 2890 | } |
| 2891 | } |
| 2892 | } |
| 2893 | } else if (state.index === 1) { |
| 2894 | for (let i = 0; i < options.choices.length; i++) { |
| 2895 | if (options.choices[i].disabled || state.selectedIndices.has(i)) { |
| 2896 | selectedIndices.delete(i) |
| 2897 | } else { |
| 2898 | selectedIndices.add(i) |
| 2899 | } |
| 2900 | } |
| 2901 | } else { |
| 2902 | const choiceIndex = state.index - metaOptionsCount |
| 2903 | if (options.choices[choiceIndex].disabled) { |
| 2904 | return Effect.succeed(Action.Beep()) |
| 2905 | } else if (selectedIndices.has(choiceIndex)) { |
| 2906 | selectedIndices.delete(choiceIndex) |
| 2907 | } else { |
| 2908 | selectedIndices.add(choiceIndex) |
| 2909 | } |
| 2910 | } |
| 2911 | return Effect.succeed(Action.NextFrame({ state: { ...state, selectedIndices } })) |
| 2912 | } |
| 2913 | |
| 2914 | const handleMultiSelectClear = <A>(options: SelectOptionsReq<A>) => |
| 2915 | Effect.fnUntraced(function*(state: MultiSelectState, _: Action<MultiSelectState, Array<A>>) { |