英文:
React pattern for click-to-edit fields
问题
I'm using React 18 with functional components and I'm looking for a clean reusable pattern for "click to edit" UIs. This is the UI paradigm where:
- You have some text.
- On hover of the text, it shows visual treatment indicating the field is editable.
- On click on the text, the text is swapped for an input field allowing you to edit the text.
- On blur of the input, a callback is made to automatically save changes back to the DB and toggle the input back to text.
I can do this per input field, but I'm trying to figure out a reusable React component that I can configure with the "view" render, the "edit" render, and any callbacks. Part of this is because my inputs will change quite a bit across my app; so I'll have inputs, textareas, select fields, etc. Right now, I'm duplicating tons of code implementing a component for every "editable field."
I was playing around with something like:
<Editable>
<p>{value}</p>
<>
<label>Update the value:</label>
<input type="text"/>
</>
</Editable>
...and then access the "view" stage in Editable via children[0] and the edit state via children[1]... but I'm not sure how to attach handlers to children and its subelements (for example, I'd want to always have children[0].onClick={() => setEditing(true) }
and the children[1]'s input to have children[1].onBlur{() => setEditing(false) }
... or something like that. Also, I'm not sure if I can figure out what elements of children to attach the handlers to if I can even do that programmatically.
英文:
Im using React 18 w/ functional components, and am looking for a clean re-usable pattern for "click to edit" UIs - this is the UI paradigm where:
- You have some text
- Onhover of text, it shows visual treatment indicating the field is editable
- OnClick on text, the text is swapped for an input field allowing you to edit the text
- OnBlur of the input, a callback is made to automatically save changes back to the DB and toggle the input back to text
I can do this per input field, but im trying to figure out an a re-usable React component that i can configure w/ the "view" render, the "edit" render, and any callbacks. Part of this is because my inputs will change quite a bit across my app; so ill have inputs, textareas, select fields, etc. Right now, im duplicating tons of code implementing a component for every "editable field".
I was playing around with something like:
<Editable>
<p>{value}</p>
<>
<label>Update the value:</label>
<input type="text"/>
</>
</Editable>
...and then access the "view" stage in Editable via children[0] and the edit state via children[1]... but im not sure how to attach handlers to children and its subelements (for example, i'd want to always have children[0].onClick={() => setEditing(true) } and the children[1]'s input to have
children[1].onBlur{() => setEditing(false) }` .. or something like that. Also im not sure if i can figure out what elements of children to attach the handlers to if i can even do that programmatically.
答案1
得分: 2
以下是代码的中文翻译:
import React from 'react';
import './style.css';
let Editable = (props) => {
let [mode, setMode] = React.useState('view');
let [value, setValue] = React.useState('Hello');
// 可能还有其他逻辑...
return props.children({ mode, setMode, value, setValue });
};
export default function App() {
return (
<div>
<Editable>
{({ mode, setMode, value, setValue }) => {
return mode === 'view' ? (
<p
onClick={() => {
setMode('edit');
}}
>
{value}
</p>
) : mode === 'edit' ? (
<div>
<input
onBlur={() => {
setMode('view');
}}
value={value}
onChange={(e) => setValue(e.target.value)}
/>
</div>
) : null;
}}
</Editable>
</div>
);
}
希望这对您有所帮助!如果您需要任何进一步的信息,请告诉我。
英文:
Something like this?
import React from 'react';
import './style.css';
let Editable = (props) => {
let [mode, setMode] = React.useState('view');
let [value, setValue] = React.useState('Hello');
// Some other logic maybe ...
return props.children({ mode, setMode, value, setValue });
};
export default function App() {
return (
<div>
<Editable>
{({ mode, setMode, value, setValue }) => {
return mode === 'view' ? (
<p
onClick={() => {
setMode('edit');
}}
>
{value}
</p>
) : mode === 'edit' ? (
<div>
<input
onBlur={() => {
setMode('view');
}}
value={value}
onChange={(e) => setValue(e.target.value)}
/>
</div>
) : null;
}}
</Editable>
</div>
);
}
This approach is called render props. It allows to have your custom logic inside the component, and delegate rendering part to the user (while letting the user to consume the custom logic from the component).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论