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
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/fields/schemaFields/widgets/ArrayWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const ArrayWidget: React.FC<SchemaFieldProps> = ({ schema, name }) => {
<FieldArray name={name}>
{({ push }) => (
<>
<ul className="list-group">
<ul className="list-group mb-2">
{(field.value ?? []).map((item: unknown, index: number) => (
<li className="list-group-item py-1" key={index}>
<SchemaField
Expand Down
4 changes: 2 additions & 2 deletions src/components/formBuilder/FormBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import styles from "./FormBuilder.module.scss";

import React, { useState } from "react";
import FormEditor from "./FormEditor";
import FormPreview from "./FormPreview";
import FormEditor from "./edit/FormEditor";
import FormPreview from "./preview/FormPreview";
import { useField } from "formik";
import { RJSFSchema } from "@/components/formBuilder/formBuilderTypes";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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";
Expand Down Expand Up @@ -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",
},
}}
/>
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


<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.

name={getFullFieldName("enumNames")}
schema={{
type: "array",
items: {
type: "string",
},
}}
/>
</>
)}

<FieldTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import {
fireTextInput,
fireFormSubmit,
} from "@/tests/formHelpers";
import { RJSFSchema } from "./formBuilderTypes";
import { RJSFSchema } from "@/components/formBuilder/formBuilderTypes";
import FormEditor, { FormEditorProps } from "./FormEditor";
import {
initAddingFieldCases,
initOneFieldSchemaCase,
initRenamingCases,
} from "./formEditor.testCases";
} from "@/components/formBuilder/formEditor.testCases";
import selectEvent from "react-select-event";
import userEvent from "@testing-library/user-event";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
RJSFSchema,
SelectStringOption,
SetActiveField,
} from "./formBuilderTypes";
} from "@/components/formBuilder/formBuilderTypes";
import { Button, Col, Row } from "react-bootstrap";
import FieldEditor from "./FieldEditor";
import {
Expand All @@ -33,8 +33,8 @@ import {
normalizeUiOrder,
replaceStringInArray,
updateRjsfSchemaWithDefaultsIfNeeded,
} from "./formBuilderHelpers";
import { UI_ORDER } from "./schemaFieldNames";
} from "@/components/formBuilder/formBuilderHelpers";
import { UI_ORDER } from "@/components/formBuilder/schemaFieldNames";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faPlus, faTrash } from "@fortawesome/free-solid-svg-icons";
import { Schema } from "@/core";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@
import React, { useCallback, useEffect, useMemo, useState } from "react";
import JsonSchemaForm from "@rjsf/bootstrap-4";
import { FieldProps, IChangeEvent } from "@rjsf/core";
import { RJSFSchema, SetActiveField } from "./formBuilderTypes";
import {
RJSFSchema,
SetActiveField,
} from "@/components/formBuilder/formBuilderTypes";
import FormPreviewStringField from "./FormPreviewStringField";
import { UI_SCHEMA_ACTIVE } from "./schemaFieldNames";
import { UI_SCHEMA_ACTIVE } from "@/components/formBuilder/schemaFieldNames";
import { produce } from "immer";
import FormPreviewBooleanField from "./FormPreviewBooleanField";
import { getPreviewValues } from "@/components/fields/fieldUtils";
import ImageCropWidgetPreview from "@/components/formBuilder/ImageCropWidgetPreview";
import ImageCropWidgetPreview from "@/components/formBuilder/preview/ImageCropWidgetPreview";
// eslint-disable-next-line import/no-named-as-default -- need default export here
import DescriptionField from "./DescriptionField";
import FieldTemplate from "./FieldTemplate";
import DescriptionField from "@/components/formBuilder/DescriptionField";
import FieldTemplate from "@/components/formBuilder/FieldTemplate";
import SelectWidgetPreview from "./SelectWidgetPreview";

export type FormPreviewProps = {
rjsfSchema: RJSFSchema;
Expand Down Expand Up @@ -99,6 +103,7 @@ const FormPreview: React.FC<FormPreviewProps> = ({

const widgets = {
imageCrop: ImageCropWidgetPreview,
SelectWidget: SelectWidgetPreview,
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { utils } from "@rjsf/core";
import { Theme as RjsfTheme } from "@rjsf/bootstrap-4";
import React from "react";
import FormPreviewFieldTemplate, {
FormPreviewFieldProps,
} from "./FormPreviewFieldTemplate";
import styles from "./FormPreviewBooleanField.module.scss";

const RjsfBooleanField = utils.getDefaultRegistry().fields.BooleanField;
const RjsfBooleanField = RjsfTheme.fields.BooleanField;

const FormPreviewBooleanField: React.FC<FormPreviewFieldProps> = (props) => (
<FormPreviewFieldTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import styles from "./FormPreviewFieldTemplate.module.scss";

import { Field, FieldProps } from "@rjsf/core";
import React from "react";
import { SetActiveField } from "./formBuilderTypes";
import { UI_SCHEMA_ACTIVE } from "./schemaFieldNames";
import { SetActiveField } from "@/components/formBuilder/formBuilderTypes";
import { UI_SCHEMA_ACTIVE } from "@/components/formBuilder/schemaFieldNames";
import cx from "classnames";

export interface FormPreviewFieldProps extends FieldProps {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
.root {
input[type="checkbox"] {
margin-left: 0 !important;
}

:global(.form-group) {
margin-bottom: 0 !important;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { utils } from "@rjsf/core";
import { Theme as RjsfTheme } from "@rjsf/bootstrap-4";
import React from "react";
import FormPreviewFieldTemplate, {
FormPreviewFieldProps,
} from "./FormPreviewFieldTemplate";
import styles from "./FormPreviewBooleanField.module.scss";

const RjsfStringField = utils.getDefaultRegistry().fields.StringField;
const RjsfStringField = RjsfTheme.fields.StringField;

const FormPreviewStringField: React.FC<FormPreviewFieldProps> = (props) => (
<FormPreviewFieldTemplate
Expand Down
57 changes: 57 additions & 0 deletions src/components/formBuilder/preview/SelectWidgetPreview.tsx
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;
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

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;
2 changes: 1 addition & 1 deletion src/pageEditor/fields/FormModalOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import React, { useEffect } from "react";
import { validateRegistryId } from "@/types/helpers";
import { actions as formBuilderActions } from "@/pageEditor/slices/formBuilderSlice";
import formBuilderSelectors from "@/pageEditor/slices/formBuilderSelectors";
import FormEditor from "@/components/formBuilder/FormEditor";
import FormEditor from "@/components/formBuilder/edit/FormEditor";
import useReduxState from "@/hooks/useReduxState";
import ConfigErrorBoundary from "@/pageEditor/fields/ConfigErrorBoundary";

Expand Down
2 changes: 1 addition & 1 deletion src/pageEditor/fields/FormRendererOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import SchemaField from "@/components/fields/schemaFields/SchemaField";
import { Schema } from "@/core";
import React, { useEffect } from "react";
import { validateRegistryId } from "@/types/helpers";
import FormEditor from "@/components/formBuilder/FormEditor";
import FormEditor from "@/components/formBuilder/edit/FormEditor";
import { actions as elementWizardActions } from "@/pageEditor/slices/formBuilderSlice";
import formBuilderSelectors from "@/pageEditor/slices/formBuilderSelectors";
import useReduxState from "@/hooks/useReduxState";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import React, { useMemo, useState } from "react";
import { Alert, Col, Container, Nav, Row, Tab } from "react-bootstrap";
import { FormState } from "@/pageEditor/slices/editorSlice";
import { RJSFSchema } from "@/components/formBuilder/formBuilderTypes";
import FormEditor from "@/components/formBuilder/FormEditor";
import FormPreview from "@/components/formBuilder/FormPreview";
import FormEditor from "@/components/formBuilder/edit/FormEditor";
import FormPreview from "@/components/formBuilder/preview/FormPreview";
import Loader from "@/components/Loader";
import FieldRuntimeContext, {
RuntimeContext,
Expand Down
2 changes: 1 addition & 1 deletion src/pageEditor/tabs/editTab/dataPanel/DataPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { actions } from "@/pageEditor/slices/formBuilderSlice";
import { Alert, Button, Nav, Tab } from "react-bootstrap";
import JsonTree from "@/components/jsonTree/JsonTree";
import dataPanelStyles from "@/pageEditor/tabs/dataPanelTabs.module.scss";
import FormPreview from "@/components/formBuilder/FormPreview";
import FormPreview from "@/components/formBuilder/preview/FormPreview";
import ErrorBoundary from "@/components/ErrorBoundary";
import BlockPreview, {
usePreviewInfo,
Expand Down