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-15 15:23:33 +00:00
|
|
|
import { countries } from "@saleor/fixtures";
|
2019-10-14 11:57:08 +00:00
|
|
|
import CardDecorator from "@saleor/storybook/CardDecorator";
|
|
|
|
import Decorator from "@saleor/storybook/Decorator";
|
|
|
|
import { ChoiceProvider } from "@saleor/storybook/mock";
|
2019-10-14 14:17:03 +00:00
|
|
|
import createSingleAutocompleteSelectHandler from "@saleor/utils/handlers/singleAutocompleteSelectChangeHandler";
|
2019-08-09 11:14:35 +00:00
|
|
|
import SingleAutocompleteSelectField, {
|
|
|
|
SingleAutocompleteSelectFieldProps
|
2019-10-14 11:57:08 +00:00
|
|
|
} from "./SingleAutocompleteSelectField";
|
2019-10-14 14:17:03 +00:00
|
|
|
import SingleAutocompleteSelectFieldContent, {
|
|
|
|
SingleAutocompleteSelectFieldContentProps
|
|
|
|
} from "./SingleAutocompleteSelectFieldContent";
|
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-10-14 14:17:03 +00:00
|
|
|
placeholder: "Select country",
|
|
|
|
value: suggestions[0].value
|
2019-06-19 14:40:52 +00:00
|
|
|
};
|
|
|
|
|
2019-12-20 10:44:41 +00:00
|
|
|
const Story: React.FC<Partial<
|
|
|
|
SingleAutocompleteSelectFieldProps & {
|
|
|
|
enableLoadMore: boolean;
|
|
|
|
}
|
|
|
|
>> = ({ allowCustomValues, emptyOption, enableLoadMore }) => {
|
2019-08-09 11:14:35 +00:00
|
|
|
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-12-20 10:44:41 +00:00
|
|
|
{({ choices, fetchChoices, onFetchMore, hasMore, loading }) => {
|
2019-10-14 14:17:03 +00:00
|
|
|
const handleSelect = createSingleAutocompleteSelectHandler(
|
|
|
|
change,
|
|
|
|
setDisplayValue,
|
|
|
|
choices
|
|
|
|
);
|
2019-08-09 11:14:35 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<SingleAutocompleteSelectField
|
|
|
|
{...props}
|
|
|
|
displayValue={displayValue}
|
|
|
|
choices={choices}
|
|
|
|
fetchChoices={fetchChoices}
|
|
|
|
helperText={`Value: ${data.country}`}
|
2019-10-14 14:17:03 +00:00
|
|
|
loading={loading}
|
2019-08-09 11:14:35 +00:00
|
|
|
onChange={handleSelect}
|
|
|
|
value={data.country}
|
2019-10-15 15:46:19 +00:00
|
|
|
hasMore={enableLoadMore ? hasMore : false}
|
2019-12-20 10:44:41 +00:00
|
|
|
onFetchMore={enableLoadMore ? onFetchMore : undefined}
|
2019-10-15 15:46:19 +00:00
|
|
|
allowCustomValues={allowCustomValues}
|
|
|
|
emptyOption={emptyOption}
|
2019-08-09 11:14:35 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
2019-06-19 14:40:52 +00:00
|
|
|
</ChoiceProvider>
|
|
|
|
)}
|
|
|
|
</Form>
|
2019-08-09 11:14:35 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-10-14 14:17:03 +00:00
|
|
|
const contentProps: SingleAutocompleteSelectFieldContentProps = {
|
|
|
|
choices: suggestions.slice(0, 10),
|
|
|
|
displayCustomValue: false,
|
|
|
|
emptyOption: false,
|
|
|
|
getItemProps: () => undefined,
|
|
|
|
hasMore: false,
|
|
|
|
highlightedIndex: 0,
|
|
|
|
inputValue: suggestions[0].label,
|
|
|
|
isCustomValueSelected: false,
|
|
|
|
loading: false,
|
|
|
|
onFetchMore: () => undefined,
|
|
|
|
selectedItem: suggestions[0].value
|
|
|
|
};
|
|
|
|
|
|
|
|
storiesOf("Generics / Select with autocomplete", module)
|
2019-08-09 11:14:35 +00:00
|
|
|
.addDecorator(CardDecorator)
|
|
|
|
.addDecorator(Decorator)
|
2019-10-14 14:17:03 +00:00
|
|
|
.add("default", () => (
|
|
|
|
<SingleAutocompleteSelectFieldContent {...contentProps} />
|
|
|
|
))
|
|
|
|
.add("can load more", () => (
|
|
|
|
<SingleAutocompleteSelectFieldContent {...contentProps} hasMore={true} />
|
|
|
|
))
|
|
|
|
.add("no data", () => (
|
|
|
|
<SingleAutocompleteSelectFieldContent {...contentProps} choices={[]} />
|
|
|
|
))
|
|
|
|
.add("interactive", () => <Story />)
|
|
|
|
.add("interactive with custom option", () => (
|
|
|
|
<Story allowCustomValues={true} />
|
|
|
|
))
|
|
|
|
.add("interactive with empty option", () => <Story emptyOption={true} />)
|
|
|
|
.add("interactive with load more", () => <Story enableLoadMore={true} />);
|