feat(@angular/cli): Added support for multiselect list prompt (#13031)

* feat(@angular/cli): Added support for multiselect list prompt

* If multiselect option is true use the checkbox
   inquirer prompt type, otherwise use list.

* feat(@angular-devkit/core): Added support for multiselect list prompt

* Added multiselect to PromptDefinition interface and usages
This commit is contained in:
Michael O'Keefe 2018-11-30 15:01:57 -05:00 committed by vikerman
parent 1ad42db285
commit db0fc7759b
5 changed files with 82 additions and 3 deletions

View File

@ -309,7 +309,7 @@ export abstract class SchematicCommand<
question.type = 'confirm'; question.type = 'confirm';
break; break;
case 'list': case 'list':
question.type = 'list'; question.type = !!definition.multiselect ? 'checkbox' : 'list';
question.choices = definition.items && definition.items.map(item => { question.choices = definition.items && definition.items.map(item => {
if (typeof item == 'string') { if (typeof item == 'string') {
return item; return item;

View File

@ -99,13 +99,14 @@ export interface PromptDefinition {
id: string; id: string;
type: string; type: string;
message: string; message: string;
default?: string | number | boolean | null; default?: string | string[] | number | boolean | null;
priority: number; priority: number;
validator?: (value: string) => boolean | string | Promise<boolean | string>; validator?: (value: string) => boolean | string | Promise<boolean | string>;
items?: Array<string | { value: JsonValue, label: string }>; items?: Array<string | { value: JsonValue, label: string }>;
raw?: string | JsonObject; raw?: string | JsonObject;
multiselect?: boolean;
} }
export type PromptProvider = (definitions: Array<PromptDefinition>) export type PromptProvider = (definitions: Array<PromptDefinition>)

View File

@ -262,6 +262,83 @@ describe('Prompt Provider', () => {
.toPromise().then(done, done.fail); .toPromise().then(done, done.fail);
}); });
it('analyzes list with multiselect option and object items', done => {
const registry = new CoreSchemaRegistry();
const data: any = {};
registry.usePromptProvider(async definitions => {
expect(definitions.length).toBe(1);
expect(definitions[0].type).toBe('list');
expect(definitions[0].multiselect).toBe(true);
expect(definitions[0].items).toEqual([
{ 'value': 'one', 'label': 'one' },
{ 'value': 'two', 'label': 'two' },
]);
return { [definitions[0].id]: { 'value': 'one', 'label': 'one' } };
});
registry
.compile({
properties: {
test: {
type: 'array',
'x-prompt': {
'type': 'list',
'multiselect': true,
'items': [
{ 'value': 'one', 'label': 'one' },
{ 'value': 'two', 'label': 'two' },
],
'message': 'test-message',
},
},
},
})
.pipe(
mergeMap(validator => validator(data)),
)
.toPromise().then(done, done.fail);
});
it('analyzes list without multiselect option and object items', done => {
const registry = new CoreSchemaRegistry();
const data: any = {};
registry.usePromptProvider(async definitions => {
expect(definitions.length).toBe(1);
expect(definitions[0].type).toBe('list');
expect(definitions[0].multiselect).toBeUndefined();
expect(definitions[0].items).toEqual([
{ 'value': 'one', 'label': 'one' },
{ 'value': 'two', 'label': 'two' },
]);
return { [definitions[0].id]: { 'value': 'two', 'label': 'two' } };
});
registry
.compile({
properties: {
test: {
type: 'array',
'x-prompt': {
'type': 'list',
'items': [
{ 'value': 'one', 'label': 'one' },
{ 'value': 'two', 'label': 'two' },
],
'message': 'test-message',
},
},
},
})
.pipe(
mergeMap(validator => validator(data)),
)
.toPromise().then(done, done.fail);
});
it('analyzes enums WITHOUT explicit list type', done => { it('analyzes enums WITHOUT explicit list type', done => {
const registry = new CoreSchemaRegistry(); const registry = new CoreSchemaRegistry();
const data: any = {}; const data: any = {};

View File

@ -568,6 +568,7 @@ export class CoreSchemaRegistry implements SchemaRegistry {
priority: 0, priority: 0,
raw: schema, raw: schema,
items, items,
multiselect: type === 'list' ? schema.multiselect : false,
default: typeof parentSchema.default == 'object' ? undefined : parentSchema.default, default: typeof parentSchema.default == 'object' ? undefined : parentSchema.default,
async validator(data: string) { async validator(data: string) {
const result = it.self.validate(parentSchema, data); const result = it.self.validate(parentSchema, data);

View File

@ -98,7 +98,7 @@ function _createPromptProvider(): schema.PromptProvider {
case 'list': case 'list':
return { return {
...question, ...question,
type: 'list', type: !!definition.multiselect ? 'checkbox' : 'list',
choices: definition.items && definition.items.map(item => { choices: definition.items && definition.items.map(item => {
if (typeof item == 'string') { if (typeof item == 'string') {
return item; return item;