Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Radio/Select): Spread x-jsf-presentation value to option root #17

Merged
merged 3 commits into from
Jun 22, 2023

Conversation

sandrina-p
Copy link
Collaborator

@sandrina-p sandrina-p commented Jun 20, 2023

Example

For example, given the following JSON Schema, where each oneOf contains a x-jsf-presentation:

{
  properties: {
    health_perks: {
      title: 'Health perks',
      oneOf: [
        {
          const: 'basic',
          title: 'Basic',
          'x-jsf-presentation': {
            cost: '$30.00/mo',
          },
          'x-another': 'extra-thing',
        },
        {
          const: 'standard',
          title: 'Standard',
          'x-jsf-presentation': {
            cost: '$50.00/mo',
          },
        }
      ],
      'x-jsf-presentation': {
        inputType: 'radio',
        layout: "vertical"
      },
      type: 'string',
    },
  },
}

Without this PR, to access the cost of each option, you'd need to do:

const { fields } = createHeadlessForm(demoAbove);

const cost = fields[0].options[0]['x-jsf-presentation'].cost

Now, you can access it directly with:

const cost = fields[0].options[0].cost

In other words, now we move the x-jsf-presentation's data to the closest parent, removing the key itself, similar to how we already do with the root (check layout to compare). The fields[0] would look like this:

{
  "name": "health_perks",
  "label": "Health perks",
  "options": [
    {
      "label": "Basic",
      "value": "basic",
      "cost": "$30.00/mo", // <-- moved to parent
      "x-another": "extra-thing" // <-- still accepts any other arbitrary key
    },
    {
      "label": "Standard",
      "value": "standard",
      "cost": "$50.00/mo"
    }
  ],
  "inputType": "radio",
  "jsonType": "string",
  "layout": "vertical"  // <-- the new behavior is similar to this existing one in the field root
}

Related

@@ -406,12 +406,27 @@ export function updateFieldsProperties(fields, formValues, jsonSchema) {

const notNullOption = (opt) => opt.const !== null;

function flatPresentation(item) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ready for review :)

const: 'standard',
title: 'Standard',
'x-jsf-presentation': {
carrierName: 'Vanilla Lda',
Copy link
Collaborator

@brennj brennj Jun 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for consistency sake in regards to internal discussions, should we wrap this in meta just to have everything aligned in case someone combs through the code?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this is the only comment i have, i'm happy with this otherwise)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, done!

Copy link
Collaborator

@brennj brennj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets go!

@sandrina-p sandrina-p merged commit 367688c into main Jun 22, 2023
@sandrina-p sandrina-p deleted the field-options-x-keywords-rename branch June 22, 2023 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants