use-select.ts
1 import { useInput } from 'ink' 2 import { type SelectState } from './use-select-state.js' 3 4 export type UseSelectProps = { 5 /** 6 * When disabled, user input is ignored. 7 * 8 * @default false 9 */ 10 isDisabled?: boolean 11 12 /** 13 * Select state. 14 */ 15 state: SelectState 16 } 17 18 export const useSelect = ({ isDisabled = false, state }: UseSelectProps) => { 19 useInput( 20 (_input, key) => { 21 if (key.downArrow) { 22 state.focusNextOption() 23 } 24 25 if (key.upArrow) { 26 state.focusPreviousOption() 27 } 28 29 if (key.return) { 30 state.selectFocusedOption() 31 } 32 }, 33 { isActive: !isDisabled }, 34 ) 35 }