saleor-dashboard/src/components/MultiAutocompleteSelectField/MultiAutocompleteSelectField.tsx

273 lines
7.2 KiB
TypeScript
Raw Normal View History

import { IconButton, TextField, Typography } from "@material-ui/core";
import { fade } from "@material-ui/core/styles/colorManipulator";
2019-08-09 11:14:35 +00:00
import CloseIcon from "@material-ui/icons/Close";
import Debounce, { DebounceProps } from "@saleor/components/Debounce";
import ArrowDropdownIcon from "@saleor/icons/ArrowDropdown";
import { makeStyles } from "@saleor/theme";
import { FetchMoreProps } from "@saleor/types";
2019-06-19 14:40:52 +00:00
import Downshift, { ControllerStateAndHelpers } from "downshift";
2019-10-16 15:51:53 +00:00
import { filter } from "fuzzaldrin";
2019-08-09 10:26:22 +00:00
import React from "react";
2019-06-19 14:40:52 +00:00
2019-10-15 15:23:33 +00:00
import MultiAutocompleteSelectFieldContent, {
MultiAutocompleteActionType,
MultiAutocompleteChoiceType
2019-10-15 15:23:33 +00:00
} from "./MultiAutocompleteSelectFieldContent";
2019-06-19 14:40:52 +00:00
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
theme => ({
chip: {
width: "100%"
},
chipClose: {
height: 32,
padding: 0,
width: 32
},
chipContainer: {
display: "flex",
flexDirection: "column",
marginTop: theme.spacing(1)
},
chipInner: {
"& svg": {
color: theme.palette.primary.contrastText
},
alignItems: "center",
background: fade(theme.palette.primary.main, 0.8),
borderRadius: 18,
color: theme.palette.primary.contrastText,
display: "flex",
justifyContent: "space-between",
margin: theme.spacing(1, 0),
paddingLeft: theme.spacing(2),
paddingRight: theme.spacing(1)
},
chipLabel: {
2019-09-12 14:38:40 +00:00
color: theme.palette.primary.contrastText
2019-06-19 14:40:52 +00:00
},
2019-12-03 15:28:40 +00:00
container: {
flexGrow: 1,
position: "relative"
},
disabledChipInner: {
"& svg": {
2020-04-27 07:53:34 +00:00
color: theme.palette.grey[200]
},
alignItems: "center",
2020-04-27 07:53:34 +00:00
background: fade(theme.palette.grey[400], 0.8),
borderRadius: 18,
color: theme.palette.primary.contrastText,
display: "flex",
justifyContent: "space-between",
margin: theme.spacing(1, 0),
paddingLeft: theme.spacing(2),
paddingRight: theme.spacing(1)
2019-12-03 15:28:40 +00:00
}
}),
{ name: "MultiAutocompleteSelectField" }
);
2019-06-19 14:40:52 +00:00
2019-10-15 15:23:33 +00:00
export interface MultiAutocompleteSelectFieldProps
extends Partial<FetchMoreProps> {
2020-03-18 17:24:55 +00:00
add?: MultiAutocompleteActionType;
2019-08-09 11:14:35 +00:00
allowCustomValues?: boolean;
displayValues: MultiAutocompleteChoiceType[];
2020-03-06 11:09:55 +00:00
error?: boolean;
2019-06-19 14:40:52 +00:00
name: string;
2019-08-09 11:14:35 +00:00
choices: MultiAutocompleteChoiceType[];
value: string[];
2019-06-19 14:40:52 +00:00
loading?: boolean;
placeholder?: string;
helperText?: string;
label?: string;
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
disabled?: boolean;
testId?: string;
2019-08-09 11:14:35 +00:00
fetchChoices?: (value: string) => void;
onChange: (event: React.ChangeEvent<any>) => void;
onFocus?: () => void;
2019-06-19 14:40:52 +00:00
}
2020-03-06 11:09:55 +00:00
const DebounceAutocomplete: React.ComponentType<DebounceProps<
string
>> = Debounce;
2019-06-19 14:40:52 +00:00
2020-03-06 11:09:55 +00:00
const MultiAutocompleteSelectFieldComponent: React.FC<MultiAutocompleteSelectFieldProps> = props => {
2019-10-30 14:34:24 +00:00
const {
2020-03-18 17:24:55 +00:00
add,
2019-08-09 11:14:35 +00:00
allowCustomValues,
2019-06-19 14:40:52 +00:00
choices,
2019-08-09 11:14:35 +00:00
displayValues,
2020-03-06 11:09:55 +00:00
error,
2019-10-15 15:23:33 +00:00
hasMore,
2019-06-19 14:40:52 +00:00
helperText,
label,
loading,
name,
placeholder,
value,
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
disabled,
testId,
2019-06-19 14:40:52 +00:00
fetchChoices,
2019-08-27 13:29:00 +00:00
onChange,
onFocus,
2019-10-15 15:23:33 +00:00
onFetchMore,
2019-10-30 14:34:24 +00:00
...rest
} = props;
const classes = useStyles(props);
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
const handleSelect = (
item: string,
downshiftOpts?: ControllerStateAndHelpers
) => {
if (downshiftOpts) {
downshiftOpts.reset({ inputValue: "" });
}
onChange({
target: { name, value: item }
} as any);
};
2019-10-15 15:23:33 +00:00
2019-10-30 14:34:24 +00:00
return (
<>
<Downshift
onInputValueChange={fetchChoices}
onSelect={handleSelect}
itemToString={() => ""}
>
{({
2020-03-18 17:24:55 +00:00
closeMenu,
2019-10-30 14:34:24 +00:00
getInputProps,
getItemProps,
isOpen,
toggleMenu,
highlightedIndex,
inputValue
}) => {
const displayCustomValue =
inputValue &&
inputValue.length > 0 &&
allowCustomValues &&
!choices.find(
choice => choice.label.toLowerCase() === inputValue.toLowerCase()
2019-10-15 15:23:33 +00:00
);
2019-10-30 14:34:24 +00:00
return (
<div className={classes.container} {...rest}>
<TextField
InputProps={{
...getInputProps({
placeholder
}),
endAdornment: (
<div>
<ArrowDropdownIcon onClick={() => toggleMenu()} />
</div>
),
id: undefined,
onClick: toggleMenu,
onFocus: () => {
if (onFocus) {
onFocus();
}
}
2019-10-30 14:34:24 +00:00
}}
2020-03-06 11:09:55 +00:00
error={error}
2019-10-30 14:34:24 +00:00
helperText={helperText}
label={label}
fullWidth={true}
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
disabled={disabled}
2019-10-30 14:34:24 +00:00
/>
{isOpen && (!!inputValue || !!choices.length) && (
<MultiAutocompleteSelectFieldContent
2020-10-19 09:33:51 +00:00
add={
add && {
...add,
onClick: () => {
add.onClick();
closeMenu();
}
2020-02-10 16:07:17 +00:00
}
2020-10-19 09:33:51 +00:00
}
2019-10-30 14:34:24 +00:00
choices={choices.filter(
choice => !value.includes(choice.value)
)}
displayCustomValue={displayCustomValue}
displayValues={displayValues}
getItemProps={getItemProps}
hasMore={hasMore}
highlightedIndex={highlightedIndex}
loading={loading}
inputValue={inputValue}
onFetchMore={onFetchMore}
/>
)}
2019-08-09 11:14:35 +00:00
</div>
2019-10-30 14:34:24 +00:00
);
}}
</Downshift>
<div className={classes.chipContainer}>
{displayValues.map(value => (
<div className={classes.chip} key={value.value}>
<div
className={
!value.disabled ? classes.chipInner : classes.disabledChipInner
}
>
2019-10-30 14:34:24 +00:00
<Typography className={classes.chipLabel}>
{value.label}
</Typography>
2020-04-27 07:53:34 +00:00
<IconButton
data-test-id={testId ? `${testId}Remove` : "remove"}
2020-04-27 07:53:34 +00:00
className={classes.chipClose}
disabled={value.disabled}
onClick={() => handleSelect(value.value)}
>
<CloseIcon fontSize="small" />
</IconButton>
2019-10-30 14:34:24 +00:00
</div>
</div>
))}
</div>
</>
);
};
2020-03-06 11:09:55 +00:00
const MultiAutocompleteSelectField: React.FC<MultiAutocompleteSelectFieldProps> = ({
choices,
fetchChoices,
testId,
2020-03-06 11:09:55 +00:00
...props
}) => {
2019-08-09 11:14:35 +00:00
const [query, setQuery] = React.useState("");
2019-10-30 14:34:24 +00:00
2019-08-09 11:14:35 +00:00
if (fetchChoices) {
2019-06-19 14:40:52 +00:00
return (
<DebounceAutocomplete debounceFn={fetchChoices}>
2019-08-09 11:14:35 +00:00
{debounceFn => (
<MultiAutocompleteSelectFieldComponent
testId={testId}
2019-08-09 11:14:35 +00:00
choices={choices}
{...props}
fetchChoices={debounceFn}
/>
2019-06-19 14:40:52 +00:00
)}
</DebounceAutocomplete>
);
}
2019-08-09 11:14:35 +00:00
return (
<MultiAutocompleteSelectFieldComponent
fetchChoices={q => setQuery(q || "")}
2019-10-16 15:51:53 +00:00
choices={filter(choices, query, {
key: "label"
})}
2019-08-09 11:14:35 +00:00
{...props}
/>
);
};
2019-10-30 14:34:24 +00:00
2019-06-19 14:40:52 +00:00
MultiAutocompleteSelectField.displayName = "MultiAutocompleteSelectField";
export default MultiAutocompleteSelectField;