diff --git a/src/index.mts b/src/index.mts index 917bf38..42801c6 100644 --- a/src/index.mts +++ b/src/index.mts @@ -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] diff --git a/test/normal.test.mts b/test/normal.test.mts index 081e435..4486c2a 100644 --- a/test/normal.test.mts +++ b/test/normal.test.mts @@ -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, { + 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 to select, to move rows, 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'], } ]) })