From adfb96c5e9968b94eb12290c8c7021518a819f91 Mon Sep 17 00:00:00 2001 From: Luka Dornhecker Date: Tue, 20 May 2025 17:01:07 +0200 Subject: [PATCH 1/2] fix(next): take x-jsf-presentation options into account when generating options property --- next/src/field/schema.ts | 18 +++++++++++------- next/test/fields.test.ts | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/next/src/field/schema.ts b/next/src/field/schema.ts index 27ae20fb4..7918917d8 100644 --- a/next/src/field/schema.ts +++ b/next/src/field/schema.ts @@ -332,15 +332,19 @@ export function buildFieldSchema( }) } - // Handle options - const options = getFieldOptions(schema) - if (options) { - field.options = options - if (schema.type === 'array') { - field.multiple = true + // Generate options from schema if no options were provided via x-jsf-presentation + if (field.options === undefined) { + const options = getFieldOptions(schema) + if (options) { + field.options = options + if (schema.type === 'array') { + field.multiple = true + } } } - else { + + // Options and fields are mutually exclusive, so we only add fields if no options were provided + if (field.options === undefined) { // We did not find options, so we might have an array to generate fields from const fields = getFields(schema, strictInputType) if (fields) { diff --git a/next/test/fields.test.ts b/next/test/fields.test.ts index 2c0634326..35034c087 100644 --- a/next/test/fields.test.ts +++ b/next/test/fields.test.ts @@ -235,6 +235,45 @@ describe('fields', () => { ]) }) + it('creates options from an enum inside items (deprecated)', () => { + const schema: JsfSchema = { + type: 'object', + properties: { + currency: { + 'type': 'array', + 'items': { + type: 'string', + enum: ['usd', 'eur', 'gbp'], + }, + 'x-jsf-presentation': { + options: [ + { + label: 'USD', + value: 'usd', + }, + { + label: 'EUR', + value: 'eur', + }, + { + label: 'GBP', + value: 'gbp', + }, + ], + }, + }, + }, + } + + const fields = buildFieldSchema(schema, 'root', true)!.fields! + + expect(fields[0].options).toEqual([ + { label: 'USD', value: 'usd' }, + { label: 'EUR', value: 'eur' }, + { label: 'GBP', value: 'gbp' }, + ]) + }) + describe('radio field', () => { it('builds a radio field with options', () => { const schema: JsfSchema = { From 08bff3c1c6d3a5674409b7451ba22662e735edca Mon Sep 17 00:00:00 2001 From: Luka Dornhecker Date: Tue, 20 May 2025 17:51:20 +0200 Subject: [PATCH 2/2] refactor options & fields logic to dedicated functions --- next/src/field/schema.ts | 57 ++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/next/src/field/schema.ts b/next/src/field/schema.ts index 7918917d8..af0307397 100644 --- a/next/src/field/schema.ts +++ b/next/src/field/schema.ts @@ -19,6 +19,42 @@ function addCheckboxAttributes(inputType: string, field: Field, schema: NonBoole } } +/** + * Add options attribute to a field + * @param field - The field to add the options to + * @param schema - The schema of the field + * @description + * This adds the options attribute to based on the schema's oneOf, anyOf, or enum if we don't already have options on the field. + */ +function addOptions(field: Field, schema: NonBooleanJsfSchema) { + if (field.options === undefined) { + const options = getFieldOptions(schema) + if (options) { + field.options = options + if (schema.type === 'array') { + field.multiple = true + } + } + } +} + +/** + * Add fields attribute to a field + * @param field - The field to add the fields to + * @param schema - The schema of the field + * @description + * This adds the fields attribute to based on the schema's items. + * Since options and fields are mutually exclusive, we only add fields if no options were provided. + */ +function addFields(field: Field, schema: NonBooleanJsfSchema, strictInputType?: boolean) { + if (field.options === undefined) { + const fields = getFields(schema, strictInputType) + if (fields) { + field.fields = fields + } + } +} + /** * Get the presentation input type for a field from a schema type (ported from v0) * @param type - The schema type @@ -332,25 +368,8 @@ export function buildFieldSchema( }) } - // Generate options from schema if no options were provided via x-jsf-presentation - if (field.options === undefined) { - const options = getFieldOptions(schema) - if (options) { - field.options = options - if (schema.type === 'array') { - field.multiple = true - } - } - } - - // Options and fields are mutually exclusive, so we only add fields if no options were provided - if (field.options === undefined) { - // We did not find options, so we might have an array to generate fields from - const fields = getFields(schema, strictInputType) - if (fields) { - field.fields = fields - } - } + addOptions(field, schema) + addFields(field, schema) return field }