React – 优化 – 正确在表单字段中显示 JSON 对象

huangapple go评论64阅读模式
英文:

React - Refine - Display Json object correctly in form field

问题

以下是您要翻译的内容:

{
	"id": "bb8dc1fa84d842bd82e3494f2d9b8e49_LATEST",
	"status": "publish",
	"version": "LATEST",
	"permission_document": {
		"Version": "2023-05-22",
		"Statement": ["*"],
		"Signature": "9434903948034389439843834900920-320-3-2024904209429049033490430--340"
	}
}

您想要在React表单字段中显示'permission_document'属性,用户应该能够编辑某些字段,然后将其重新提交到后端。

这是用于显示表单的前端代码:

import React from "react";
import { IResourceComponentsProps } from "@refinedev/core";
import { Edit, useForm, useFileUploadState } from "@refinedev/antd";
import { Form } from "antd";
import MDEditor from "@uiw/react-md-editor";
import { IPermissionDocument } from "interfaces";

export const PermissionDocumentEdit: React.FC<IResourceComponentsProps> = () => {
    
    const { formProps, saveButtonProps } = useForm<IPermissionDocument>();
    
    return (
        <Edit saveButtonProps={{ ...saveButtonProps }}>
            <Form {...formProps} layout="vertical">
                <Form.Item
                    label="Permission Document"
                    name="permission_document"                    
                    rules={[
                        {
                            required: true,
                        },
                    ]}
                >

                <MDEditor 
                    id="permission_document" 
                    preview='edit' data-color-mode="light"                
                />
                </Form.Item>
            </Form>
        </Edit>
    );
};

如何以漂亮的方式显示JSON?
点击此处查看前端显示和当前显示的外观

我缺少一些前端技能来找到这个问题的解决方案。

英文:

I have the following json object retrieved from the backend REST API.

{
	&quot;id&quot;: &quot;bb8dc1fa84d842bd82e3494f2d9b8e49_LATEST&quot;,
	&quot;status&quot;: &quot;publish&quot;,
	&quot;version&quot;: &quot;LATEST&quot;,
	&quot;permission_document&quot;: {
		&quot;Version&quot;: &quot;2023-05-22&quot;,
		&quot;Statement&quot;: [&quot;*&quot;],
		&quot;Signature&quot;: &quot;9434903948034389439843834900920-320-3-2024904209429049033490430--340&quot;
	}
}

I want to display the attribute 'permission_document' in a React form field where the user should be able to edit some fields and then resubmit it to the backend.

Im using the Refine dashboard template: https://refine.dev/
And this is the front-end code for displaying the form:

import React from &quot;react&quot;;
import { IResourceComponentsProps } from &quot;@refinedev/core&quot;;

import { Edit, useForm, useFileUploadState } from &quot;@refinedev/antd&quot;;

import { Form } from &quot;antd&quot;;

import MDEditor from &quot;@uiw/react-md-editor&quot;;

import { IPermissionDocument } from &quot;interfaces&quot;;

export const PermissionDocumentEdit: React.FC&lt;IResourceComponentsProps&gt; = () =&gt; {
    
    const { formProps, saveButtonProps } = useForm&lt;IPermissionDocument&gt;();
    
    return (
        &lt;Edit saveButtonProps={{ ...saveButtonProps }}&gt;
            &lt;Form {...formProps} layout=&quot;vertical&quot;&gt;
                &lt;Form.Item
                    label=&quot;Permission Document&quot;
                    name=&quot;permission_document&quot;                    
                    rules={[
                        {
                            required: true,
                        },
                    ]}
                &gt;

                &lt;MDEditor 
                    id=&quot;permission_document&quot; 
                    preview=&#39;edit&#39; data-color-mode=&quot;light&quot;                
                /&gt;
                &lt;/Form.Item&gt;
            &lt;/Form&gt;
        &lt;/Edit&gt;
    );
};

How to display the json as pretty json?
Check here to see how it looks on the frond end and how it is currently displayed

I lack a little bit of frond end skills to find the solution for this problem

答案1

得分: 1

你试图在编辑器内打印对象。
在显示之前,你必须将对象转换为字符串,像这样:

JSON.stringify(JSONobject, "", 2)

2 是缩进的空格数。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

英文:

you're trying to print the object inside the editor.
You have to transform your object in a string before displaying it, like this:

JSON.stringify(JSONobject, &quot;&quot;, 2)

2 is the number of spaces for indentation.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

huangapple
  • 本文由 发表于 2023年5月23日 00:32:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定