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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

choice prompt parity #875

Merged
merged 4 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion libraries/botbuilder-dialogs/src/dialogContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,16 @@ export class DialogContext {
(promptOrOptions as Activity).type !== undefined) ||
typeof promptOrOptions === 'string'
) {
options = { prompt: promptOrOptions as string | Partial<Activity>, choices: choices };
options = { prompt: promptOrOptions as string | Partial<Activity> };
} else {
options = { ...promptOrOptions as PromptOptions };
}

if (choices)
{
options.choices = choices;
}

return this.beginDialog(dialogId, options);
}

Expand Down
5 changes: 3 additions & 2 deletions libraries/botbuilder-dialogs/src/prompts/choicePrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ export class ChoicePrompt extends Prompt<FoundChoice> {
const choices: any[] = (this.style === ListStyle.suggestedAction ? ChoiceFactory.toChoices(options.choices) : options.choices) || [];
const channelId: string = context.activity.channelId;
const choiceOptions: ChoiceFactoryOptions = this.choiceOptions || ChoicePrompt.defaultChoiceOptions[locale];
const choiceStyle: ListStyle = options.style || this.style;
if (isRetry && options.retryPrompt) {
prompt = this.appendChoices(options.retryPrompt, channelId, choices, this.style, choiceOptions);
prompt = this.appendChoices(options.retryPrompt, channelId, choices, choiceStyle, choiceOptions);
} else {
prompt = this.appendChoices(options.prompt, channelId, choices, this.style, choiceOptions);
prompt = this.appendChoices(options.prompt, channelId, choices, choiceStyle, choiceOptions);
}

// Send prompt
Expand Down
6 changes: 6 additions & 0 deletions libraries/botbuilder-dialogs/src/prompts/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ export interface PromptOptions {
*/
choices?: (string | Choice)[];

/**
* (Optional) Property that can be used to override or set the value of ChoicePrompt.Style
* when the prompt is executed using DialogContext.prompt.
*/
style?: ListStyle

/**
* (Optional) Additional validation rules to pass the prompts validator routine.
*/
Expand Down
56 changes: 56 additions & 0 deletions libraries/botbuilder-dialogs/tests/choicePrompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,62 @@ describe('ChoicePrompt', function () {
.assertReply('Please choose a color.');
});

it('should render choices if PromptOptions & choices are passed into DialogContext.prompt()', async function() {
const adapter = new TestAdapter(async (turnContext) => {
const dc = await dialogs.createContext(turnContext);

const results = await dc.continueDialog();
if (results.status === DialogTurnStatus.empty) {
await dc.prompt('prompt', { prompt: 'Please choose a color.', style: ListStyle.inline }, stringChoices);
} else if (results.status === DialogTurnStatus.complete) {
const selectedChoice = results.result;
await turnContext.sendActivity(selectedChoice.value);
}
await convoState.saveChanges(turnContext);
});
const convoState = new ConversationState(new MemoryStorage());

const dialogState = convoState.createProperty('dialogState');
const dialogs = new DialogSet(dialogState);
const choicePrompt = new ChoicePrompt('prompt');
choicePrompt.style = ListStyle.none;

dialogs.add(choicePrompt);

await adapter.send('Hello')
.assertReply('Please choose a color. (1) red, (2) green, or (3) blue')
.send(answerMessage)
.assertReply('red');
});

it('should send a prompt and choices if they are passed in via third argument in dc.prompt().', async function () {
const adapter = new TestAdapter(async (turnContext) => {
const dc = await dialogs.createContext(turnContext);

const results = await dc.continueDialog();
if (results.status === DialogTurnStatus.empty) {
await dc.prompt('prompt', { prompt: 'Please choose a color.' }, stringChoices);
} else if (results.status === DialogTurnStatus.complete) {
const selectedChoice = results.result;
await turnContext.sendActivity(selectedChoice.value);
}
await convoState.saveChanges(turnContext);
});
const convoState = new ConversationState(new MemoryStorage());

const dialogState = convoState.createProperty('dialogState');
const dialogs = new DialogSet(dialogState);
const choicePrompt = new ChoicePrompt('prompt');
choicePrompt.style = ListStyle.none;

dialogs.add(choicePrompt);

await adapter.send('Hello')
.assertReply('Please choose a color.')
.send(answerMessage)
.assertReply('red');
});

it('should not recognize if choices are not passed in.', async function () {
const adapter = new TestAdapter(async (turnContext) => {
const dc = await dialogs.createContext(turnContext);
Expand Down