diff --git a/client/src/components/pages/AddRecipe.tsx b/client/src/components/pages/AddRecipe.tsx index ebd5a40..057276c 100644 --- a/client/src/components/pages/AddRecipe.tsx +++ b/client/src/components/pages/AddRecipe.tsx @@ -26,7 +26,7 @@ const AddRecipe = () => { // clear out selector state on page load useEffect(() => { setData(new Array()); - setSelected(new Array()); + setSelected(new Array()); setOptions(new Array()); }, []) @@ -60,20 +60,25 @@ const AddRecipe = () => { multiple freeSolo autoHighlight + fullWidth filterSelectedOptions - style={{ width: '50vw' }} className="ui-creatable-component" id="ingredient-autocomplete" options={options.map(each => each.label)} + onChange={(e) => updateSelection(e.target['innerText' as keyof EventTarget].toString())} onKeyDown={(e) => { if (e.code == 'Enter') { const inputVal: string = e.target['value' as keyof EventTarget].toString(); - const newOption = createOptionFromText(inputVal, optionCount + 1); - setOptions((prev) => [...prev, newOption]); - setOptionCount(prev => prev + 1); + console.log(inputVal) + if (inputVal.length) { + setSelected(prev => [...prev, inputVal]) + const newOption = createOptionFromText(inputVal, optionCount + 1); + setOptions((prev) => [...prev, newOption]); + setOptionCount(prev => prev + 1); + } } }} - renderTags={(value, getTagProps) => value.map((option, idx) => handleDelete(target)} />)} + renderTags={(value, getTagProps) => value.map((option, idx) => handleDelete(option)} />)} renderInput={(params) => ( { ) // create dropdown from new data + /* const selectorInstance = { options={options} onChange={(selection: MultiValue) => onChange(selection)} onCreateOption={(input: string) => onCreateOption(input, () => {})} - /> + /> + */ + data.length && setSelector(autocompleteInstance); setTriggerChange(true); } - }, [data, options]) + }, [data, options, selected]) // once the dropdown data has populated, mount it within the full form useEffect(() => { @@ -135,9 +143,15 @@ const AddRecipe = () => { setInput(data); }, [input]) + const updateSelection = (target: string) => { + setSelected((prev) => { + return [...prev, target]; + }) + } + const handleDelete = (target: string) => { - setOptions((prev) => { - return prev.filter(option => option.label !== target); + setSelected((prev) => { + return prev.filter(option => option !== target); }) } diff --git a/client/src/context/SelectorContext.tsx b/client/src/context/SelectorContext.tsx index a2b871f..55961df 100644 --- a/client/src/context/SelectorContext.tsx +++ b/client/src/context/SelectorContext.tsx @@ -4,8 +4,8 @@ import { OptionType } from "../util/types" interface SelectorContextProps { data: Array setData: Dispatch>> | VoidFunction - selected: Array - setSelected: Dispatch>> | VoidFunction + selected: Array + setSelected: Dispatch>> | VoidFunction options: Array setOptions: Dispatch>> | VoidFunction selector: JSX.Element @@ -17,7 +17,7 @@ interface SelectorContextProps { const defaultValue: SelectorContextProps = { data: new Array(), setData: () => {}, - selected: new Array(), + selected: new Array(), setSelected: () => {}, options: new Array(), setOptions: () => {}, diff --git a/client/src/context/SelectorProvider.tsx b/client/src/context/SelectorProvider.tsx index 650d9f5..6f65d48 100644 --- a/client/src/context/SelectorProvider.tsx +++ b/client/src/context/SelectorProvider.tsx @@ -6,7 +6,7 @@ function SelectorProvider({ children }: { children: JSX.Element | JSX.Element const [data, setData] = useState>([]); const [selector, setSelector] = useState(<>) const [options, setOptions] = useState>([]); - const [selected, setSelected] = useState>([]); + const [selected, setSelected] = useState>([]); /** * Event handler for a change in selection state @@ -20,7 +20,7 @@ function SelectorProvider({ children }: { children: JSX.Element | JSX.Element const newOption: OptionType = { label: label, value: newID } setOptions((prev) => [...prev, newOption]); - setSelected((prev) => [...prev, newOption]); + setSelected((prev) => [...prev, newOption.label]); setData((prev) => [...prev, generateObject(label, newID)]); }