mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-08 18:22:40 +00:00
Add method for adding new tags
Component now accepts new tags entered in the always-present input field. Entering a value and hitting Enter submits the tag, and it appears as a new tag. Updated the tag list keys to be unique (via `index`). To-Do: empty 'new tag' input after submitting.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
const React = require('react');
|
const React = require('react');
|
||||||
const { useState, useEffect } = React;
|
const { useState, useEffect } = React;
|
||||||
|
const _ = require('lodash');
|
||||||
|
|
||||||
const TagInput = ({ unique = true, values = [], ...props }) => {
|
const TagInput = ({ unique = true, values = [], ...props }) => {
|
||||||
const [temporaryValue, setTemporaryValue] = useState('');
|
const [temporaryValue, setTemporaryValue] = useState('');
|
||||||
@@ -25,6 +26,11 @@ const TagInput = ({ unique = true, values = [], ...props }) => {
|
|||||||
|
|
||||||
const submitTag = (newValue, originalValue) => {
|
const submitTag = (newValue, originalValue) => {
|
||||||
setValueContext((prevContext) => {
|
setValueContext((prevContext) => {
|
||||||
|
// add new tag
|
||||||
|
if(originalValue === null){
|
||||||
|
return [...prevContext, { value: newValue, editing: false }]
|
||||||
|
}
|
||||||
|
// update existing tag
|
||||||
return prevContext.map((context) => {
|
return prevContext.map((context) => {
|
||||||
if (context.value === originalValue) {
|
if (context.value === originalValue) {
|
||||||
return { ...context, value: newValue, editing: false };
|
return { ...context, value: newValue, editing: false };
|
||||||
@@ -45,9 +51,9 @@ const TagInput = ({ unique = true, values = [], ...props }) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const renderReadTag = (context) => {
|
const renderReadTag = (context, index) => {
|
||||||
return (
|
return (
|
||||||
<div key={context.value}
|
<div key={index}
|
||||||
data-value={context.value}
|
data-value={context.value}
|
||||||
className='tag'
|
className='tag'
|
||||||
onClick={() => editTag(context.value)}>
|
onClick={() => editTag(context.value)}>
|
||||||
@@ -56,10 +62,10 @@ const TagInput = ({ unique = true, values = [], ...props }) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const renderWriteTag = (context) => {
|
const renderWriteTag = (context, index) => {
|
||||||
return (
|
return (
|
||||||
<input type='text'
|
<input type='text'
|
||||||
key={context.value}
|
key={index}
|
||||||
defaultValue={context.value}
|
defaultValue={context.value}
|
||||||
onKeyDown={(evt) => handleInputKeyDown(evt, context.value)}
|
onKeyDown={(evt) => handleInputKeyDown(evt, context.value)}
|
||||||
autoFocus
|
autoFocus
|
||||||
@@ -71,13 +77,14 @@ const TagInput = ({ unique = true, values = [], ...props }) => {
|
|||||||
<div className='field'>
|
<div className='field'>
|
||||||
<label>{props.label}</label>
|
<label>{props.label}</label>
|
||||||
<div className='list'>
|
<div className='list'>
|
||||||
{valueContext.map((context) => { return context.editing ? renderWriteTag(context) : renderReadTag(context); })}
|
{valueContext.map((context, index) => { return context.editing ? renderWriteTag(context, index) : renderReadTag(context, index); })}
|
||||||
|
|
||||||
<input type='text'
|
<input type='text'
|
||||||
className='value'
|
className='value'
|
||||||
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)} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user