Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat (select): Allow default config in Select prompt #1329

Merged
merged 3 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/select/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const answer = await select({
| -------- | ---------------------------------------------------------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| message | `string` | yes | The question to ask |
| choices | `Array<{ value: string, name?: string, description?: string, disabled?: boolean \| string } \| Separator>` | yes | List of the available choices. The `value` will be returned as the answer, and used as display if no `name` is defined. Choices who're `disabled` will be displayed, but not selectable. The `description` will be displayed under the prompt when the cursor land over the choice. |
| default | `string` | no | Selects the default value that is initially. If omitted, the first selectable item is selected. |
| default | `string` | no | Defines in front of which item the cursor will initially appear. When omitted, the cursor will appear on the first selectable item. |
| pageSize | `number` | no | By default, lists of choice longer than 7 will be paginated. Use this option to control how many choices will appear on the screen at once. |
| loop | `boolean` | no | Defaults to `true`. When set to `false`, the cursor will be constrained to the top and bottom of the choice list without looping. |

Expand Down
9 changes: 4 additions & 5 deletions packages/select/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@
config: SelectConfig<Value>,
done: (value: Value) => void,
): string => {
const { choices: items, loop = true, pageSize, default: _default } = config;
const { choices: items, loop = true, pageSize } = config;
const firstRender = useRef(true);
const prefix = usePrefix();
const [status, setStatus] = useState('pending');

const bounds = useMemo(() => {
const first = items.findIndex(isSelectable);
// TODO: Replace with `findLastIndex` when it's available.

Check warning on line 71 in packages/select/src/index.mts

View workflow job for this annotation

GitHub Actions / Linting

Unexpected 'todo' comment: 'TODO: Replace with `findLastIndex` when...'

Check warning on line 71 in packages/select/src/index.mts

View workflow job for this annotation

GitHub Actions / Linting

Unexpected 'todo' comment: 'TODO: Replace with `findLastIndex` when...'
const last = items.length - 1 - [...items].reverse().findIndex(isSelectable);
if (first < 0)
throw new Error(
Expand All @@ -78,12 +78,11 @@
}, [items]);

const defaultItemIndex = useMemo(() => {
if (!_default) return -1;
if (!('default' in config)) return -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree that it's nicer, but perhaps we should just rename all default to defaultValue as it's a bit annoying to work with the reserved word imo.

return items.findIndex(
(item) =>
!Separator.isSeparator(item) && isSelectable(item) && item.value === _default,
(item) => isSelectable(item) && item.value === config.default,
);
}, [_default, items]);
}, [config.default, items]);

const [active, setActive] = useState(
defaultItemIndex === -1 ? bounds.first : defaultItemIndex,
Expand Down