Git Product home page Git Product logo

react-json-editor-ui's Introduction

React Json Edit

React-based visual json editor.

Getting Started

Install

npm i react-json-editor-ui -S

Props

key description required default
width The container width false 500
data The editor uses data true null
onChange Callback the data true null
optionsMap When a match for auto-complete on the input value false null

Example:

import * as React from 'react'
import * as ReactDOM from 'react-dom'
import JsonEditor from 'react-json-editor-ui'
import 'react-json-editor-ui/dist/react-json-editor-ui.cjs.development.css'

const App = () => {
  const [editObject, setEditObject] = React.useState<any>({
    name: 'may',
    age: null,
    address: [
      'Panyu Shiqiao on Canton',
      'Tianhe',
      {
        city: 'forida meta 11',
      },
    ],
    others: {
      id: 1246,
      joinTime: '2017-08-20. 10:20',
      description: 'another',
    },
  })

  return (
    <JsonEditor
      data={editObject}
      onChange={data => {
        setEditObject(data)
      }}
      optionsMap={{
        color: [
          { value: 'red', label: 'Red' },
          { value: 'blue', label: 'Blue' },
        ],
        city: [
          { value: 'beijing', label: 'Beijing' },
          { value: 'shanghai', label: 'Shanghai' },
        ],
      }}
    />
  )
}
export default App

License

MIT

Copyright (c) 2013-present

react-json-editor-ui's People

Contributors

itsatan avatar jianxiaobai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-json-editor-ui's Issues

Unable to change data from outside the component

Hello,

When trying to edit the data received by the Editor from outside the component, the data state updates, but the component does not re-render. Is there any way to fix this?

My code:

import { useState } from "react";
import JsonEditor from "react-json-editor-ui";
import "react-json-editor-ui/dist/react-json-editor-ui.cjs.development.css";

const App = () => {
  const initialData = {
    id: 1,
    title: "TestProject",
    arr: [],
  };

  let isInitial = true;

  const [undoAvailable, setUndoAvailable] = useState(false);
  const [redoAvailable, setRedoAvailable] = useState(false);

  const [undoData, setUndoData] = useState(null);
  const [redoData, setRedoData] = useState(null);
  const [data, setData] = useState(initialData);

  const logToConsole = () => {
    console.log("data:", data);
  };

  const handleChange = (newData, isManual?: "undo" | "redo") => {
    console.log("Changed!");

    if (isManual) {
      switch (isManual) {
        case "undo":
          setData(newData);
          break;
        case "redo":
          setData(newData);
          break;
        default:
          setData(newData);
      }
    } else {
      setData(newData);
    }

    isInitial = false;
    if (!isInitial) {
      setUndoAvailable(true);
    }
  };

  const handleUndo = () => {
    console.log("Undo!");

    setRedoData(data);
    handleChange(undoData, "undo");

    setRedoAvailable(true);
    setUndoAvailable(false);
  };

  const handleRedo = () => {
    console.log("Redo!");

    setUndoData(data);
    handleChange(redoData, "redo");

    setRedoAvailable(false);
    setUndoAvailable(true);
  };

  const resetData = () => {
    console.log("Reset!");

    setData(initialData);
    isInitial = true;
  };

  return (
    <div
      style={{ border: "2px solid black", margin: "1rem", padding: "0.25rem" }}
    >
      <div style={{ border: "2px solid black", padding: "0.5rem" }}>
        <JsonEditor
          data={data}
          onChange={(data) => {
            handleChange(data);
          }}
          optionsMap={{
            color: [
              { value: "red", label: "Red" },
              { value: "blue", label: "Blue" },
            ],
            city: [
              { value: "beijing", label: "Beijing" },
              { value: "shanghai", label: "Shanghai" },
            ],
          }}
        />
      </div>
      <button
        onClick={() => {
          logToConsole();
        }}
        style={{
          margin: "0.5rem",
          marginLeft: "0",
          padding: "0.25rem",
          cursor: "pointer",
        }}
      >
        Log to console
      </button>
      <button
        onClick={() => {
          resetData();
        }}
        style={{
          margin: "0.5rem",
          marginLeft: "0",
          padding: "0.25rem",
          cursor: "pointer",
        }}
      >
        Reset data
      </button>
      <div style={{ float: "right" }}>
        <button
          style={{
            margin: "0.5rem",
            marginRight: "0",
            padding: "0.25rem",
            ...(undoAvailable
              ? { cursor: "pointer" }
              : { cursor: "not-allowed" }),
          }}
          onClick={() => {
            handleUndo();
          }}
          disabled={!undoAvailable}
        >
          Undo
        </button>
        <button
          style={{
            margin: "0.5rem",
            marginRight: "0",
            padding: "0.25rem",
            ...(redoAvailable
              ? { cursor: "pointer" }
              : { cursor: "not-allowed" }),
          }}
          onClick={() => {
            handleRedo();
          }}
          disabled={!redoAvailable}
        >
          Redo
        </button>
      </div>
    </div>
  );
};

export default App;

Changing keys in version 1.0.8 doesn't work

It seems that when changing the key name the object gets recursively duplicated under the previous key

Screen.Recording.2023-07-10.at.15.09.57.mov

Version 1.0.7 doesn't have this issue

Removing object keys messes up the field types

Screen.Recording.2023-08-24.at.14.12.00.mov

After removing the joinTime key, the description key seems to "take over" the type of the key above it, in this case, it becomes a boolean.

Incorrect boolean values in editor

Sometimes editor shows incorrect boolean values. As in screenshot below the right part shows stringified object that is passed to editor and the boolean value here is correct. But the left part (editor) shows incorrectly.
Screenshot from 2023-04-12 10-40-12

Package Breaks Local Antd Theming

I'm trying to use this library in a project built with antd 4. For some reason, importing the component (without even importing the styles) breaks my theming and makes the app goes back to the default blue Color.

`data` works just as initial value. The component is not stateless

Description

Based on the example: https://github.com/jianxiaoBai/react-json-editor-ui/blob/main/example/index.tsx#L46 the component should be stateless. We contain a state on the business level but JsonEditor just accepts the data and provides new data in the onChange callback.
But it doesn't work. If you try to mock the onChange function and do not change your state the component will work. The problem is: I couldn't change the component state outside. If I need to provide different data based on my state I couldn't pass it into JsonEditor because the component will ignore it.

Question

Is this component should be stateless and this is a bug or it should contain state and it's impossible to change outside?

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.