Skip to content

Commit

Permalink
Fix allowUnset selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartheleway committed Apr 12, 2024
1 parent 16d90eb commit ddd4566
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,15 @@ export default createPrompt(
}
else if (config.allowUnset) {
if (currentValues.length) {
currentValues = []
if (
value !== currentValues[0]
&& hasValue(value)
) {
currentValues = [value]
}
else {
currentValues = []
}
}
else if (hasValue(value)) {
currentValues = [value]
Expand Down
96 changes: 95 additions & 1 deletion test/normal.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,106 @@ describe('table-multiple prompt [normal]', () => {
'└──────────┴───────┴─────────┘"'
].join('\n'))

events.keypress('right')
events.keypress('space')

expect(getScreen()).toMatchInlineSnapshot([
'"What do you want?',
'',
'┌──────────┬───────┬─────────┐',
'│ 1-2 of 2 │ A? │ Default │',
'├──────────┼───────┼─────────┤',
'│ Test 1 │ ◯ │ [ ◉ ] │',
'├──────────┼───────┼─────────┤',
'│ Test 2 │ ◯ │ ◉ │',
'└──────────┴───────┴─────────┘"'
].join('\n'))

events.keypress('enter')

await expect(answer).resolves.toEqual([])
})

it('handle undefined column value with multiple choices & allowUnset', async () => {
const choices = [
{
value: '1',
title: 'Test 1',
},
{
value: '2',
title: 'Test 2',
}
]

const { answer, events, getScreen } = await render(tableMultiple<string>, {
message: 'What do you want?',
columns: [
{
title: 'A?',
value: 'A',
},
{
title: 'B?',
value: 'B',
},
{
title: 'Default',
value: undefined,
},
],
rows: choices,
allowUnset: true,
})

expect(getScreen()).toMatchInlineSnapshot([
'"What do you want? (Press <space> to select, <Up and Down> to move rows, <Left',
'and Right> to move columns)',
'',
'┌──────────┬───────┬───────┬─────────┐',
'│ 1-2 of 2 │ A? │ B? │ Default │',
'├──────────┼───────┼───────┼─────────┤',
'│ Test 1 │ [ ◯ ] │ ◯ │ ◉ │',
'├──────────┼───────┼───────┼─────────┤',
'│ Test 2 │ ◯ │ ◯ │ ◉ │',
'└──────────┴───────┴───────┴─────────┘"'
].join('\n'))

events.keypress('space')

expect(getScreen()).toMatchInlineSnapshot([
'"What do you want?',
'',
'┌──────────┬───────┬───────┬─────────┐',
'│ 1-2 of 2 │ A? │ B? │ Default │',
'├──────────┼───────┼───────┼─────────┤',
'│ Test 1 │ [ ◉ ] │ ◯ │ ◯ │',
'├──────────┼───────┼───────┼─────────┤',
'│ Test 2 │ ◯ │ ◯ │ ◉ │',
'└──────────┴───────┴───────┴─────────┘"',
].join('\n'))

events.keypress('right')
events.keypress('space')

expect(getScreen()).toMatchInlineSnapshot([
'"What do you want?',
'',
'┌──────────┬───────┬───────┬─────────┐',
'│ 1-2 of 2 │ A? │ B? │ Default │',
'├──────────┼───────┼───────┼─────────┤',
'│ Test 1 │ ◯ │ [ ◉ ] │ ◯ │',
'├──────────┼───────┼───────┼─────────┤',
'│ Test 2 │ ◯ │ ◯ │ ◉ │',
'└──────────┴───────┴───────┴─────────┘"',
].join('\n'))

events.keypress('enter')

await expect(answer).resolves.toEqual([
{
choice: choices[0],
answers: ['A'],
answers: ['B'],
}
])
})
Expand Down

0 comments on commit ddd4566

Please sign in to comment.