Skip to content

Commit

Permalink
feat(@angular-devkit/core): Added support for multiselect list prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
okeefem2 committed Nov 28, 2018
1 parent 6f1144d commit dd895c6
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/angular_devkit/core/src/json/schema/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ export interface PromptDefinition {
id: string;
type: string;
message: string;
default?: string | number | boolean | null;
default?: string | string[] | number | boolean | null;
priority: number;
validator?: (value: string) => boolean | string | Promise<boolean | string>;

items?: Array<string | { value: JsonValue, label: string }>;

raw?: string | JsonObject;
multiselect?: boolean;
}

export type PromptProvider = (definitions: Array<PromptDefinition>)
Expand Down
77 changes: 77 additions & 0 deletions packages/angular_devkit/core/src/json/schema/prompt_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,83 @@ describe('Prompt Provider', () => {
.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 => {
const registry = new CoreSchemaRegistry();
const data: any = {};
Expand Down
1 change: 1 addition & 0 deletions packages/angular_devkit/core/src/json/schema/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ export class CoreSchemaRegistry implements SchemaRegistry {
priority: 0,
raw: schema,
items,
multiselect: type === 'list' ? schema.multiselect : false,
default: typeof parentSchema.default == 'object' ? undefined : parentSchema.default,
async validator(data: string) {
const result = it.self.validate(parentSchema, data);
Expand Down
2 changes: 1 addition & 1 deletion packages/angular_devkit/schematics_cli/bin/schematics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function _createPromptProvider(): schema.PromptProvider {
case 'list':
return {
...question,
type: 'list',
type: !!definition.multiselect ? 'checkbox' : 'list',
choices: definition.items && definition.items.map(item => {
if (typeof item == 'string') {
return item;
Expand Down

0 comments on commit dd895c6

Please sign in to comment.