1 | initial version |
Here is an example of what the code might look like:
import React, { useState } from 'react';
import { Table, TableBody, TableCell, TableHead, TableRow, Typography, Select, MenuItem } from '@material-ui/core';
const options = ['Option 1', 'Option 2', 'Option 3'];
function App() {
const [selectedOptions, setSelectedOptions] = useState([]);
const handleChange = (event) => {
const selected = event.target.value;
setSelectedOptions((prevSelected) => {
if (prevSelected.includes(selected)) {
return prevSelected.filter((option) => option !== selected);
} else {
return [...prevSelected, selected];
}
});
};
const formatOptions = (optionsArray) => {
return optionsArray.join(', ');
};
return (
<Table>
<TableHead>
<TableRow>
<TableCell>Selected Options</TableCell>
</TableRow>
</TableHead>
<TableBody>
<TableRow>
<TableCell>
<Select multiple value={selectedOptions} onChange={handleChange}>
{options.map((option) => (
<MenuItem key={option} value={option}>
{option}
</MenuItem>
))}
</Select>
</TableCell>
</TableRow>
<TableRow>
<TableCell>
<Typography>{formatOptions(selectedOptions)}</Typography>
</TableCell>
</TableRow>
</TableBody>
</Table>
);
}
export default App;