|
| 1 | +import { Box, useInput } from 'ink' |
| 2 | +import React, { useCallback, useState } from 'react' |
| 3 | +import CheckBox from './components/Checkbox.js' |
| 4 | +import Indicator from './components/Indicator.js' |
| 5 | +import ItemComponent from './components/Item.js' |
| 6 | + |
| 7 | +type Item<T> = { |
| 8 | + label: string |
| 9 | + value: T |
| 10 | + key?: string | number |
| 11 | +} |
| 12 | + |
| 13 | +type MultiSelectProps<T> = { |
| 14 | + items: Item<T>[] |
| 15 | + defaultSelected?: Item<T>[] |
| 16 | + focus?: boolean |
| 17 | + initialIndex?: number |
| 18 | + indicatorComponent?: React.FC<{ isHighlighted: boolean }> |
| 19 | + checkboxComponent?: React.FC<{ isSelected: boolean }> |
| 20 | + itemComponent?: React.FC<{ isHighlighted: boolean; label: string }> |
| 21 | + limit?: number | null |
| 22 | + onSelect?: (selectedItem: Item<T>) => void |
| 23 | + onUnselect?: (unselectedItem: Item<T>) => void |
| 24 | + onSubmit?: (selectedItems: Item<T>[]) => void |
| 25 | + onHighlight?: (highlightedItem: Item<T>) => void |
| 26 | +} |
| 27 | + |
| 28 | +const MultiSelect = <T,>({ |
| 29 | + items = [], |
| 30 | + defaultSelected = [], |
| 31 | + focus = true, |
| 32 | + initialIndex = 0, |
| 33 | + indicatorComponent = Indicator, |
| 34 | + checkboxComponent = CheckBox, |
| 35 | + itemComponent = ItemComponent, |
| 36 | + limit = null, |
| 37 | + onSelect = () => {}, |
| 38 | + onUnselect = () => {}, |
| 39 | + onSubmit = () => {}, |
| 40 | + onHighlight = () => {}, |
| 41 | +}: MultiSelectProps<T>) => { |
| 42 | + const [highlightedIndex, setHighlightedIndex] = useState(initialIndex) |
| 43 | + const [selectedItems, setSelectedItems] = useState(defaultSelected) |
| 44 | + |
| 45 | + const hasLimit = limit !== null && limit < items.length |
| 46 | + |
| 47 | + const slicedItems = hasLimit ? items.slice(0, limit) : items |
| 48 | + |
| 49 | + const includesItems = useCallback((item: Item<T>, selectedItems: Item<T>[]) => { |
| 50 | + return ( |
| 51 | + selectedItems.filter( |
| 52 | + (selectedItem) => selectedItem.value === item.value && selectedItem.label === item.label, |
| 53 | + ).length > 0 |
| 54 | + ) |
| 55 | + }, []) |
| 56 | + |
| 57 | + const handleSelect = useCallback( |
| 58 | + (item: Item<T>) => { |
| 59 | + if (includesItems(item, selectedItems)) { |
| 60 | + const newSelectedItems = selectedItems.filter( |
| 61 | + (selectedItem) => selectedItem.value !== item.value && selectedItem.label !== item.label, |
| 62 | + ) |
| 63 | + setSelectedItems(newSelectedItems) |
| 64 | + onUnselect(item) |
| 65 | + } else { |
| 66 | + const newSelectedItems = [...selectedItems, item] |
| 67 | + setSelectedItems(newSelectedItems) |
| 68 | + onSelect(item) |
| 69 | + } |
| 70 | + }, |
| 71 | + [selectedItems, onSelect, onUnselect, includesItems], |
| 72 | + ) |
| 73 | + |
| 74 | + const handleSubmit = useCallback(() => { |
| 75 | + onSubmit(selectedItems) |
| 76 | + }, [selectedItems, onSubmit]) |
| 77 | + |
| 78 | + useInput( |
| 79 | + useCallback( |
| 80 | + (input, key) => { |
| 81 | + if (key.upArrow) { |
| 82 | + setHighlightedIndex((prevIndex) => { |
| 83 | + const index = prevIndex === 0 ? slicedItems.length - 1 : prevIndex - 1 |
| 84 | + // biome-ignore lint/style/noNonNullAssertion: <explanation> |
| 85 | + onHighlight(slicedItems[index]!) |
| 86 | + return index |
| 87 | + }) |
| 88 | + } else if (key.downArrow) { |
| 89 | + setHighlightedIndex((prevIndex) => { |
| 90 | + const index = prevIndex === slicedItems.length - 1 ? 0 : prevIndex + 1 |
| 91 | + // biome-ignore lint/style/noNonNullAssertion: <explanation> |
| 92 | + onHighlight(slicedItems[index]!) |
| 93 | + return index |
| 94 | + }) |
| 95 | + } else if (key.return) { |
| 96 | + handleSubmit() |
| 97 | + } else if (input === ' ') { |
| 98 | + // biome-ignore lint/style/noNonNullAssertion: <explanation> |
| 99 | + handleSelect(slicedItems[highlightedIndex]!) |
| 100 | + } |
| 101 | + }, |
| 102 | + [onHighlight, handleSelect, handleSubmit, slicedItems, highlightedIndex], |
| 103 | + ), |
| 104 | + { isActive: focus }, |
| 105 | + ) |
| 106 | + |
| 107 | + return ( |
| 108 | + <Box flexDirection="column"> |
| 109 | + {slicedItems.map((item, index) => { |
| 110 | + const key = item.key || item.label |
| 111 | + const isHighlighted = index === highlightedIndex |
| 112 | + const isSelected = includesItems(item, selectedItems) |
| 113 | + |
| 114 | + return ( |
| 115 | + <Box key={key}> |
| 116 | + {React.createElement(indicatorComponent, { isHighlighted })} |
| 117 | + {React.createElement(checkboxComponent, { isSelected })} |
| 118 | + {React.createElement(itemComponent, { |
| 119 | + ...item, |
| 120 | + isHighlighted, |
| 121 | + })} |
| 122 | + </Box> |
| 123 | + ) |
| 124 | + })} |
| 125 | + </Box> |
| 126 | + ) |
| 127 | +} |
| 128 | + |
| 129 | +export default MultiSelect |
| 130 | + |
| 131 | +export { Indicator, ItemComponent, CheckBox, type Item } |
0 commit comments