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-08-09 11:14:35 +00:00
|
|
|
import SingleAutocompleteSelectField, {
|
|
|
|
SingleAutocompleteSelectFieldProps
|
|
|
|
} from "@saleor/components/SingleAutocompleteSelectField";
|
|
|
|
import { maybe } from "@saleor/misc";
|
2019-06-19 14:40:52 +00:00
|
|
|
import CardDecorator from "../../CardDecorator";
|
|
|
|
import Decorator from "../../Decorator";
|
|
|
|
import { ChoiceProvider } from "../../mock";
|
|
|
|
|
|
|
|
const suggestions = [
|
|
|
|
"Afghanistan",
|
|
|
|
"Burundi",
|
|
|
|
"Comoros",
|
|
|
|
"Egypt",
|
|
|
|
"Equatorial Guinea",
|
|
|
|
"Greenland",
|
|
|
|
"Isle of Man",
|
|
|
|
"Israel",
|
|
|
|
"Italy",
|
|
|
|
"United States",
|
|
|
|
"Wallis and Futuna",
|
|
|
|
"Zimbabwe"
|
|
|
|
].map(c => ({ label: c, value: c.toLocaleLowerCase().replace(/\s+/, "_") }));
|
|
|
|
|
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} />);
|