preparing to change ingredient entry strategy
This commit is contained in:
@@ -26,7 +26,7 @@ const AddRecipe = () => {
|
|||||||
// clear out selector state on page load
|
// clear out selector state on page load
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setData(new Array<IIngredient>());
|
setData(new Array<IIngredient>());
|
||||||
setSelected(new Array<OptionType>());
|
setSelected(new Array<string>());
|
||||||
setOptions(new Array<OptionType>());
|
setOptions(new Array<OptionType>());
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
@@ -60,20 +60,25 @@ const AddRecipe = () => {
|
|||||||
multiple
|
multiple
|
||||||
freeSolo
|
freeSolo
|
||||||
autoHighlight
|
autoHighlight
|
||||||
|
fullWidth
|
||||||
filterSelectedOptions
|
filterSelectedOptions
|
||||||
style={{ width: '50vw' }}
|
|
||||||
className="ui-creatable-component"
|
className="ui-creatable-component"
|
||||||
id="ingredient-autocomplete"
|
id="ingredient-autocomplete"
|
||||||
options={options.map(each => each.label)}
|
options={options.map(each => each.label)}
|
||||||
|
onChange={(e) => updateSelection(e.target['innerText' as keyof EventTarget].toString())}
|
||||||
onKeyDown={(e) => {
|
onKeyDown={(e) => {
|
||||||
if (e.code == 'Enter') {
|
if (e.code == 'Enter') {
|
||||||
const inputVal: string = e.target['value' as keyof EventTarget].toString();
|
const inputVal: string = e.target['value' as keyof EventTarget].toString();
|
||||||
const newOption = createOptionFromText(inputVal, optionCount + 1);
|
console.log(inputVal)
|
||||||
setOptions((prev) => [...prev, newOption]);
|
if (inputVal.length) {
|
||||||
setOptionCount(prev => prev + 1);
|
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) => <Chip variant="outlined" label={option} { ...getTagProps({ index: idx }) } onDelete={(target: string) => handleDelete(target)} />)}
|
renderTags={(value, getTagProps) => value.map((option, idx) => <Chip variant="outlined" label={option} { ...getTagProps({ index: idx }) } onDelete={() => handleDelete(option)} />)}
|
||||||
renderInput={(params) => (
|
renderInput={(params) => (
|
||||||
<TextField
|
<TextField
|
||||||
{...params}
|
{...params}
|
||||||
@@ -85,6 +90,7 @@ const AddRecipe = () => {
|
|||||||
)
|
)
|
||||||
|
|
||||||
// create dropdown from new data
|
// create dropdown from new data
|
||||||
|
/*
|
||||||
const selectorInstance = <Creatable
|
const selectorInstance = <Creatable
|
||||||
className="ui-creatable-component"
|
className="ui-creatable-component"
|
||||||
id="ingredient-selector"
|
id="ingredient-selector"
|
||||||
@@ -93,11 +99,13 @@ const AddRecipe = () => {
|
|||||||
options={options}
|
options={options}
|
||||||
onChange={(selection: MultiValue<OptionType>) => onChange(selection)}
|
onChange={(selection: MultiValue<OptionType>) => onChange(selection)}
|
||||||
onCreateOption={(input: string) => onCreateOption(input, () => {})}
|
onCreateOption={(input: string) => onCreateOption(input, () => {})}
|
||||||
/>
|
/>
|
||||||
|
*/
|
||||||
|
|
||||||
data.length && setSelector(autocompleteInstance);
|
data.length && setSelector(autocompleteInstance);
|
||||||
setTriggerChange(true);
|
setTriggerChange(true);
|
||||||
}
|
}
|
||||||
}, [data, options])
|
}, [data, options, selected])
|
||||||
|
|
||||||
// once the dropdown data has populated, mount it within the full form
|
// once the dropdown data has populated, mount it within the full form
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -135,9 +143,15 @@ const AddRecipe = () => {
|
|||||||
setInput(data);
|
setInput(data);
|
||||||
}, [input])
|
}, [input])
|
||||||
|
|
||||||
|
const updateSelection = (target: string) => {
|
||||||
|
setSelected((prev) => {
|
||||||
|
return [...prev, target];
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const handleDelete = (target: string) => {
|
const handleDelete = (target: string) => {
|
||||||
setOptions((prev) => {
|
setSelected((prev) => {
|
||||||
return prev.filter(option => option.label !== target);
|
return prev.filter(option => option !== target);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import { OptionType } from "../util/types"
|
|||||||
interface SelectorContextProps<T> {
|
interface SelectorContextProps<T> {
|
||||||
data: Array<T>
|
data: Array<T>
|
||||||
setData: Dispatch<SetStateAction<Array<T>>> | VoidFunction
|
setData: Dispatch<SetStateAction<Array<T>>> | VoidFunction
|
||||||
selected: Array<OptionType>
|
selected: Array<string>
|
||||||
setSelected: Dispatch<SetStateAction<Array<OptionType>>> | VoidFunction
|
setSelected: Dispatch<SetStateAction<Array<string>>> | VoidFunction
|
||||||
options: Array<OptionType>
|
options: Array<OptionType>
|
||||||
setOptions: Dispatch<SetStateAction<Array<OptionType>>> | VoidFunction
|
setOptions: Dispatch<SetStateAction<Array<OptionType>>> | VoidFunction
|
||||||
selector: JSX.Element
|
selector: JSX.Element
|
||||||
@@ -17,7 +17,7 @@ interface SelectorContextProps<T> {
|
|||||||
const defaultValue: SelectorContextProps<any> = {
|
const defaultValue: SelectorContextProps<any> = {
|
||||||
data: new Array<any>(),
|
data: new Array<any>(),
|
||||||
setData: () => {},
|
setData: () => {},
|
||||||
selected: new Array<any>(),
|
selected: new Array<string>(),
|
||||||
setSelected: () => {},
|
setSelected: () => {},
|
||||||
options: new Array<OptionType>(),
|
options: new Array<OptionType>(),
|
||||||
setOptions: () => {},
|
setOptions: () => {},
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ function SelectorProvider<T>({ children }: { children: JSX.Element | JSX.Element
|
|||||||
const [data, setData] = useState<Array<T>>([]);
|
const [data, setData] = useState<Array<T>>([]);
|
||||||
const [selector, setSelector] = useState<JSX.Element>(<></>)
|
const [selector, setSelector] = useState<JSX.Element>(<></>)
|
||||||
const [options, setOptions] = useState<Array<OptionType>>([]);
|
const [options, setOptions] = useState<Array<OptionType>>([]);
|
||||||
const [selected, setSelected] = useState<Array<OptionType>>([]);
|
const [selected, setSelected] = useState<Array<string>>([]);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event handler for a change in selection state
|
* Event handler for a change in selection state
|
||||||
@@ -20,7 +20,7 @@ function SelectorProvider<T>({ children }: { children: JSX.Element | JSX.Element
|
|||||||
const newOption: OptionType = { label: label, value: newID }
|
const newOption: OptionType = { label: label, value: newID }
|
||||||
|
|
||||||
setOptions((prev) => [...prev, newOption]);
|
setOptions((prev) => [...prev, newOption]);
|
||||||
setSelected((prev) => [...prev, newOption]);
|
setSelected((prev) => [...prev, newOption.label]);
|
||||||
setData((prev) => [...prev, generateObject(label, newID)]);
|
setData((prev) => [...prev, generateObject(label, newID)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user