Skip to content

Commit 3c42170

Browse files
committed
Added Molecules Form
1 parent d4ec7ee commit 3c42170

4 files changed

Lines changed: 82 additions & 0 deletions

File tree

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export { AccessibleList } from "./molecules/accessible-list/accessible-list";
6262
export { Card } from "./molecules/cards/card";
6363
export { DropdownButton } from "./molecules/dropdown-button/dropdown-button";
6464
export { ErrorBanner } from "./molecules/errors/error-banner";
65+
export { Form } from "./molecules/forms/form";
6566

6667
// Form Fields
6768

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Faker from "faker";
2+
import { Form } from "./form";
3+
import React from "react";
4+
import { text } from "@storybook/addon-knobs";
5+
import { SubmitButton } from "../../atoms/forms/submit-button";
6+
7+
export default {
8+
component: Form,
9+
title: "Molecules | Forms / Form",
10+
};
11+
12+
const renderChildren = () => (
13+
<React.Fragment>
14+
<div className="c-form-field">
15+
<label>
16+
{Faker.hacker.noun()}
17+
<input type="text"></input>
18+
</label>
19+
</div>
20+
21+
<SubmitButton />
22+
</React.Fragment>
23+
);
24+
25+
export const formDefault = () => (
26+
<Form onSubmit={() => alert("form submitted")}>{renderChildren()}</Form>
27+
);
28+
29+
export const formKnobs = () => (
30+
<Form
31+
buttonText={text("buttonText", "Submit")}
32+
cssClassName={text("cssClassName", "c-form")}
33+
onSubmit={() => alert("Form Submitted")}>
34+
{renderChildren()}
35+
</Form>
36+
);

src/molecules/forms/form.test.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from "react";
2+
import { render } from "@testing-library/react";
3+
import { Form } from "./form";
4+
import faker from "faker";
5+
6+
describe("Form", () => {
7+
test("when default props, renders children", () => {
8+
// Arrange
9+
const expected = faker.random.words();
10+
11+
// Act
12+
const { getByText } = render(
13+
<Form onSubmit={() => {}}>{expected}</Form>
14+
);
15+
16+
// Assert
17+
expect(getByText(expected)).not.toBeNull();
18+
});
19+
});

src/molecules/forms/form.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as React from "react";
2+
import { PropsWithChildren } from "react";
3+
4+
const COMPONENT_CLASS = "c-form";
5+
6+
interface FormProps {
7+
action?: string;
8+
buttonText?: string;
9+
cssClassName?: string;
10+
id?: string;
11+
method?: string;
12+
onSubmit: (e: React.FormEvent<HTMLFormElement>) => void;
13+
}
14+
15+
const Form: React.FC<FormProps> = (props: PropsWithChildren<FormProps>) => (
16+
<form
17+
action={props.action}
18+
className={props.cssClassName ?? COMPONENT_CLASS}
19+
id={props.id}
20+
method={props.method}
21+
onSubmit={props.onSubmit}>
22+
{props.children}
23+
</form>
24+
);
25+
26+
export { Form };

0 commit comments

Comments
 (0)