Skip to content

Commit 27c6953

Browse files
authored
fix(next): Field - convert enum to options (#181)
* fix(next): Field - convert enum to options * annotate a bug in jsonType for later
1 parent 2a1f52c commit 27c6953

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

next/src/field/schema.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ function convertToOptions(nodeOptions: JsfSchema[]): Array<FieldOption> {
160160
* Get field options from schema
161161
*/
162162
function getFieldOptions(schema: NonBooleanJsfSchema) {
163-
// Handle oneOf or radio input type
164163
if (schema.oneOf) {
165164
return convertToOptions(schema.oneOf || [])
166165
}
@@ -175,6 +174,15 @@ function getFieldOptions(schema: NonBooleanJsfSchema) {
175174
return convertToOptions(schema.anyOf)
176175
}
177176

177+
// Handle enum
178+
if (schema.enum) {
179+
const enumAsOneOf: JsfSchema['oneOf'] = schema.enum?.map(value => ({
180+
title: typeof value === 'string' ? value : JSON.stringify(value),
181+
const: value,
182+
})) || []
183+
return convertToOptions(enumAsOneOf)
184+
}
185+
178186
return null
179187
}
180188

next/test/fields.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,40 @@ describe('fields', () => {
283283
])
284284
})
285285

286+
it('build a radio field with enum', () => {
287+
const schema: JsfSchema = {
288+
type: 'object',
289+
properties: {
290+
status: {
291+
'enum': ['active', true, false, 1],
292+
'x-jsf-presentation': {
293+
inputType: 'radio',
294+
},
295+
},
296+
},
297+
}
298+
299+
const fields = buildFieldSchema(schema, 'root', true)!.fields!
300+
301+
expect(fields).toEqual([
302+
{
303+
type: 'radio',
304+
inputType: 'radio',
305+
jsonType: 'text', // BUG: This is wrong, should be undefined.
306+
isVisible: true,
307+
name: 'status',
308+
required: false,
309+
enum: ['active', true, false, 1],
310+
options: [
311+
{ label: 'active', value: 'active' },
312+
{ label: 'true', value: true },
313+
{ label: 'false', value: false },
314+
{ label: '1', value: 1 },
315+
],
316+
},
317+
])
318+
})
319+
286320
it('skips options without a null const value', () => {
287321
const schema: JsfSchema = {
288322
type: 'object',

0 commit comments

Comments
 (0)