Giter Club home page Giter Club logo

Comments (6)

primetwig avatar primetwig commented on July 24, 2024 1

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.

primetwig avatar primetwig commented on July 24, 2024

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.

amitvalue avatar amitvalue commented on July 24, 2024

when i'm rendering input facing some problem, at the time of drag-drop it's rendering wrong data.

from react-nestable.

primetwig avatar primetwig commented on July 24, 2024

could you provide some example on codepen or jsfiddle?

from react-nestable.

amitvalue avatar amitvalue commented on July 24, 2024

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.

amitvalue avatar amitvalue commented on July 24, 2024

@primetwig , You are genius, You make developer life easy.
Thanks for the time.

from react-nestable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.