Comments (6)
Replace your App.js
content with this and check:
import React, { Component } from "react";
import "./App.css";
import Nestable from "react-nestable";
const data = [
{
id: 1,
content: "Getting Started with Redux: An Intro",
heading: 1,
elemt: "input",
children: [
{ id: 2, content: "Table of Contents", heading: 2, elemt: "input" }
]
},
{
id: 3,
content: "The Core Concepts",
heading: 1,
elemt: "input",
children: [
{
id: 4,
content: "1. Single source of truth",
heading: 2,
elemt: "input"
},
{
id: 5,
content: "2. State is read-only",
heading: 2,
elemt: "input"
},
{
id: 6,
content: "3. Changes are made with pure functions",
heading: 2,
elemt: "input"
}
]
},
{
id: 7,
content: "Best Practices",
heading: 1,
elemt: "input",
children: [
{ id: 8, content: "State Shape", heading: 2, elemt: "input" },
{
id: 9,
content: "Flat Objects",
heading: 3,
elemt: "input"
},
{ id: 10, content: "Actions", heading: 2, elemt: "input" },
{
id: 11,
content: "Reducers",
heading: 2,
elemt: "input"
}
]
},
{
id: 12,
content: "Testing",
heading: 1,
elemt: "input",
children: [
{ id: 13, content: "Action creators", heading: 2, elemt: "input" },
{
id: 14,
content: "Reducers",
heading: 2,
elemt: "input"
}
]
},
{
id: 15,
content: "Wrapping it up",
heading: 1,
elemt: "input",
children: [
{ id: 16, content: "Free eBook", heading: 2, elemt: "input" },
{
id: 17,
content: "Learn Node",
heading: 2,
elemt: "input"
},
{ id: 18, content: "Carly Kubacak", heading: 2, elemt: "input" },
{
id: 19,
content: "Carly Kubacak",
heading: 2,
elemt: "input"
},
{
id: 20,
content:
"💖 A side project brought to you from Las Vegas and DC by...",
heading: 3,
elemt: "input"
},
{
id: 21,
content: "Easiest Local Dev Environment",
heading: 2,
elemt: "input"
},
{
id: 22,
content: "Get Started with Vue.js",
heading: 2,
elemt: "input"
},
{ id: 23, content: "Scotch", heading: 2, elemt: "input" }
]
},
{
id: 24,
content: "Get Access",
heading: 1,
elemt: "input",
children: []
}
];
class App extends Component {
constructor(props) {
super(props);
this.state = {
data,
edit: {}
};
}
renderItem = ({ item }) => {
const { edit } = this.state;
return (
<div>
{typeof edit[item.id] !== 'undefined' ? (
<div>
<input
type="text" value={edit[item.id]}
onChange={e => this.onInputChange(e, item)}
/>
<button onClick={e => this.onSave(e, item)}>
save
</button>
</div>
) : (
<div onDoubleClick={e => this.onStartEdit(e, item)}>
{item.content}
</div>
)}
</div>
);
};
onInputChange = (e, item) => {
this.setState({
edit: {
...this.state.edit,
[item.id]: e.target.value
}
});
};
onStartEdit = (e, item) => {
this.setState({
edit: {
...this.state.edit,
[item.id]: item.content
}
});
};
onSave = (e, item) => {
const { data, edit } = this.state;
const id = item.id;
const newValue = edit[id];
console.log({ id, newValue });
const updatedData = (list, value) => {
return list.map(item => {
return {
...item,
content: id === item.id ? newValue : item.content,
children: item.children && updatedData(item.children, value)
};
});
};
this.setState({
data: updatedData(data, newValue),
edit: {
...this.state.edit,
[item.id]: undefined
}
});
};
onOrderChange = (items, item) => {
this.setState({ data: items })
};
render() {
const { data } = this.state;
return (
<div className="App">
<div className="col-md-6">
<Nestable
items={data}
renderItem={this.renderItem}
onChange={this.onOrderChange}
/>
</div>
</div>
);
}
}
export default App;
from react-nestable.
Hello.
You can have your own click handlers using render function, like:
const handleClick = (e, item) => {
// e.stopPropagation(); // this may be needed if you dont use drag handler
console.log("direct click!", item);
};
const renderItem = ({ item, handler }) => (
<div>
{handler}
<div onClick={e => handleClick(e, item)}>
{item.text}
</div>
</div>
);
Having clicks within every item, you can render input instead of text.
Also, you can show buttons "insert before"/"insert after" on hover inside every item.
from react-nestable.
when i'm rendering input facing some problem, at the time of drag-drop it's rendering wrong data.
from react-nestable.
could you provide some example on codepen or jsfiddle?
from react-nestable.
Sorry for late response
I'm sharing the git url
https://github.com/amitvalue2018/input-edit-nestable
If you have any query please let me know.
from react-nestable.
@primetwig , You are genius, You make developer life easy.
Thanks for the time.
from react-nestable.
Related Issues (20)
- depth on renderItem not working showing undefined HOT 2
- Issue moving tree item to different level when no other children present
- Performance/Memory issues with more than 60K records HOT 1
- [question] how can I force an item to expand? HOT 1
- [question] How do I disable nesting for an Item HOT 2
- [feature] Expose newly created items list in confirmChange HOT 6
- Queries.
- Allow hook for list of collapsed IDs HOT 1
- targetPath is `undefined` on onChange function
- Confirm after dropped Item and before rendering HOT 1
- How can I use onChange method? HOT 2
- "confirmChange" prevents reordering as well as nesting
- Custom styling for items placeholder HOT 2
- Children to be printed HOT 1
- include disable Drag HOT 5
- Hide collapsible icon when dragging HOT 2
- Getting Error while dragging expanded list to the bottom HOT 2
- TypeError: list.map is not a function HOT 1
- overflow-y scroll causes wrong dragging item position HOT 1
- How to update nth Children title HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-nestable.