-
Notifications
You must be signed in to change notification settings - Fork 2
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: List component - basic model, types & UI #3197
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eec2d34
feat: Basic model and Editor modal
DafyddLlyr f220353
fix: Move feature flag to Node to avoid 'conditional hooks' warning
DafyddLlyr 51bfc1a
feat: Basic UI structure
DafyddLlyr f07dcee
test: Fix a11y test and add todos
DafyddLlyr 0243169
test: Basic test of form structure being build from test schema
DafyddLlyr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
editor.planx.uk/src/@planx/components/List/Public.test.tsx
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
137 changes: 137 additions & 0 deletions
137
editor.planx.uk/src/@planx/components/List/Public/Fields.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import Box from "@mui/material/Box"; | ||
import FormControl from "@mui/material/FormControl"; | ||
import FormLabel from "@mui/material/FormLabel"; | ||
import MenuItem from "@mui/material/MenuItem"; | ||
import RadioGroup from "@mui/material/RadioGroup"; | ||
import React from "react"; | ||
import SelectInput from "ui/editor/SelectInput"; | ||
import InputLabel from "ui/public/InputLabel"; | ||
import Input from "ui/shared/Input"; | ||
import InputRowLabel from "ui/shared/InputRowLabel"; | ||
|
||
import { DESCRIPTION_TEXT } from "../../shared/constants"; | ||
import BasicRadio from "../../shared/Radio/BasicRadio"; | ||
import type { NumberField, QuestionField, TextField } from "../model"; | ||
|
||
type Props<T> = T & { id: string }; | ||
|
||
export const TextFieldInput: React.FC<Props<TextField>> = ({ | ||
id, | ||
data, | ||
required, | ||
}) => ( | ||
<InputLabel label={data.title} htmlFor={id}> | ||
<Input | ||
type={((type) => { | ||
if (type === "email") return "email"; | ||
else if (type === "phone") return "tel"; | ||
return "text"; | ||
})(data.type)} | ||
multiline={data.type && ["long", "extraLong"].includes(data.type)} | ||
bordered | ||
id={id} | ||
rows={ | ||
data.type && ["long", "extraLong"].includes(data.type) ? 5 : undefined | ||
} | ||
name="text" | ||
required={required} | ||
inputProps={{ | ||
"aria-describedby": [ | ||
data.description ? DESCRIPTION_TEXT : "", | ||
// TODO: When handling errors, revisit this | ||
// formik.errors.text ? `${ERROR_MESSAGE}-${inputFieldId}` : "", | ||
] | ||
.filter(Boolean) | ||
.join(" "), | ||
}} | ||
/> | ||
</InputLabel> | ||
); | ||
|
||
export const NumberFieldInput: React.FC<Props<NumberField>> = ({ | ||
id, | ||
data, | ||
required, | ||
}) => ( | ||
<InputLabel label={data.title} htmlFor={id}> | ||
<Box sx={{ display: "flex", alignItems: "baseline" }}> | ||
<Input | ||
required={required} | ||
bordered | ||
name="value" | ||
type="number" | ||
// value={formik.values.value} | ||
// onChange={formik.handleChange} | ||
// errorMessage={formik.errors.value as string} | ||
inputProps={{ | ||
"aria-describedby": [ | ||
data.description ? DESCRIPTION_TEXT : "", | ||
// formik.errors.value ? `${ERROR_MESSAGE}-${props.id}` : "", | ||
] | ||
.filter(Boolean) | ||
.join(" "), | ||
}} | ||
id={id} | ||
/> | ||
{data.units && <InputRowLabel>{data.units}</InputRowLabel>} | ||
</Box> | ||
</InputLabel> | ||
); | ||
|
||
export const RadioFieldInput: React.FC<Props<QuestionField>> = ({ | ||
id, | ||
data, | ||
}) => ( | ||
<FormControl sx={{ width: "100%" }} component="fieldset"> | ||
<FormLabel | ||
component="legend" | ||
id={`radio-buttons-group-label-${id}`} | ||
sx={(theme) => ({ | ||
color: theme.palette.text.primary, | ||
"&.Mui-focused": { | ||
color: theme.palette.text.primary, | ||
}, | ||
})} | ||
> | ||
{data.title} | ||
</FormLabel> | ||
{/* <ErrorWrapper id={props.id} error={formik.errors.selected?.id}> */} | ||
<RadioGroup | ||
aria-labelledby={`radio-buttons-group-label-${id}`} | ||
name={`radio-buttons-group-${id}`} | ||
sx={{ p: 1 }} | ||
// value={formik.values.selected.id} | ||
> | ||
{data.options.map(({ id, data }) => ( | ||
<BasicRadio | ||
key={id} | ||
id={id} | ||
title={data.text} | ||
onChange={() => console.log("change radio")} | ||
/> | ||
))} | ||
</RadioGroup> | ||
{/* </ErrorWrapper> */} | ||
</FormControl> | ||
); | ||
|
||
export const SelectFieldInput: React.FC<Props<QuestionField>> = ({ | ||
id, | ||
data, | ||
required, | ||
}) => ( | ||
<InputLabel label={data.title} id={`select-label-${id}`}> | ||
<SelectInput | ||
bordered | ||
required={required} | ||
title={data.title} | ||
labelId={`select-label-${id}`} | ||
> | ||
{data.options.map((option) => ( | ||
<MenuItem key={option.id} value={option.data.text}> | ||
{option.data.text} | ||
</MenuItem> | ||
))} | ||
</SelectInput> | ||
</InputLabel> | ||
); |
116 changes: 116 additions & 0 deletions
116
editor.planx.uk/src/@planx/components/List/Public/index.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import React from "react"; | ||
import { axe, setup } from "testUtils"; | ||
|
||
import ListComponent, { Props } from "../Public"; | ||
import { Zoo } from "../schemas/Zoo"; | ||
|
||
const mockProps: Props = { | ||
fn: "mock", | ||
schema: Zoo, | ||
schemaName: "Zoo", | ||
title: "Mock Title", | ||
description: "Mock description", | ||
}; | ||
|
||
describe("Basic UI", () => { | ||
it("renders correctly", () => { | ||
const { getByText } = setup(<ListComponent {...mockProps} />); | ||
|
||
expect(getByText(/Mock Title/)).toBeInTheDocument(); | ||
expect(getByText(/Mock description/)).toBeInTheDocument(); | ||
}); | ||
|
||
it("parses provided schema to render expected form", async () => { | ||
const { getByLabelText, getByText, user, getByRole, queryAllByRole } = | ||
setup(<ListComponent {...mockProps} />); | ||
|
||
// Text inputs are generated from schema... | ||
const textInput = getByLabelText(/What's their name?/) as HTMLInputElement; | ||
expect(textInput).toBeInTheDocument(); | ||
expect(textInput.type).toBe("text"); | ||
|
||
// Props are correctly read | ||
const emailInput = getByLabelText( | ||
/What's their email address?/, | ||
) as HTMLInputElement; | ||
expect(emailInput).toBeInTheDocument(); | ||
expect(emailInput.type).toBe("email"); | ||
|
||
// Number inputs are generated from schema | ||
const numberInput = getByLabelText(/How old are they?/) as HTMLInputElement; | ||
expect(numberInput).toBeInTheDocument(); | ||
expect(numberInput.type).toBe("number"); | ||
|
||
// Props are correctly read | ||
const units = getByText(/years old/); | ||
expect(units).toBeInTheDocument(); | ||
|
||
// Question inputs generated from schema | ||
// Combobox displayed when number of options > 2 | ||
const selectInput = getByRole("combobox"); | ||
expect(selectInput).toBeInTheDocument(); | ||
|
||
// Open combobox | ||
await user.click(selectInput); | ||
|
||
// All options display | ||
expect(getByRole("option", { name: "Small" })).toBeInTheDocument(); | ||
expect(getByRole("option", { name: "Medium" })).toBeInTheDocument(); | ||
expect(getByRole("option", { name: "Large" })).toBeInTheDocument(); | ||
|
||
// No default option selected | ||
expect( | ||
queryAllByRole("option", { selected: true }) as HTMLOptionElement[], | ||
).toHaveLength(0); | ||
|
||
// Close combobox | ||
await user.click(selectInput); | ||
|
||
// Radio groups displayed when number of options = 2 | ||
const radioInput = getByLabelText(/How cute are they?/); | ||
expect(radioInput).toBeInTheDocument(); | ||
|
||
// All options display | ||
const radioButton1 = getByLabelText("Very") as HTMLInputElement; | ||
expect(radioButton1).toBeInTheDocument(); | ||
expect(radioButton1.type).toBe("radio"); | ||
|
||
const radioButton2 = getByLabelText("Super") as HTMLInputElement; | ||
expect(radioButton2).toBeInTheDocument(); | ||
expect(radioButton2.type).toBe("radio"); | ||
|
||
// No default option selected | ||
expect(radioButton1).not.toBeChecked(); | ||
expect(radioButton2).not.toBeChecked(); | ||
|
||
expect(getByText(/Save/, { selector: "button" })).toBeInTheDocument(); | ||
expect(getByText(/Cancel/, { selector: "button" })).toBeInTheDocument(); | ||
}); | ||
|
||
it("should not have any accessibility violations", async () => { | ||
const { container } = setup(<ListComponent {...mockProps} />); | ||
const results = await axe(container); | ||
expect(results).toHaveNoViolations(); | ||
}); | ||
}); | ||
|
||
describe("Navigating back", () => { | ||
test.todo("it pre-populates list correctly"); | ||
}); | ||
|
||
describe("Building a list", () => { | ||
test.todo("Adding an item"); | ||
test.todo("Editing an item"); | ||
test.todo("Removing an item"); | ||
}); | ||
|
||
describe("Form validation and error handling", () => { | ||
test.todo("Text field"); | ||
test.todo("Number field"); | ||
test.todo("Question field - select"); | ||
test.todo("Question field - radio"); | ||
}); | ||
|
||
describe("Payload generation", () => { | ||
it.todo("generates a valid payload on submission"); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
There's some repeated code here from existing components.
I'm hoping that once error handling / form values are handled I can DRY this up a little with more generic, shared, input components.