0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-10 02:42:43 +00:00

Set new tag input to clear itself after submission

Now whenever a new tag is submitted, the input element is cleared and ready for the next tag.

Whitespace cleanup.
This commit is contained in:
Gazook89
2024-09-18 23:39:26 -05:00
parent c1288ce4bb
commit a54adc1e4b

View File

@@ -16,19 +16,17 @@ const TagInput = ({ unique = true, values = [], ...props }) => {
}) })
}; };
const handleInputKeyDown = (evt, value) => { const handleInputKeyDown = (evt, value, clear = false) => {
if (evt.key === 'Enter') { if (evt.key === 'Enter') {
submitTag(evt.target.value, value); submitTag(evt.target.value, value);
} if(clear){ setTemporaryValue(''); }
};
}; };
const submitTag = (newValue, originalValue, index) => { const submitTag = (newValue, originalValue, index) => {
setValueContext((prevContext) => { setValueContext((prevContext) => {
// remove existing tag // remove existing tag
if(newValue === null){ if(newValue === null){
console.log('remove');
return [...prevContext].filter((context, i)=>i !== index); return [...prevContext].filter((context, i)=>i !== index);
} }
// add new tag // add new tag
@@ -90,7 +88,7 @@ const TagInput = ({ unique = true, values = [], ...props }) => {
placeholder={props.placeholder} placeholder={props.placeholder}
value={temporaryValue} value={temporaryValue}
onChange={(e) => setTemporaryValue(e.target.value)} onChange={(e) => setTemporaryValue(e.target.value)}
onKeyDown={(evt) => handleInputKeyDown(evt, null)} /> onKeyDown={(evt) => handleInputKeyDown(evt, null, true)} />
</div> </div>
</div> </div>
); );