2022-02-02 13:18:15 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
|
|
|
|
import Chip from "./sub-components/chip";
|
|
|
|
import TextInput from "../text-input";
|
|
|
|
import Scrollbar from "../scrollbar";
|
|
|
|
import { EmailSettings, parseAddress } from "../utils/email/";
|
|
|
|
|
|
|
|
import {
|
|
|
|
StyledContent,
|
|
|
|
StyledChipGroup,
|
|
|
|
StyledChipWithInput,
|
|
|
|
} from "./styled-inputwithchips";
|
|
|
|
|
|
|
|
const InputWithChips = (props) => {
|
2022-02-03 11:57:50 +00:00
|
|
|
const [options, setOptions] = useState(props.options || []);
|
2022-02-02 13:18:15 +00:00
|
|
|
|
|
|
|
const [value, setValue] = useState("");
|
|
|
|
|
|
|
|
const [currentChip, setCurrentChip] = useState(null);
|
|
|
|
const [selectedChips, setSelectedChips] = useState([]);
|
|
|
|
|
|
|
|
const [isShiftDown, setIsShiftDown] = useState(false);
|
|
|
|
const [isCtrlDown, setIsCtrlDown] = useState(false);
|
|
|
|
|
|
|
|
const emailSettings = new EmailSettings();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
document.addEventListener("keydown", onKeyDown);
|
|
|
|
document.addEventListener("keyup", onKeyUp);
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener("keydown", onKeyDown);
|
|
|
|
document.removeEventListener("keyup", onKeyUp);
|
|
|
|
};
|
|
|
|
}, [selectedChips, isShiftDown, isCtrlDown]);
|
|
|
|
|
|
|
|
const onChange = (e) => {
|
|
|
|
setValue(e.target.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onClick = (value) => {
|
2022-02-03 11:57:50 +00:00
|
|
|
if (isShiftDown) {
|
|
|
|
const isExisted = !!selectedChips.find((it) => it.value === value.value);
|
|
|
|
return isExisted
|
|
|
|
? setSelectedChips(
|
|
|
|
selectedChips.filter((it) => it.value != value.value)
|
|
|
|
)
|
|
|
|
: setSelectedChips([value, ...selectedChips]);
|
|
|
|
} else {
|
|
|
|
setSelectedChips([value]);
|
|
|
|
}
|
2022-02-02 13:18:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const onDoubleClick = (value) => {
|
|
|
|
setCurrentChip(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onDelete = (value) => {
|
2022-02-03 11:57:50 +00:00
|
|
|
setOptions(options.filter((it) => it.value !== value.value));
|
2022-02-02 13:18:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const onEnterPress = () => {
|
2022-02-03 11:57:50 +00:00
|
|
|
if (
|
|
|
|
!options.find((it) => it.value === value) &&
|
|
|
|
value &&
|
|
|
|
value.trim().length > 0
|
|
|
|
) {
|
|
|
|
setOptions([...options, { label: value, value }]);
|
2022-02-02 13:18:15 +00:00
|
|
|
setValue("");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const checkEmail = (email) => {
|
|
|
|
const emailObj = parseAddress(email, emailSettings);
|
|
|
|
return emailObj.isValid();
|
|
|
|
};
|
|
|
|
|
|
|
|
const onSaveNewChip = (value, newValue) => {
|
2022-02-03 11:57:50 +00:00
|
|
|
setOptions(
|
2022-02-02 14:10:27 +00:00
|
|
|
options.map((it) =>
|
2022-02-02 13:18:15 +00:00
|
|
|
it.value === value.value ? { label: newValue, value: newValue } : it
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onInputKeyDown = (e) => {
|
|
|
|
const code = e.code;
|
|
|
|
if (code === "Enter" || code === "NumpadEnter") onEnterPress();
|
|
|
|
};
|
|
|
|
|
|
|
|
const copyToClipbord = () => {
|
2022-02-03 11:57:50 +00:00
|
|
|
navigator.clipboard.writeText(
|
|
|
|
selectedChips.map((it) => it.value).join(", ")
|
|
|
|
);
|
2022-02-02 13:18:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const onKeyUp = (e) => {
|
|
|
|
const code = e.code;
|
|
|
|
switch (code) {
|
|
|
|
case "ShiftLeft": {
|
|
|
|
setIsShiftDown(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "ControlLeft": {
|
|
|
|
setIsCtrlDown(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onKeyDown = (e) => {
|
|
|
|
const code = e.code;
|
|
|
|
|
|
|
|
if (code === "ShiftLeft") {
|
|
|
|
setIsShiftDown(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (code === "ControlLeft") {
|
|
|
|
setIsCtrlDown(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (code === "Escape") {
|
|
|
|
setSelectedChips([]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selectedChips.length > 1 && code === "Backspace") {
|
2022-02-02 14:10:27 +00:00
|
|
|
const Chips = options.filter((e) => !~selectedChips.indexOf(e));
|
2022-02-03 11:57:50 +00:00
|
|
|
setOptions(Chips);
|
2022-02-02 13:18:15 +00:00
|
|
|
setSelectedChips([]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selectedChips.length > 0) {
|
|
|
|
let chip = "";
|
|
|
|
|
|
|
|
if (isShiftDown && code === "ArrowRigth") {
|
|
|
|
chip = selectedChips[selectedChips.length - 1];
|
|
|
|
} else {
|
|
|
|
chip = selectedChips[0];
|
|
|
|
}
|
|
|
|
|
2022-02-02 14:10:27 +00:00
|
|
|
const index = options.findIndex((it) => it === chip);
|
2022-02-02 13:18:15 +00:00
|
|
|
switch (code) {
|
|
|
|
case "ArrowLeft": {
|
|
|
|
if (isShiftDown) {
|
2022-02-02 14:10:27 +00:00
|
|
|
selectedChips.includes(options[index - 1])
|
2022-02-02 13:18:15 +00:00
|
|
|
? setSelectedChips(
|
2022-02-02 14:10:27 +00:00
|
|
|
selectedChips.filter((it) => it !== options[index])
|
2022-02-02 13:18:15 +00:00
|
|
|
)
|
2022-02-02 14:10:27 +00:00
|
|
|
: options[index - 1] &&
|
|
|
|
setSelectedChips([options[index - 1], ...selectedChips]);
|
2022-02-02 13:18:15 +00:00
|
|
|
} else {
|
2022-02-02 14:10:27 +00:00
|
|
|
setSelectedChips([options[index - 1]]);
|
2022-02-02 13:18:15 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "ArrowRight": {
|
|
|
|
if (isShiftDown) {
|
2022-02-02 14:10:27 +00:00
|
|
|
selectedChips.includes(options[index + 1])
|
2022-02-02 13:18:15 +00:00
|
|
|
? setSelectedChips(
|
2022-02-02 14:10:27 +00:00
|
|
|
selectedChips.filter((it) => it !== options[index])
|
2022-02-02 13:18:15 +00:00
|
|
|
)
|
2022-02-02 14:10:27 +00:00
|
|
|
: options[index + 1] &&
|
|
|
|
setSelectedChips([options[index + 1], ...selectedChips]);
|
2022-02-02 13:18:15 +00:00
|
|
|
} else {
|
2022-02-02 14:10:27 +00:00
|
|
|
setSelectedChips([options[index + 1]]);
|
2022-02-02 13:18:15 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "KeyC": {
|
|
|
|
if (isCtrlDown) {
|
|
|
|
copyToClipbord();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<StyledContent>
|
|
|
|
<StyledChipGroup>
|
|
|
|
<Scrollbar
|
|
|
|
scrollclass={"scroll"}
|
|
|
|
stype="thumbV"
|
|
|
|
style={{ height: "fit-content" }}
|
|
|
|
>
|
|
|
|
<StyledChipWithInput>
|
2022-02-02 14:10:27 +00:00
|
|
|
{options.map((it) => {
|
2022-02-02 13:18:15 +00:00
|
|
|
return (
|
|
|
|
<Chip
|
|
|
|
key={it?.value}
|
|
|
|
value={it}
|
|
|
|
currentChip={currentChip}
|
|
|
|
isSelected={
|
|
|
|
!!selectedChips.find((item) => item?.value === it?.value)
|
|
|
|
}
|
|
|
|
isValid={checkEmail(it?.value)}
|
|
|
|
onDelete={onDelete}
|
|
|
|
onDoubleClick={onDoubleClick}
|
|
|
|
onSaveNewChip={onSaveNewChip}
|
|
|
|
onClick={onClick}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
<TextInput
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
2022-02-02 14:10:27 +00:00
|
|
|
placeholder={options.length > 0 ? "" : props.placeholder}
|
2022-02-02 13:18:15 +00:00
|
|
|
onKeyDown={onInputKeyDown}
|
|
|
|
withBorder={false}
|
|
|
|
/>
|
|
|
|
</StyledChipWithInput>
|
|
|
|
</Scrollbar>
|
|
|
|
</StyledChipGroup>
|
|
|
|
</StyledContent>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
InputWithChips.propTypes = {
|
2022-02-03 11:57:50 +00:00
|
|
|
/** Array of objects with chips */
|
|
|
|
options: PropTypes.arrayOf(PropTypes.object).isRequired,
|
|
|
|
/** The placeholder is displayed only when the input is empty */
|
2022-02-02 13:18:15 +00:00
|
|
|
placeholder: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
2022-02-03 11:57:50 +00:00
|
|
|
InputWithChips.defaultProps = {
|
|
|
|
placeholder: "Add placeholder to props",
|
|
|
|
};
|
|
|
|
|
2022-02-02 13:18:15 +00:00
|
|
|
export default InputWithChips;
|