Skip to content

Commit

Permalink
[Feature] Add sumUp method to format answers after submit
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartheleway committed Oct 6, 2024
1 parent 5a3a9d0 commit 5628a54
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const answer = await tableMultiple({
| pageSize | `number` | no | The number of lines to display. | `7` |
| required | `boolean` | no | Indicate if at least one choice is necessary. | `false`
| rows | `(TableRow \| Separator)[]` | yes | The list of rows. Each row offer a choice which can be multiple (radio vs checkbox vs checkbox-multi) based on the `mode` option. |
| sumUp | `TableAnswers<Value> => string` | no | This allow to display a resume of selected answers to user after validation. |
| validate | `TableAnswers<Value> => boolean \| string \| Promise<string \| boolean>` | no | On submit, validate the answered content. When returning a string, it'll be used as the error message displayed to the user. Note: returning a rejected promise, we'll assume a code error happened and crash. |

## Columns
Expand Down
11 changes: 9 additions & 2 deletions src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type TableConfig<Value extends object | string | number> = {
loop?: boolean,
allowUnset?: boolean,
validate?: (answers: TableAnswers<Value>) => boolean | string | Promise<string | boolean>,
sumUp?: (answers: TableAnswers<Value>) => string
}

type TableAnswer<Value> = {
Expand Down Expand Up @@ -200,6 +201,7 @@ export default createPrompt(
pageSize = 7,
loop = true,
validate = () => true,
sumUp = () => '',
} = config

const [status, setStatus] = useState<Status>('idle')
Expand Down Expand Up @@ -385,11 +387,16 @@ export default createPrompt(
config.message,
helpTip,
].filter(Boolean).join(' '),
'',
]

if (status !== 'done') {
printToShell.push(table.toString())
printToShell.push('', table.toString())
} else {
const summarized = sumUp(values)

if (summarized) {
printToShell.push(summarized)
}
}

printToShell.push(`${error}${ansiEscapes.cursorHide}`)
Expand Down
68 changes: 68 additions & 0 deletions test/normal.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,74 @@ describe('table-multiple prompt [normal]', () => {
].join('\n'))
})

it('format answers as requested', 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',
},
],
rows: choices,
sumUp: (answers: TableAnswers<string>) => answers.map(answer => answer.answers.length ? `${answer.choice.title}: ${answer.answers.join(',')}` : '').filter(Boolean).join('; ')
})

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

events.keypress('space')

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

events.keypress('enter')

await Promise.resolve()

expect(getScreen()).toMatchInlineSnapshot([
'"✔ What do you want?',
'Test 1: A"',
].join('\n'))

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

it('throws error if missing columns', async () => {
const choices = [
{
Expand Down

0 comments on commit 5628a54

Please sign in to comment.