2019-06-19 14:40:52 +00:00
|
|
|
import { storiesOf } from "@storybook/react";
|
2019-08-09 10:26:22 +00:00
|
|
|
import React from "react";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
import Form from "@saleor/components/Form";
|
2019-10-14 11:57:08 +00:00
|
|
|
import { maybe } from "@saleor/misc";
|
|
|
|
import CardDecorator from "@saleor/storybook/CardDecorator";
|
|
|
|
import Decorator from "@saleor/storybook/Decorator";
|
|
|
|
import { ChoiceProvider } from "@saleor/storybook/mock";
|
|
|
|
import { countries } from "./fixtures";
|
2019-08-09 11:14:35 +00:00
|
|
|
import SingleAutocompleteSelectField, {
|
|
|
|
SingleAutocompleteSelectFieldProps
|
2019-10-14 11:57:08 +00:00
|
|
|
} from "./SingleAutocompleteSelectField";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2019-10-14 11:57:08 +00:00
|
|
|
const suggestions = countries.map(c => ({ label: c.name, value: c.code }));
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2019-08-09 11:14:35 +00:00
|
|
|
const props: SingleAutocompleteSelectFieldProps = {
|
|
|
|
choices: undefined,
|
|
|
|
displayValue: undefined,
|
2019-06-19 14:40:52 +00:00
|
|
|
label: "Country",
|
|
|
|
loading: false,
|
|
|
|
name: "country",
|
2019-08-09 11:14:35 +00:00
|
|
|
onChange: () => undefined,
|
2019-06-19 14:40:52 +00:00
|
|
|
placeholder: "Select country"
|
|
|
|
};
|
|
|
|
|
2019-08-09 11:14:35 +00:00
|
|
|
const Story: React.FC<
|
|
|
|
Partial<SingleAutocompleteSelectFieldProps>
|
|
|
|
> = storyProps => {
|
|
|
|
const [displayValue, setDisplayValue] = React.useState(suggestions[0].label);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form initial={{ country: suggestions[0].value }}>
|
2019-06-19 14:40:52 +00:00
|
|
|
{({ change, data }) => (
|
|
|
|
<ChoiceProvider choices={suggestions}>
|
2019-08-09 11:14:35 +00:00
|
|
|
{({ choices, loading, fetchChoices }) => {
|
|
|
|
const handleSelect = (event: React.ChangeEvent<any>) => {
|
|
|
|
const value: string = event.target.value;
|
|
|
|
const match = choices.find(choice => choice.value === value);
|
|
|
|
const label = maybe(() => match.label, value);
|
|
|
|
setDisplayValue(label);
|
|
|
|
change(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SingleAutocompleteSelectField
|
|
|
|
{...props}
|
|
|
|
displayValue={displayValue}
|
|
|
|
choices={choices}
|
|
|
|
fetchChoices={fetchChoices}
|
|
|
|
helperText={`Value: ${data.country}`}
|
|
|
|
onChange={handleSelect}
|
|
|
|
value={data.country}
|
|
|
|
loading={loading}
|
|
|
|
{...storyProps}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
2019-06-19 14:40:52 +00:00
|
|
|
</ChoiceProvider>
|
|
|
|
)}
|
|
|
|
</Form>
|
2019-08-09 11:14:35 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
storiesOf("Generics / SingleAutocompleteSelectField", module)
|
|
|
|
.addDecorator(CardDecorator)
|
|
|
|
.addDecorator(Decorator)
|
|
|
|
.add("with loaded data", () => <Story />)
|
|
|
|
.add("with loading data", () => <Story loading={true} />)
|
|
|
|
.add("with custom option", () => <Story allowCustomValues={true} />);
|