Autocomplete in channel selection (#2020)

* Add autocomplete to channel picker

* Update macaw

* Update macaw

* Use filter from fuzzaldrin
This commit is contained in:
Dominik Żegleń 2022-05-05 10:36:32 +02:00 committed by GitHub
parent 7d9441a7ec
commit a95a3021e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 29 deletions

6
package-lock.json generated
View file

@ -5224,9 +5224,9 @@
} }
}, },
"@saleor/macaw-ui": { "@saleor/macaw-ui": {
"version": "0.5.0", "version": "0.5.2",
"resolved": "https://registry.npmjs.org/@saleor/macaw-ui/-/macaw-ui-0.5.0.tgz", "resolved": "https://registry.npmjs.org/@saleor/macaw-ui/-/macaw-ui-0.5.2.tgz",
"integrity": "sha512-1dTgbmBHplWpqqyX7kZZSVMz2FESNeUerD5AXPfRYpE6Cr0L+oEfY4NFZlhe2dTY8hPeLz+hPv36JLlD/sGWyA==", "integrity": "sha512-fAbIGLnMo9BiVuR6W6MMZUprIutS+f4i1oEG8PX7+WM4rMZbvD204wxvHyx5dXSrU1DhLzDR4MK7dEGK/jykHA==",
"requires": { "requires": {
"clsx": "^1.1.1", "clsx": "^1.1.1",
"downshift": "^6.1.7", "downshift": "^6.1.7",

View file

@ -28,7 +28,7 @@
"@material-ui/icons": "^4.11.2", "@material-ui/icons": "^4.11.2",
"@material-ui/lab": "^4.0.0-alpha.58", "@material-ui/lab": "^4.0.0-alpha.58",
"@material-ui/styles": "^4.11.4", "@material-ui/styles": "^4.11.4",
"@saleor/macaw-ui": "^0.5.0", "@saleor/macaw-ui": "^0.5.2",
"@saleor/sdk": "^0.4.4", "@saleor/sdk": "^0.4.4",
"@sentry/react": "^6.0.0", "@sentry/react": "^6.0.0",
"@types/faker": "^5.1.6", "@types/faker": "^5.1.6",

View file

@ -1,17 +1,14 @@
import { MenuItem } from "@material-ui/core";
import ActionDialog from "@saleor/components/ActionDialog"; import ActionDialog from "@saleor/components/ActionDialog";
import { import { Choice } from "@saleor/components/SingleSelectField";
Choices, import useChoiceSearch from "@saleor/hooks/useChoiceSearch";
SingleSelectField
} from "@saleor/components/SingleSelectField";
import useStateFromProps from "@saleor/hooks/useStateFromProps"; import useStateFromProps from "@saleor/hooks/useStateFromProps";
import { ConfirmButtonTransitionState } from "@saleor/macaw-ui"; import { Autocomplete, ConfirmButtonTransitionState } from "@saleor/macaw-ui";
import React from "react"; import React from "react";
import { useIntl } from "react-intl"; import { useIntl } from "react-intl";
import { useStyles } from "../styles";
export interface ChannelPickerDialogProps { export interface ChannelPickerDialogProps {
channelsChoices: Choices; channelsChoices: Array<Choice<string, string>>;
confirmButtonState: ConfirmButtonTransitionState; confirmButtonState: ConfirmButtonTransitionState;
defaultChoice: string; defaultChoice: string;
open: boolean; open: boolean;
@ -27,11 +24,11 @@ const ChannelPickerDialog: React.FC<ChannelPickerDialogProps> = ({
onClose, onClose,
onConfirm onConfirm
}) => { }) => {
const classes = useStyles({});
const intl = useIntl(); const intl = useIntl();
const [choice, setChoice] = useStateFromProps( const [choice, setChoice] = useStateFromProps(
defaultChoice || (!!channelsChoices.length ? channelsChoices[0].value : "") defaultChoice || (!!channelsChoices.length ? channelsChoices[0].value : "")
); );
const { result, search } = useChoiceSearch(channelsChoices);
return ( return (
<ActionDialog <ActionDialog
@ -45,21 +42,30 @@ const ChannelPickerDialog: React.FC<ChannelPickerDialogProps> = ({
description: "dialog header" description: "dialog header"
})} })}
> >
<div> <Autocomplete
<div className={classes.select}> choices={result}
<SingleSelectField fullWidth
choices={channelsChoices} label={intl.formatMessage({
name="channels" defaultMessage: "Channel name",
label={intl.formatMessage({ id: "nKwgxY",
id: "nKwgxY", description: "select label"
defaultMessage: "Channel name", })}
description: "select label" value={choice}
})} onChange={e => setChoice(e.target.value)}
value={choice} onInputChange={search}
onChange={e => setChoice(e.target.value)} >
/> {({ getItemProps, highlightedIndex }) =>
</div> result.map((choice, choiceIndex) => (
</div> <MenuItem
selected={highlightedIndex === choiceIndex}
key={choice.value}
{...getItemProps({ item: choice, index: choiceIndex })}
>
{choice.label}
</MenuItem>
))
}
</Autocomplete>
</ActionDialog> </ActionDialog>
); );
}; };

View file

@ -0,0 +1,16 @@
import { Choice } from "@saleor/components/SingleSelectField";
import { filter } from "fuzzaldrin";
import { useMemo, useState } from "react";
function useChoiceSearch(choices: Array<Choice<string, string>>) {
const [query, setQuery] = useState("");
const sortedChoices = useMemo(
() => filter(choices, query, { key: "label" }) || [],
[choices, query]
);
return { search: setQuery, result: sortedChoices };
}
export default useChoiceSearch;

View file

@ -190,7 +190,7 @@ export const OrderList: React.FC<OrderListProps> = ({ params }) => {
/> />
{!noChannel && ( {!noChannel && (
<ChannelPickerDialog <ChannelPickerDialog
channelsChoices={mapNodeToChoice(availableChannels)} channelsChoices={channelOpts}
confirmButtonState="success" confirmButtonState="success"
defaultChoice={channel.id} defaultChoice={channel.id}
open={params.action === "create-order"} open={params.action === "create-order"}