prevent multi select crashing

This commit is contained in:
Shams mosowi
2020-04-28 12:02:51 +08:00
parent 32aafb19b6
commit 4afca01eed
2 changed files with 29 additions and 20 deletions

View File

@@ -58,19 +58,24 @@ export default function MultiSelect({
width: dropdownWidth,
});
const sanitisedValue = value.filter(v => v?.length > 0);
const sanitisedValue = Array.isArray(value)
? value.filter((v) => v?.length > 0)
: [value];
// Transform `option` prop if its just strings
let options =
typeof optionsProp[0] === "string"
? (optionsProp as string[]).map(
item => ({ label: item, value: item } as OptionType)
(item) => ({ label: item, value: item } as OptionType)
)
: (optionsProp as OptionType[]);
// If `freeText` enabled, show the users custom fields
if (freeText) {
// `value` prop is an array of all values. It removes labels
const formattedValues = sanitisedValue?.map(x => ({ label: x, value: x }));
const formattedValues = sanitisedValue?.map((x) => ({
label: x,
value: x,
}));
options = _unionWith(
options,
formattedValues,
@@ -86,7 +91,7 @@ export default function MultiSelect({
className={clsx(classes.root, className)}
{...TextFieldProps}
SelectProps={{
renderValue: value => {
renderValue: (value) => {
const selected = value as string[];
if (selected.length === 1 && typeof selected[0] === "string") {
const selectedOption = _find(options, { value: selected[0] });
@@ -109,7 +114,7 @@ export default function MultiSelect({
...TextFieldProps.SelectProps?.MenuProps,
},
}}
ref={el => {
ref={(el) => {
if (!el) return;
const width = el.getBoundingClientRect().width;
if (dropdownWidth < width) setDropdownWidth(width);

View File

@@ -9,7 +9,7 @@ import FormattedChip from "components/FormattedChip";
import { FieldType } from "constants/fields";
import { useFiretableContext } from "contexts/firetableContext";
const useStyles = makeStyles(theme =>
const useStyles = makeStyles((theme) =>
createStyles({
root: {
minWidth: 0,
@@ -63,23 +63,27 @@ export default function MultiSelect({
? (([value] as unknown) as string[])
: value;
// And support transforming array of strings back to string
const handleChange = value => onSubmit(isSingle ? value.join(", ") : value);
const handleChange = (value) => onSubmit(isSingle ? value.join(", ") : value);
// Render chips
const renderValue = value => (
<Grid container spacing={1} wrap="nowrap" className={classes.chipList}>
{value?.map(
item =>
typeof item === "string" && (
<Grid item key={item}>
<FormattedChip label={item} className={classes.chip} />
</Grid>
)
)}
</Grid>
);
const renderValue = (value) => {
//if (Array.isArray(value))
return (
<Grid container spacing={1} wrap="nowrap" className={classes.chipList}>
{value?.map(
(item) =>
typeof item === "string" && (
<Grid item key={item}>
<FormattedChip label={item} className={classes.chip} />
</Grid>
)
)}
</Grid>
);
// else
};
const onClick = e => e.stopPropagation();
const onClick = (e) => e.stopPropagation();
const onClose = () => {
if (dataGridRef?.current?.selectCell)
dataGridRef.current.selectCell({ rowIdx, idx: column.idx });