react实现json框

1、react-json-editor-ajrm 

import React, {useState} from 'react';
import ReactJson from 'react-json-editor-ajrm';
import locale from 'react-json-editor-ajrm/locale/zh-cn';


export default function test() {
    const [data, setData] = useState<any>({"type": 1});
    console.log(data);
    return (
        <>
            <div>
                <ReactJson
                    locale={locale}
                    placeholder={data}
                    confirmGood={false}
                    onKeyPressUpdate={true}
                    theme="light_mitsuketa_tribute"
                    style={{
                    outerBox: { height: "auto", maxHeight: "350px", width: "100%" },
                    container: {
                        height: "auto",
                        maxHeight: "350px",
                        width: "100%",
                        overflow: "scroll"
                    },
                    body: { minHeight: "45px", width: "100%" }
                    }}
                    onChange={(v: any) => setData(v)}  
                />
            </div>
        </>

    );
}

效果:

2、@monaco-editor/react

import React, {useState} from 'react';
import Editor from "@monaco-editor/react";


export default function AITool() {
    const data = {
        "type": 1,
        "range": {
          "prefix": "D",
          "start": 21000,
          "length": 276
        },
        "holding": {
          "address": "D20005",
          "value": 1
        },
        "done": [1]
      };
    const [error, setError] = useState<any>(null);
    const [json, setJson] = useState(JSON.stringify(data, null, 2));
    const handleEditorChange = (value: any, event: any) => {
        console.log("hi", event, value);
        try {
            JSON.parse(value);
            console.log(value);
            setJson(value);
            setError(null);
        } catch (error) {
          setError('非法json, 请检查并修正');
        }
    };

    return (
        <>
            <div>
                {error && <div>{error}</div>}
                <div>
                    <Editor
                        height="90vh"
                        defaultLanguage="json"
                        defaultValue={json}
                        onChange={handleEditorChange}
                    />
                </div>
            </div>
        </>

    );
}

效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值