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

#2870 Add ability to pass in an array variable or template for dropdown field options #2915

Merged
merged 18 commits into from
Mar 15, 2022

Conversation

BALEHOK
Copy link
Contributor

@BALEHOK BALEHOK commented Mar 9, 2022

Relates to #2870

Array of options with a variable (preview):
Screen Shot 2022-03-09 at 6 38 54 PM

Array of options with a variable (rendered):
Screen Shot 2022-03-09 at 6 38 41 PM

Set option labels (@options holds the indexes of the source array and its values):
Screen Shot 2022-03-15 at 8 47 22 PM

Screen Shot 2022-03-15 at 8 46 44 PM

  • Pass in an array variable containing all the option strings
  • Pass in an object variable, using the keys as enum and the values as enumNames in the json-schema (maybe this should be opposite with how keys/values are used?)
  • Use a (string) variable for one of the options
  • Set a template string for one of the options
  • Allow hard-coding text for separate option-value and display-label (enum vs enumName in json-schema) for an individual option
  • Allow passing in variables for the value and label for an individual option
  • Allow setting string templates for the value and label for an individual option
  • Allow passing in two array variables at the top level, one for enum and one for enumNames, with the array order dictating which value goes to which name.

@BALEHOK BALEHOK requested a review from BLoe March 9, 2022 19:21
<FormGroup>
<FormLabel>{props.schema.title}</FormLabel>
<div className="text-muted form-text">
Dropdown options defined by a variable are not displayed in preview.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we could show a disabled input here with the variable name or something to indicate in the preview which variable is wired up, like when you put a variable in one of the options?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks great, thanks!

@BALEHOK BALEHOK requested a review from BLoe March 11, 2022 13:41
/>

<SchemaField
label="Option labels"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The number of items here not necessarily should be equal to the number of options, RJSF is fine with that.

@BALEHOK BALEHOK requested a review from twschiller March 11, 2022 13:45
Comment on lines 257 to 266
<SchemaField
label="Options"
name={getFullFieldName("enum")}
schema={{
type: "array",
items: {
type: "string",
},
}}
/>
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the Options field be required here?

Copy link
Contributor Author

@BALEHOK BALEHOK Mar 11, 2022

Choose a reason for hiding this comment

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

This brings us to our discussion of a required array field. Technically I can leave the field blank and see a dropdown with no options.
Let's first define what a "required array field" means. Is an empty array okay?
Then let's think if we need to mark this field as required.

Copy link
Contributor

@BLoe BLoe Mar 11, 2022

Choose a reason for hiding this comment

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

Ah yeah, because you default it to an empty array in the schema already, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep

Comment on lines 28 to 29
// @ts-expect-error -- enumNames is a valid property of the RJSF schema.
const { enumNames: labels } = props.schema;
Copy link
Contributor

Choose a reason for hiding this comment

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

It's odd that the schema type from the json schema library doesn't have the enumNames field. Do you know if we're using an out of date version or something like that?

Copy link
Contributor

Choose a reason for hiding this comment

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

The spec for json schema also doesn't mention it 🤔

https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01

Apparently it was a proposal at some point that was rejected from being added to the spec. I found this issue thread of people discussing it:
rjsf-team/react-jsonschema-form#532

Should we use the proposed oneOf implementation from that thread as opposed to enumNames? I understand this is supported by react-jsonschema-form, but what are the implications here if we deviate from the core json schema spec? Does this affect anything else like yup validation or anything like that? Are we blocking ourselves from using other tools/libraries in the future that only support the spec? Just want to be thorough here before we make the decision to diverge from the core spec... Also, it will be annoying to handle type issues with this going forward.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good one, I'll check this!

Copy link
Contributor

@twschiller twschiller Mar 13, 2022

Choose a reason for hiding this comment

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

@BALEHOK Ben and I confirmed that this approach works in RJSF (PR was merged in here: rjsf-team/react-jsonschema-form#581)

We should use the const keyword form:

properties:
   choiceField:
      oneOf:
         - const: a
           title:  Choice A
         - const: b
           title: Choice B

Yup: RJSF doesn't use Yup (the do their own validation, probably directly vs. the JSON Schema)

Considerations:

  • If the form definition is currently using enum (is from the workshop), should be easy to convert on mount?
  • If the existing form uses enum + enumNames, you can either convert on mount, or show the edit in workshop message

<FormGroup>
<FormLabel>{props.schema.title}</FormLabel>
<div className="text-muted form-text">
Dropdown options defined by a variable are not displayed in preview.
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks great, thanks!

@twschiller
Copy link
Contributor

Overall looking good. Found a couple bugs:

  1. Getting a preview error when switching the initial field to a dropdown with labels

image

  1. If you've passed a variable value, you can't switch from Dropdown to Dropdown with Labels

image

image

@twschiller twschiller added this to the 1.5.7 milestone Mar 15, 2022
@BALEHOK BALEHOK force-pushed the feature/2870-dd-variable-support branch from ee5f609 to d9ba8b3 Compare March 15, 2022 19:50
userEvent.click(textOption);
});
await selectSchemaFieldType(
`${RJSF_SCHEMA_PROPERTY_NAME}.schema.properties.${fieldName}.default`,
Copy link
Contributor

@twschiller twschiller Mar 15, 2022

Choose a reason for hiding this comment

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

This should probably be be using joinName to correctly handle spaces/special characters in field names (unless we block them on the entry side)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, this is a test file, do you think it can be a problem in the test?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oops, didn't notice the file name! This is perfectly ok here

Copy link
Contributor

@twschiller twschiller left a comment

Choose a reason for hiding this comment

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

Thanks for the tests! I noticed one spot that might have problems with some field names. But not a blocker to getting this merged in

@BALEHOK BALEHOK enabled auto-merge (squash) March 15, 2022 21:31
@BALEHOK BALEHOK merged commit dda063e into main Mar 15, 2022
@BALEHOK BALEHOK deleted the feature/2870-dd-variable-support branch March 15, 2022 21:35
@fregante fregante mentioned this pull request Jan 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants