diff --git a/next/src/field/schema.ts b/next/src/field/schema.ts index 1d71ea916..c1bd1f6b1 100644 --- a/next/src/field/schema.ts +++ b/next/src/field/schema.ts @@ -160,7 +160,6 @@ function convertToOptions(nodeOptions: JsfSchema[]): Array { * Get field options from schema */ function getFieldOptions(schema: NonBooleanJsfSchema) { - // Handle oneOf or radio input type if (schema.oneOf) { return convertToOptions(schema.oneOf || []) } @@ -175,6 +174,15 @@ function getFieldOptions(schema: NonBooleanJsfSchema) { return convertToOptions(schema.anyOf) } + // Handle enum + if (schema.enum) { + const enumAsOneOf: JsfSchema['oneOf'] = schema.enum?.map(value => ({ + title: typeof value === 'string' ? value : JSON.stringify(value), + const: value, + })) || [] + return convertToOptions(enumAsOneOf) + } + return null } diff --git a/next/test/fields.test.ts b/next/test/fields.test.ts index 11f5b3b6d..b899e985c 100644 --- a/next/test/fields.test.ts +++ b/next/test/fields.test.ts @@ -283,6 +283,40 @@ describe('fields', () => { ]) }) + it('build a radio field with enum', () => { + const schema: JsfSchema = { + type: 'object', + properties: { + status: { + 'enum': ['active', true, false, 1], + 'x-jsf-presentation': { + inputType: 'radio', + }, + }, + }, + } + + const fields = buildFieldSchema(schema, 'root', true)!.fields! + + expect(fields).toEqual([ + { + type: 'radio', + inputType: 'radio', + jsonType: 'text', // BUG: This is wrong, should be undefined. + isVisible: true, + name: 'status', + required: false, + enum: ['active', true, false, 1], + options: [ + { label: 'active', value: 'active' }, + { label: 'true', value: true }, + { label: 'false', value: false }, + { label: '1', value: 1 }, + ], + }, + ]) + }) + it('skips options without a null const value', () => { const schema: JsfSchema = { type: 'object',