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

274 lines
7.5 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";
Use MacawUI (#1229) * Replace withStyleswith useStyles (#1100) * Replace withStyleswith useStyles * Update messages * Use rem as a spacing unit (#1101) * Use rems as spacing units * Fix visual bugs * Update stories * Use macaw-ui as theme provider (#1108) * Use macaw ui as a theme provider * Add react-dom to aliases * Fix jest module resolution * Update useTheme hook usage * Fix test wrapper * Use macaw from git repo * Fix CI * Update stories * Fix aliasing * Extract savebar to macaw ui (#1146) * wip * Use savebar from macaw * Use confirm button from macaw * Improve file structure * Use sidebar context from macaw * Update macaw * Update macaw version * Remove savebar from storybook * Update stories * Use alerts and notifications from macaw (#1166) * Use alerts from macaw * Add notifications from macaw * Update stories * Pin macaw version * Encapsulate limit reached in one component * Remove unused imports * Use backlinks from macaw (#1183) * Use backlink from macaw * Update macaw version * Use macaw sidebar (#1148) * Use sidebar from macaw * Use shipped logo * Use lowercase * Update stories * Use user chip from macaw (#1191) * Use user chip from macaw * Use dedicated components for menu items * Simplify code * Bump version and fix types (#1210) * Rename onBack to onClick * Rename UserChip to UserChipMenu * Rename IMenuItem to SidebarMenuItem * Update macaw version * Fix tables after changes in macaw (#1220) * Update macaw version * Update changelog * Update stories * Fix after rebase * Update to macaw 0.2.0 * Lint files * Update macaw to 0.2.2
2021-07-21 08:59:52 +00:00
import { makeStyles } from "@saleor/macaw-ui";
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;
fetchOnFocus?: boolean;
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,
2019-10-15 15:23:33 +00:00
onFetchMore,
fetchOnFocus,
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 (
<>
<DebounceAutocomplete debounceFn={fetchChoices}>
{debounceFn => (
<Downshift
onInputValueChange={value => debounceFn(value)}
onSelect={handleSelect}
itemToString={() => ""}
>
{({
closeMenu,
getInputProps,
getItemProps,
isOpen,
toggleMenu,
highlightedIndex,
inputValue
}) => {
const displayCustomValue =
inputValue &&
inputValue.length > 0 &&
allowCustomValues &&
!choices.find(
choice =>
choice.label.toLowerCase() === inputValue.toLowerCase()
);
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 (fetchOnFocus) {
fetchChoices(inputValue);
}
2020-10-19 09:33:51 +00:00
}
}}
error={error}
helperText={helperText}
label={label}
fullWidth={true}
disabled={disabled}
/>
{isOpen && (
<MultiAutocompleteSelectFieldContent
add={
add && {
...add,
onClick: () => {
add.onClick();
closeMenu();
}
}
}
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-10-30 14:34:24 +00:00
)}
</div>
);
}}
</Downshift>
)}
</DebounceAutocomplete>
2019-10-30 14:34:24 +00:00
<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 (
<MultiAutocompleteSelectFieldComponent
testId={testId}
choices={choices}
{...props}
fetchChoices={fetchChoices}
/>
2019-06-19 14:40:52 +00:00
);
}
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;