-
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
#5717: swap native select widget for searchable select widget in Form Builder #5750
Changes from all commits
9035323
e1b0904
2ace9be
20c5413
f394f27
4f5319a
2b38fbd
9f6aac7
be8554e
f940aaf
f0aa566
9eb615d
a7fd808
4cc4464
1833100
9bc9efd
32ba79c
cc78960
c5b61d0
bd2fd18
8f2ed6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (C) 2023 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 React from "react"; | ||
import { type WidgetProps } from "@rjsf/core"; | ||
import Select from "react-select"; | ||
import { FormLabel, FormGroup } from "react-bootstrap"; | ||
|
||
type OptionType = { label: string; value: string }; | ||
|
||
const defaultOption: OptionType[] = []; | ||
|
||
const RjsfSelectWidget: React.FC<WidgetProps> = ({ | ||
schema, | ||
id, | ||
options, | ||
value, | ||
required, | ||
disabled, | ||
readonly, | ||
onChange, | ||
onBlur, | ||
onFocus, | ||
rawErrors, | ||
label, | ||
multiple, | ||
}) => { | ||
const _onChange = (option: OptionType | null) => { | ||
onChange(option ? option.value : ""); | ||
}; | ||
|
||
const _onBlur = () => { | ||
onBlur(id, value); | ||
}; | ||
|
||
const _onFocus = () => { | ||
onFocus(id, value); | ||
}; | ||
|
||
// Check to ensure enumOptions is an array | ||
// RJSF seems to occasionally set enumOptions as FALSE. Maybe related to async options: | ||
// https://github.com/rjsf-team/react-jsonschema-form/issues/415 | ||
const enumOptions = (options.enumOptions || defaultOption) as OptionType[]; | ||
|
||
const selectOptions = | ||
enumOptions.map(({ value, label }) => ({ | ||
value, | ||
label, | ||
})) ?? []; | ||
|
||
return ( | ||
<FormGroup> | ||
<FormLabel className={rawErrors?.length > 0 ? "text-danger" : ""}> | ||
{label || schema.title} | ||
{(label || schema.title) && required ? "*" : null} | ||
</FormLabel> | ||
<div data-testid="formbuilder-select-wrapper"> | ||
<Select | ||
id={id} | ||
options={selectOptions} | ||
isDisabled={disabled || readonly} | ||
isMulti={multiple} | ||
value={selectOptions.find((option) => option.value === value)} | ||
onChange={_onChange} | ||
onBlur={_onBlur} | ||
onFocus={_onFocus} | ||
/> | ||
</div> | ||
</FormGroup> | ||
); | ||
}; | ||
|
||
export default RjsfSelectWidget; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ import { unwrapTemplateExpressions } from "@/components/fields/fieldUtils"; | |
import ImageCropWidgetPreview from "@/components/formBuilder/preview/ImageCropWidgetPreview"; | ||
import DescriptionField from "@/components/formBuilder/DescriptionField"; | ||
import FieldTemplate from "@/components/formBuilder/FieldTemplate"; | ||
import SelectWidgetPreview from "./SelectWidgetPreview"; | ||
import RjsfSelectWidget from "@/components/formBuilder/RjsfSelectWidget"; | ||
import FormPreviewSchemaField from "./FormPreviewSchemaField"; | ||
import databaseSchema from "@schemas/database.json"; | ||
import googleSheetSchema from "@schemas/googleSheetId.json"; | ||
|
@@ -88,12 +88,10 @@ const FormPreview: React.FC<FormPreviewProps> = ({ | |
|
||
if (property.format === "preview") { | ||
// Intentionally setting a string value, not an array. @see FormPreviewSchemaField for details | ||
// @ts-expect-error -- intentionally assigning to a string | ||
property.enum = `[Mod Name] - ${activeField} database`; | ||
property.enum = [`[Mod Name] - ${activeField} database`]; | ||
} else { | ||
// Intentionally setting a string value, not an array. @see FormPreviewSchemaField for details | ||
// @ts-expect-error -- intentionally assigning to a string | ||
property.enum = "Select..."; | ||
Comment on lines
-91
to
-96
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. @BLoe here's my slightly-less-hacky solution. It should work the same way except it doesn't disable the field in the preview. 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. Cool, makes sense |
||
property.enum = ["Select..."]; | ||
} | ||
|
||
delete property.$ref; | ||
|
@@ -179,9 +177,9 @@ const FormPreview: React.FC<FormPreviewProps> = ({ | |
|
||
const widgets = { | ||
imageCrop: ImageCropWidgetPreview, | ||
database: SelectWidgetPreview, | ||
SelectWidget: SelectWidgetPreview, | ||
googleSheet: SelectWidgetPreview, | ||
database: RjsfSelectWidget, | ||
SelectWidget: RjsfSelectWidget, | ||
googleSheet: RjsfSelectWidget, | ||
}; | ||
|
||
return ( | ||
|
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 is fine for now as long as it works and we have test coverage and the tests are passing.
In the future, might be nice to try and cut these over to use React Testing Library more specific assertions/matchers, as opposed to using the
querySelector
escape hatch and asserting null or not.