-
Notifications
You must be signed in to change notification settings - Fork 22
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
Changes from 4 commits
2daaddc
e96fa49
e16575b
431b87b
37f5531
de665d2
5be51ed
16deb54
d82da8c
d335cb8
cb408b7
5761b04
573b231
d9ba8b3
8e0975c
f145e93
da2c141
02b25da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,8 @@ import { | |
RJSFSchema, | ||
SelectStringOption, | ||
SetActiveField, | ||
} from "./formBuilderTypes"; | ||
import { UI_WIDGET } from "./schemaFieldNames"; | ||
} from "@/components/formBuilder/formBuilderTypes"; | ||
import { UI_WIDGET } from "@/components/formBuilder/schemaFieldNames"; | ||
import { | ||
FIELD_TYPES_WITHOUT_DEFAULT, | ||
FIELD_TYPE_OPTIONS, | ||
|
@@ -34,15 +34,13 @@ import { | |
stringifyUiType, | ||
UiType, | ||
validateNextPropertyName, | ||
} from "./formBuilderHelpers"; | ||
} from "@/components/formBuilder/formBuilderHelpers"; | ||
import { Schema, SchemaPropertyType } from "@/core"; | ||
import ConnectedFieldTemplate from "@/components/form/ConnectedFieldTemplate"; | ||
import FieldTemplate from "@/components/form/FieldTemplate"; | ||
import { produce } from "immer"; | ||
import SelectWidget, { | ||
SelectWidgetOnChange, | ||
} from "@/components/form/widgets/SelectWidget"; | ||
import OptionsWidget from "@/components/form/widgets/OptionsWidget"; | ||
import SwitchButtonWidget, { | ||
CheckBoxLike, | ||
} from "@/components/form/widgets/switchButton/SwitchButtonWidget"; | ||
|
@@ -255,11 +253,29 @@ const FieldEditor: React.FC<{ | |
)} | ||
|
||
{propertySchema.enum && ( | ||
<ConnectedFieldTemplate | ||
name={getFullFieldName("enum")} | ||
label="Options" | ||
as={OptionsWidget} | ||
/> | ||
<> | ||
<SchemaField | ||
label="Options" | ||
name={getFullFieldName("enum")} | ||
schema={{ | ||
type: "array", | ||
items: { | ||
type: "string", | ||
}, | ||
}} | ||
/> | ||
|
||
<SchemaField | ||
label="Option labels" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
name={getFullFieldName("enumNames")} | ||
schema={{ | ||
type: "array", | ||
items: { | ||
type: "string", | ||
}, | ||
}} | ||
/> | ||
</> | ||
)} | ||
|
||
<FieldTemplate | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (C) 2022 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { WidgetProps } from "@rjsf/core"; | ||
import { Theme as RjsfTheme } from "@rjsf/bootstrap-4"; | ||
import React from "react"; | ||
|
||
const RjsfSelectWidget = RjsfTheme.widgets.SelectWidget; | ||
|
||
const SelectWidgetPreview: React.VFC<WidgetProps> = (props) => { | ||
// If Select Options is a variable, then `props.schema.enum` holds the name of the variable (i.e. string). | ||
const { enum: values } = props.schema; | ||
if (typeof values === "string") { | ||
// @ts-expect-error -- enumNames is a valid property of the RJSF schema. | ||
const { enumNames: labels } = props.schema; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: Should we use the proposed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good one, I'll check this! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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:
|
||
const enumOptions = [ | ||
{ | ||
value: values, | ||
label: typeof labels === "string" ? labels : values, | ||
}, | ||
]; | ||
|
||
const schema = { | ||
...props.schema, | ||
enum: [values], | ||
enumNames: typeof labels === "string" ? labels : undefined, | ||
}; | ||
|
||
return ( | ||
<RjsfSelectWidget | ||
{...props} | ||
disabled | ||
options={{ enumOptions }} | ||
schema={schema} | ||
value={values} | ||
/> | ||
); | ||
} | ||
|
||
return <RjsfSelectWidget {...props} />; | ||
}; | ||
|
||
export default SelectWidgetPreview; |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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
.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep