mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-03 14:52:38 +00:00
add editing of input functionality
Currently uses uncontrolled inputs with a `defaultValue` attribute set to the values passed in via props. The input can then be edited, and when `Enter` is pressed, it updates the stored value state. Later, this can be updated to be trigger with `Tab` or clicking outside the active input element.
This commit is contained in:
@@ -1,53 +1,74 @@
|
|||||||
const React = require('react');
|
const React = require('react');
|
||||||
const { useState } = React;
|
const { useState } = React;
|
||||||
const _ = require('lodash');
|
|
||||||
|
|
||||||
const TagInput = ({unique = true, values = [], ...props})=>{
|
|
||||||
|
|
||||||
|
const TagInput = ({ unique = true, values = [], ...props }) => {
|
||||||
const [temporaryValue, setTemporaryValue] = useState('');
|
const [temporaryValue, setTemporaryValue] = useState('');
|
||||||
const [valueContext, setValueContext] = useState(values.map((value)=>({ value: value, editing : false })));
|
const [valueContext, setValueContext] = useState(values.map((value) => ({ value, editing: false })));
|
||||||
|
|
||||||
|
const handleInputKeyDown = (evt, value) => {
|
||||||
|
if (evt.key === 'Enter') {
|
||||||
|
submitTag(evt.target.value, value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const editTag = (evt)=>{
|
const submitTag = (newValue, originalValue) => {
|
||||||
setValueContext(valueContext.map((context)=>{
|
setValueContext((prevContext) => {
|
||||||
context.editing = context.value === evt.target.dataset.value ? true : false;
|
return prevContext.map((context) => {
|
||||||
return context;
|
if (context.value === originalValue) {
|
||||||
}))
|
return { ...context, value: newValue, editing: false };
|
||||||
}
|
}
|
||||||
|
return context;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const renderReadTag = (value)=>{
|
const editTag = (valueToEdit) => {
|
||||||
|
setValueContext((prevContext) => {
|
||||||
|
return prevContext.map((context) => {
|
||||||
|
if (context.value === valueToEdit) {
|
||||||
|
return { ...context, editing: true };
|
||||||
|
}
|
||||||
|
return { ...context, editing: false };
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const renderReadTag = (context) => {
|
||||||
return (
|
return (
|
||||||
<div key={value}
|
<div key={context.value}
|
||||||
data-value={value}
|
data-value={context.value}
|
||||||
className={`tag`}
|
className='tag'
|
||||||
onClick={(evt)=>editTag(evt)}>
|
onClick={() => editTag(context.value)}>
|
||||||
{value}
|
{context.value}
|
||||||
</div>
|
</div>
|
||||||
)
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
const renderWriteTag = (value)=>{
|
const renderWriteTag = (context) => {
|
||||||
return (
|
return (
|
||||||
<input type='text' value={value} />
|
<input type='text'
|
||||||
)
|
key={context.value}
|
||||||
}
|
defaultValue={context.value}
|
||||||
|
onKeyDown={(evt) => handleInputKeyDown(evt, context.value)}
|
||||||
|
autoFocus
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<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); })}
|
||||||
{Object.values(valueContext).map((context, i)=>{ return context.editing ? renderWriteTag(context.value) : renderReadTag(context.value) })}
|
|
||||||
|
|
||||||
<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)} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
module.exports = TagInput;
|
module.exports = TagInput;
|
||||||
|
|||||||
Reference in New Issue
Block a user