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

WD-8546 - Create secrets #1693

Merged
merged 4 commits into from
Feb 5, 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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"react-scripts": "5.0.1",
"react-useportal": "1.0.19",
"redux": "5.0.1",
"vanilla-framework": "4.5.1"
"vanilla-framework": "4.5.1",
"yup": "1.3.3"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the validation library that gets used in our other projects that use Formik (I was surprised to see it wasn't being used already).

},
"devDependencies": {
"@babel/plugin-proposal-private-property-in-object": "7.21.11",
Expand Down
50 changes: 50 additions & 0 deletions src/components/FormikField/FormikField.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { render, screen } from "@testing-library/react";
import { Formik } from "formik";

import FormikField from "./FormikField";

describe("FormikField", () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests were copied from MAAS.

it("can set a different component", () => {
const Component = () => <select />;
render(
<Formik initialValues={{}} onSubmit={jest.fn()}>
<FormikField component={Component} name="username" />
</Formik>,
);

expect(screen.getByRole("combobox")).toBeInTheDocument();
expect(screen.queryByRole("textbox")).not.toBeInTheDocument();
});

it("can pass errors", () => {
render(
<Formik
initialErrors={{ username: "Uh oh!" }}
initialTouched={{ username: true }}
initialValues={{ username: "" }}
onSubmit={jest.fn()}
>
<FormikField name="username" />
</Formik>,
);

expect(screen.getByRole("textbox")).toHaveAccessibleErrorMessage(
"Error: Uh oh!",
);
});

it("can hide the errors", () => {
render(
<Formik
initialErrors={{ username: "Uh oh!" }}
initialTouched={{ username: true }}
initialValues={{ username: "" }}
onSubmit={jest.fn()}
>
<FormikField displayError={false} name="username" />
</Formik>,
);

expect(screen.getByRole("textbox")).not.toHaveAccessibleErrorMessage();
});
});
43 changes: 43 additions & 0 deletions src/components/FormikField/FormikField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Input } from "@canonical/react-components";
import { useField } from "formik";
import {
useId,
type ComponentProps,
type ComponentType,
type ElementType,
type HTMLProps,
} from "react";

export type Props<C extends ElementType | ComponentType = typeof Input> = {
component?: C;
displayError?: boolean;
name: string;
value?: HTMLProps<HTMLElement>["value"];
} & ComponentProps<C>;

const FormikField = <C extends ElementType | ComponentType = typeof Input>({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This component was copied from MAAS.

component: Component = Input,
displayError = true,
name,
value,
label,
...props
}: Props<C>): JSX.Element => {
const id = useId();
const [field, meta] = useField({ name, type: props.type, value });

return (
<Component
aria-label={label}
error={meta.touched && displayError ? meta.error : null}
// Have to manually set the id until this issue has been fixed:
// https://github.com/canonical/react-components/issues/957
id={id}
label={label}
{...field}
{...props}
/>
);
};

export default FormikField;
Empty file.
Loading
Loading