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: Remove optional/required distinction in List schema #3332

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 9 additions & 11 deletions editor.planx.uk/src/@planx/components/List/Public/Fields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ type Props<T> = T & { id: string };
export const TextFieldInput: React.FC<Props<TextField>> = ({
id,
data,
required,
}) => {
const { formik, activeIndex } = useListContext();

return (
<InputLabel
label={required === false ? data.title + " (optional)" : data.title}
label={data.title}
htmlFor={id}
>
<Input
Expand All @@ -50,7 +49,7 @@ export const TextFieldInput: React.FC<Props<TextField>> = ({
data.type && ["long", "extraLong"].includes(data.type) ? 5 : undefined
}
name={`userData[${activeIndex}]['${data.fn}']`}
required={required}
required
inputProps={{
"aria-describedby": [
data.description ? DESCRIPTION_TEXT : "",
Expand All @@ -69,18 +68,17 @@ export const TextFieldInput: React.FC<Props<TextField>> = ({
export const NumberFieldInput: React.FC<Props<NumberField>> = ({
id,
data,
required,
}) => {
const { formik, activeIndex } = useListContext();

return (
<InputLabel
label={required === false ? data.title + " (optional)" : data.title}
label={data.title}
htmlFor={id}
>
<Box sx={{ display: "flex", alignItems: "baseline" }}>
<Input
required={required}
required
bordered
name={`userData[${activeIndex}]['${data.fn}']`}
type="number"
Expand Down Expand Up @@ -110,7 +108,7 @@ export const NumberFieldInput: React.FC<Props<NumberField>> = ({

export const RadioFieldInput: React.FC<Props<QuestionField>> = (props) => {
const { formik, activeIndex } = useListContext();
const { id, data, required } = props;
const { id, data } = props;

return (
<FormControl sx={{ width: "100%" }} component="fieldset">
Expand All @@ -124,7 +122,7 @@ export const RadioFieldInput: React.FC<Props<QuestionField>> = (props) => {
},
})}
>
{required === false ? data.title + " (optional)" : data.title}
{data.title}
</FormLabel>
<ErrorWrapper
id={`${id}-error`}
Expand Down Expand Up @@ -152,7 +150,7 @@ export const RadioFieldInput: React.FC<Props<QuestionField>> = (props) => {

export const SelectFieldInput: React.FC<Props<QuestionField>> = (props) => {
const { formik, activeIndex } = useListContext();
const { id, data, required } = props;
const { id, data } = props;

const isDisabled = (option: Option) => {
if (!props.unique) return false;
Expand All @@ -168,7 +166,7 @@ export const SelectFieldInput: React.FC<Props<QuestionField>> = (props) => {

return (
<InputLabel
label={required === false ? data.title + " (optional)" : data.title}
label={data.title}
id={`select-label-${id}`}
>
<ErrorWrapper
Expand All @@ -177,7 +175,7 @@ export const SelectFieldInput: React.FC<Props<QuestionField>> = (props) => {
>
<SelectInput
bordered
required={required}
required
title={data.title}
labelId={`select-label-${id}`}
value={formik.values.userData[activeIndex][data.fn]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ describe("Form validation and error handling", () => {
test.todo("number fields use existing validation schemas");
test.todo("question fields use validation schema");
test.todo("unique constraints are enforced on question where this is set");
test.todo("optional fields can be empty when saving an item");
test.todo("an error displays if the minimum number of items is not met");
test.todo("an error displays if the maximum number of items is exceeded");
test.todo(
Expand Down
6 changes: 2 additions & 4 deletions editor.planx.uk/src/@planx/components/List/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,18 @@ const questionInputValidationSchema = (data: QuestionInput) =>
.oneOf(data.options.map((option) => option.data.val || option.data.text))
.required("Select your answer before continuing");

// TODO: Add summary fields for inactive view?
export type TextField = {
type: "text";
required?: boolean;
data: TextInput & { fn: string };
};

export type NumberField = {
type: "number";
required?: boolean;
data: NumberInput & { fn: string };
};

export type QuestionField = {
type: "question";
required?: boolean;
unique?: boolean;
data: QuestionInput & { fn: string };
};
Expand Down
Loading