-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add story for dependent async selects (#2078)
- Loading branch information
1 parent
6ce4a19
commit fef890a
Showing
7 changed files
with
323 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
"@comet/admin": minor | ||
--- | ||
|
||
Add `OnChangeField` helper to listen to field changes | ||
|
||
**Example** | ||
|
||
```tsx | ||
<OnChangeField name="product"> | ||
{(value, previousValue) => { | ||
// Will be called when field 'product' changes | ||
}} | ||
</OnChangeField> | ||
``` |
138 changes: 138 additions & 0 deletions
138
packages/admin/admin/src/form/helpers/OnChangeField.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,138 @@ | ||
// Copied from https://github.com/final-form/react-final-form-listeners/blob/master/src/OnChange.test.js | ||
import { cleanup, fireEvent, render } from "@testing-library/react"; | ||
import React from "react"; | ||
import { Field, Form } from "react-final-form"; | ||
|
||
import { OnChangeField } from "./OnChangeField"; | ||
|
||
const onSubmitMock = () => { | ||
// Noop | ||
}; | ||
|
||
describe("OnChangeField", () => { | ||
afterEach(cleanup); | ||
|
||
it("should not call listener on first render", () => { | ||
const spy = jest.fn(); | ||
render( | ||
<Form onSubmit={onSubmitMock} initialValues={{ foo: "bar" }}> | ||
{() => <OnChangeField name="foo">{spy}</OnChangeField>} | ||
</Form>, | ||
); | ||
expect(spy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should call listener when going from uninitialized to value", () => { | ||
const spy = jest.fn(); | ||
const { getByTestId } = render( | ||
<Form onSubmit={onSubmitMock}> | ||
{() => ( | ||
<form> | ||
<Field name="name" component="input" data-testid="name" /> | ||
<OnChangeField name="name">{spy}</OnChangeField> | ||
</form> | ||
)} | ||
</Form>, | ||
); | ||
expect(spy).not.toHaveBeenCalled(); | ||
fireEvent.change(getByTestId("name"), { target: { value: "erikras" } }); | ||
expect(spy).toHaveBeenCalled(); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy).toHaveBeenCalledWith("erikras", ""); | ||
}); | ||
|
||
it("should call listener when going from initialized to value", () => { | ||
const spy = jest.fn(); | ||
const { getByTestId } = render( | ||
<Form onSubmit={onSubmitMock} initialValues={{ name: "erik" }}> | ||
{() => ( | ||
<form> | ||
<Field name="name" component="input" data-testid="name" /> | ||
<OnChangeField name="name">{spy}</OnChangeField> | ||
</form> | ||
)} | ||
</Form>, | ||
); | ||
expect(spy).not.toHaveBeenCalled(); | ||
fireEvent.change(getByTestId("name"), { target: { value: "erikras" } }); | ||
expect(spy).toHaveBeenCalled(); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy).toHaveBeenCalledWith("erikras", "erik"); | ||
}); | ||
|
||
it("should call listener when changing to null", () => { | ||
const spy = jest.fn(); | ||
const { getByTestId } = render( | ||
<Form onSubmit={onSubmitMock} initialValues={{ name: "erikras" }}> | ||
{() => ( | ||
<form> | ||
<Field name="name" component="input" data-testid="name" /> | ||
<OnChangeField name="name">{spy}</OnChangeField> | ||
</form> | ||
)} | ||
</Form>, | ||
); | ||
expect(spy).not.toHaveBeenCalled(); | ||
fireEvent.change(getByTestId("name"), { target: { value: null } }); | ||
expect(spy).toHaveBeenCalled(); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy).toHaveBeenCalledWith("", "erikras"); | ||
}); | ||
|
||
it("should handle quick subsequent changes properly", () => { | ||
const toppings = ["Pepperoni", "Mushrooms", "Olives"]; | ||
const { getByTestId } = render( | ||
<Form onSubmit={onSubmitMock}> | ||
{({ form }) => ( | ||
<React.Fragment> | ||
<Field name="everything" component="input" type="checkbox" data-testid="everything" /> | ||
<OnChangeField name="everything"> | ||
{(next) => { | ||
if (next) { | ||
return form.change("toppings", toppings); | ||
} | ||
}} | ||
</OnChangeField> | ||
{toppings.length > 0 && | ||
toppings.map((topping, index) => { | ||
return ( | ||
<Field component="input" key={topping} name="toppings" value={topping} type="checkbox" data-testid={topping} /> | ||
); | ||
})} | ||
<OnChangeField name="toppings"> | ||
{(next) => { | ||
form.change("everything", next && next.length === toppings.length); | ||
}} | ||
</OnChangeField> | ||
</React.Fragment> | ||
)} | ||
</Form>, | ||
); | ||
expect((getByTestId("everything") as HTMLInputElement).checked).toBe(false); | ||
expect((getByTestId("Pepperoni") as HTMLInputElement).checked).toBe(false); | ||
expect((getByTestId("Mushrooms") as HTMLInputElement).checked).toBe(false); | ||
expect((getByTestId("Olives") as HTMLInputElement).checked).toBe(false); | ||
|
||
fireEvent.click(getByTestId("Pepperoni")); | ||
expect((getByTestId("Pepperoni") as HTMLInputElement).checked).toBe(true); | ||
expect((getByTestId("everything") as HTMLInputElement).checked).toBe(false); | ||
|
||
fireEvent.click(getByTestId("Mushrooms")); | ||
expect((getByTestId("Mushrooms") as HTMLInputElement).checked).toBe(true); | ||
expect((getByTestId("everything") as HTMLInputElement).checked).toBe(false); | ||
|
||
fireEvent.click(getByTestId("Olives")); | ||
expect((getByTestId("Olives") as HTMLInputElement).checked).toBe(true); | ||
expect((getByTestId("everything") as HTMLInputElement).checked).toBe(true); | ||
|
||
fireEvent.click(getByTestId("Olives")); | ||
expect((getByTestId("Olives") as HTMLInputElement).checked).toBe(false); | ||
expect((getByTestId("everything") as HTMLInputElement).checked).toBe(false); | ||
|
||
fireEvent.click(getByTestId("everything")); | ||
expect((getByTestId("Pepperoni") as HTMLInputElement).checked).toBe(true); | ||
expect((getByTestId("Mushrooms") as HTMLInputElement).checked).toBe(true); | ||
expect((getByTestId("Olives") as HTMLInputElement).checked).toBe(true); | ||
expect((getByTestId("everything") as HTMLInputElement).checked).toBe(true); | ||
}); | ||
}); |
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,21 @@ | ||
// Inspired by https://github.com/final-form/react-final-form-listeners/blob/master/src/OnChange.js | ||
import { useEffect, useRef } from "react"; | ||
import { useField } from "react-final-form"; | ||
|
||
type Props = { name: string; children: (value: any, previousValue: any) => void }; | ||
|
||
function OnChangeField({ name, children }: Props) { | ||
const { input } = useField(name); | ||
const previousValue = useRef(input.value); | ||
|
||
useEffect(() => { | ||
if (input.value !== previousValue.current) { | ||
children(input.value, previousValue.current); | ||
previousValue.current = input.value; | ||
} | ||
}, [input.value, children]); | ||
|
||
return null; | ||
} | ||
|
||
export { OnChangeField }; |
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
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
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
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,82 @@ | ||
import { gql, useApolloClient } from "@apollo/client"; | ||
import { AsyncSelectField, FinalForm, OnChangeField } from "@comet/admin"; | ||
import { Box } from "@mui/material"; | ||
import { storiesOf } from "@storybook/react"; | ||
import * as React from "react"; | ||
|
||
import { apolloStoryDecorator } from "../../apollo-story.decorator"; | ||
import { Manufacturer, Product } from "../../mocks/handlers"; | ||
|
||
interface FormValues { | ||
manufacturer?: Manufacturer; | ||
product?: Product; | ||
} | ||
|
||
storiesOf("@comet/admin/form", module) | ||
.addDecorator(apolloStoryDecorator("/graphql")) | ||
.add("Dependent async selects", function () { | ||
const client = useApolloClient(); | ||
|
||
return ( | ||
<Box maxWidth={400}> | ||
<FinalForm<FormValues> | ||
mode="add" | ||
onSubmit={() => { | ||
// Noop | ||
}} | ||
> | ||
{({ values, form }) => ( | ||
<> | ||
<AsyncSelectField | ||
name="manufacturer" | ||
loadOptions={async () => { | ||
const { data } = await client.query({ | ||
query: gql` | ||
query Manufacturers { | ||
manufacturers { | ||
id | ||
name | ||
} | ||
} | ||
`, | ||
}); | ||
|
||
return data.manufacturers; | ||
}} | ||
getOptionLabel={(option: Manufacturer) => option.name} | ||
label="Manufacturer" | ||
fullWidth | ||
/> | ||
<AsyncSelectField | ||
name="product" | ||
loadOptions={async () => { | ||
const { data } = await client.query({ | ||
query: gql` | ||
query Products($manufacturer: ID) { | ||
products(manufacturer: $manufacturer) { | ||
id | ||
name | ||
} | ||
} | ||
`, | ||
variables: { manufacturer: values.manufacturer?.id }, | ||
}); | ||
|
||
return data.products; | ||
}} | ||
getOptionLabel={(option: Product) => option.name} | ||
label="Product" | ||
fullWidth | ||
disabled={!values.manufacturer} | ||
/> | ||
<OnChangeField name="manufacturer"> | ||
{() => { | ||
form.change("product", undefined); | ||
}} | ||
</OnChangeField> | ||
</> | ||
)} | ||
</FinalForm> | ||
</Box> | ||
); | ||
}); |