|
|
|
@ -2,6 +2,7 @@ import styles from './styles.module.css' |
|
|
|
|
import Option, {OptionInterface} from '../option' |
|
|
|
|
import {useContext, useEffect, useState} from "react"; |
|
|
|
|
import Context, {ContextInterface} from "../command/context"; |
|
|
|
|
import {Select, MenuItem, Checkbox, ListItemText} from "@mui/material"; |
|
|
|
|
|
|
|
|
|
interface OptionsInterface { |
|
|
|
|
optionList: OptionInterface[] |
|
|
|
@ -9,46 +10,58 @@ interface OptionsInterface { |
|
|
|
|
|
|
|
|
|
export default function Options({optionList}: OptionsInterface) { |
|
|
|
|
let {optionList: ol, setOptionList} = useContext<ContextInterface>(Context) |
|
|
|
|
let [availableOptions, setAvailableOptions] = useState<OptionInterface[]>(optionList); |
|
|
|
|
let [selectedOptions, setSelectedOptions] = useState<OptionInterface[]>([]); |
|
|
|
|
let [values, setValues] = useState<OptionInterface[]>([]); |
|
|
|
|
let [selectedValues, setSelectedValues] = useState<string[]>([]); |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
let result: OptionInterface[] = []; |
|
|
|
|
optionList.forEach((value, index, array) => { |
|
|
|
|
let check = values.find(item => item === value) |
|
|
|
|
!check && result.push(value) |
|
|
|
|
let check = selectedValues.find(item => item === value.name) |
|
|
|
|
check && result.push(value) |
|
|
|
|
}) |
|
|
|
|
setAvailableOptions(result) |
|
|
|
|
}, [values]); |
|
|
|
|
setSelectedOptions(result) |
|
|
|
|
|
|
|
|
|
let temp: any = {} |
|
|
|
|
optionList.forEach((value, index, array) => { |
|
|
|
|
let check = selectedValues.find(item => item === value.name) |
|
|
|
|
check && (temp[value.name] = value.acceptValue ? '' : null) |
|
|
|
|
}) |
|
|
|
|
setOptionList(temp) |
|
|
|
|
}, [selectedValues]); |
|
|
|
|
|
|
|
|
|
if (!optionList.length) { |
|
|
|
|
return <></> |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let onChange = (event: any) => { |
|
|
|
|
let selectedValue = event.target.value |
|
|
|
|
let selectedOption = availableOptions.find(item => item.name === selectedValue) |
|
|
|
|
selectedOption && values.push(selectedOption) |
|
|
|
|
setValues([ ...values ]) |
|
|
|
|
event.target.value = ''; |
|
|
|
|
const handleChange = (event: any) => { |
|
|
|
|
const { target: { value }} = event |
|
|
|
|
setSelectedValues(typeof value === 'string' ? value.split(',') : value) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
let temp = { |
|
|
|
|
...ol |
|
|
|
|
} |
|
|
|
|
if (selectedOption) { |
|
|
|
|
temp[selectedOption.name] = selectedOption.acceptValue ? '' : null; |
|
|
|
|
} |
|
|
|
|
setOptionList(temp) |
|
|
|
|
} |
|
|
|
|
return ( |
|
|
|
|
<> |
|
|
|
|
{availableOptions.length > 0 && <><select onChange={onChange}> |
|
|
|
|
<option value="">-</option> |
|
|
|
|
{availableOptions.map((option: OptionInterface, index: number) => ( |
|
|
|
|
<option key={index} value={option.name}>{option.name}</option> |
|
|
|
|
{optionList.length > 0 && |
|
|
|
|
<> |
|
|
|
|
<Select |
|
|
|
|
value={selectedValues} |
|
|
|
|
label="Option" |
|
|
|
|
multiple |
|
|
|
|
displayEmpty |
|
|
|
|
onChange={handleChange} |
|
|
|
|
renderValue={(selectedValues) => `Options`} |
|
|
|
|
size="small" |
|
|
|
|
> |
|
|
|
|
{optionList.map((option: OptionInterface, index: number) => ( |
|
|
|
|
<MenuItem key={index} value={option.name}> |
|
|
|
|
<Checkbox checked={selectedValues.indexOf(option.name) > -1} /> |
|
|
|
|
<ListItemText primary={option.name} /> |
|
|
|
|
</MenuItem> |
|
|
|
|
))} |
|
|
|
|
</select> </>} |
|
|
|
|
{values.map((option: OptionInterface, index: number) => ( |
|
|
|
|
</Select> |
|
|
|
|
|
|
|
|
|
</>} |
|
|
|
|
{selectedOptions.map((option: OptionInterface, index: number) => ( |
|
|
|
|
<Option key={index} {...option} /> |
|
|
|
|
))} |
|
|
|
|
</> |
|
|
|
|