Files
rowy/www/src/components/Table/ColumnEditor/SelectOptionsInput.tsx

101 lines
2.7 KiB
TypeScript
Raw Normal View History

2019-10-02 14:38:07 +10:00
import React, { useEffect, useState } from "react";
2019-09-24 13:53:28 +10:00
import { createStyles, makeStyles, Theme } from "@material-ui/core/styles";
import Chip from "@material-ui/core/Chip";
2019-10-03 18:04:58 +10:00
import TextField from "@material-ui/core/TextField";
import Grid from "@material-ui/core/Grid";
import Divider from "@material-ui/core/Divider";
2019-09-24 13:53:28 +10:00
import _includes from "lodash/includes";
import _camelCase from "lodash/camelCase";
2019-10-02 14:38:07 +10:00
import AddIcon from "@material-ui/icons/AddCircle";
import IconButton from "@material-ui/core/IconButton";
import InputAdornment from "@material-ui/core/InputAdornment";
2019-09-24 13:53:28 +10:00
const useStyles = makeStyles(Theme =>
createStyles({
2019-10-02 14:38:07 +10:00
root: {},
field: {
width: "100%",
},
chipsContainer: {
2019-09-24 13:53:28 +10:00
display: "flex",
justifyContent: "center",
flexWrap: "wrap",
maxWidth: 300,
padding: Theme.spacing(1),
},
chip: {
margin: Theme.spacing(0.5),
},
})
);
export default function SelectOptionsInput(props: any) {
const { options, setValue } = props;
const classes = useStyles();
2019-10-02 14:38:07 +10:00
const [newOption, setNewOption] = useState("");
const handleAdd = () => {
2019-09-24 13:53:28 +10:00
// setOptions([...options, newOption]);
2019-10-02 14:38:07 +10:00
if (newOption !== "") {
setValue("options", [...options, newOption]);
setNewOption("");
}
2019-09-24 13:53:28 +10:00
};
const handleDelete = (optionToDelete: string) => () => {
const newOptions = options.filter(
(option: string) => option !== optionToDelete
);
setValue("options", newOptions);
};
useEffect(() => {
setValue({ data: { options } });
}, [options]);
return (
<Grid container direction="column" className={classes.root}>
<Grid item>
<TextField
2019-10-02 14:38:07 +10:00
value={newOption}
className={classes.field}
2019-09-24 13:53:28 +10:00
label="New Option"
2019-10-02 14:38:07 +10:00
onChange={e => {
setNewOption(e.target.value);
}}
2019-09-24 13:53:28 +10:00
onKeyPress={(e: any) => {
if (e.key === "Enter") {
2019-10-02 14:38:07 +10:00
handleAdd();
2019-09-24 13:53:28 +10:00
}
}}
2019-10-02 14:38:07 +10:00
InputProps={{
endAdornment: (
<InputAdornment position="end">
<IconButton
edge="end"
aria-label="toggle password visibility"
onClick={(e: any) => {
handleAdd();
}}
>
{<AddIcon />}
</IconButton>
</InputAdornment>
),
}}
2019-09-24 13:53:28 +10:00
/>
</Grid>
2019-10-02 14:38:07 +10:00
<Grid item className={classes.chipsContainer}>
2019-09-24 13:53:28 +10:00
{options.map((option: string) => {
return (
<Chip
key={option}
label={option}
onDelete={handleDelete(option)}
className={classes.chip}
/>
);
})}
</Grid>
</Grid>
);
}