@a-b-r-o-w-n/react-json-view 中文文档教程

发布于 5年前 浏览 20 项目主页 更新于 3年前

alt text

npmnpm构建状态Coverage Status

react-json-view

RJV 是一个 React 组件,用于显示和编辑 javascript arraysJSON 对象

该组件提供了一个响应式界面,用于在 Web 浏览器中显示数组或 JSON。 NPM 提供了一个被转译为 ES5 的源代码分布; 因此您可以将此组件包含在任何基于 Web 的 javascript 应用程序中。

查看交互式演示

Implementation Example

// import the react-json-view component
import ReactJson from 'react-json-view'

// use the component in your app!
<ReactJson src={my_json_object} />

Output Examples

Default Theme

alt text

Hopscotch Theme, with Triangle Icons:

alt text

Installation Instructions

安装这个NPM 组件。

npm install --save react-json-view

或者添加到您的 package.json 配置文件:

"dependencies": {
    "react-json-view": "latest"
}

Props

NameTypeDefaultDescription
srcJSON ObjectNoneThis property contains your input JSON
namestring or false"root"Contains the name of your root node. Use null or false for no name.
themestring"rjv-default"RJV supports base-16 themes. Check out the list of supported themes in the demo. A custom "rjv-default" theme applies by default.
styleobject{}Style attributes for react-json-view container. Explicit style attributes will override attributes provided by a theme.
iconStylestring"circle"Style of expand/collapse icons. Accepted values are "circle", triangle" or "square".
indentWidthinteger4Set the indent-width for nested objects
collapsedboolean or integerfalseWhen set to true, all nodes will be collapsed by default. Use an integer value to collapse at a particular depth.
collapseStringsAfterLengthintegerfalseWhen an integer value is assigned, strings will be cut off at that length. Collapsed strings are followed by an ellipsis. String content can be expanded and collapsed by clicking on the string value.
shouldCollapse(field)=>{}falseCallback function to provide control over what objects and arrays should be collapsed by default. An object is passed to the callback containing name, src, type ("array" or "object") and namespace.
groupArraysAfterLengthinteger100When an integer value is assigned, arrays will be displayed in groups by count of the value. Groups are displayed with brakcet notation and can be expanded and collapsed by clickong on the brackets.
enableClipboardboolean or (copy)=>{}trueWhen prop is not false, the user can copy objects and arrays to clipboard by clicking on the clipboard icon. Copy callbacks are supported.
displayObjectSizebooleantrueWhen set to true, objects and arrays are labeled with size
displayDataTypesbooleantrueWhen set to true, data type labels prefix values
onEdit(edit)=>{}falseWhen a callback function is passed in, edit functionality is enabled. The callback is invoked before edits are completed. Returning false from onEdit will prevent the change from being made. see: onEdit docs
onAdd(add)=>{}falseWhen a callback function is passed in, add functionality is enabled. The callback is invoked before additions are completed. Returning false from onAdd will prevent the change from being made. see: onAdd docs
defaultValuestring |number |boolean |array |objectnullSets the default value to be used when adding an item to json
onDelete(delete)=>{}falseWhen a callback function is passed in, delete functionality is enabled. The callback is invoked before deletions are completed. Returning false from onDelete will prevent the change from being made. see: onDelete docs
onSelect(select)=>{}falseWhen a function is passed in, clicking a value triggers the onSelect method to be called.
sortKeysbooleanfalseset to true to sort object keys
validationMessagestring"Validation Error"Custom message for validation failures to onEdit, onAdd, or onDelete callbacks

Features

  • onEdit, onAdd and onDelete props allow users to edit the src variable
  • Object, array, string and function values can be collapsed and expanded
  • Object and array nodes display length
  • Object and array nodes support a "Copy to Clipboard" feature
  • String values can be truncated after a specified length
  • Arrays can be subgrouped after a specified length
  • Base-16 Theme Support
  • When onEdit is enabled:
  • Ctrl/Cmd+Click Edit Mode
  • Ctrl/Cmd+Enter Submit

Customizing Style

Stock Themes

RJV 现在支持 base-16 主题!

当您实例化您的 rjv 组件时,您可以指定一个 theme 名称或对象。

<ReactJson src={my_important_json} theme="monokai" />

查看支持的主题列表在组件演示中

Monokai theme example

替代文本

Solarized theme example

alt text

Use Your Own Theme

您可以提供自己的 base-16 主题对象。

为了更好地理解自定义主题,请查看 我的示例实现base-16 主题样式指南

onEdit, onAdd and onDelete Interaction

将回调方法传递给 onEditonAddonDelete 属性。 当用户尝试更新您的 src 对象时,将调用您的方法。

以下对象将传递给您的方法:

{
    updated_src: src, //new src value
    name: name, //new var name
    namespace: namespace, //list, namespace indicating var location
    new_value: new_value, //new variable value
    existing_value: existing_value, //existing variable value
}

从回调方法返回 false 将防止 src 受到影响。

Contributing to the source code

Run the Dev Server

# clone this repository
git clone git@github.com:mac-s-g/react-json-view.git && cd react-json-view
# install dependencies
npm install --save-dev
# run the dev server with hot reloading
npm run dev

Webpack Dev Server 应该会在您的 Web 浏览器中自动打开 http://localhost:2000。 如果没有,请打开浏览器并导航到端口 2000。当 /src/ 目录中的文件被修改时,热加载器将自动重新加载。

Run the Production Build

# run the build (note: you may need to use `sudo` priveledges to run the build successfully)
npm run build

请在发布拉取请求之前为您的代码添加测试。

您可以使用 npm run testnpm run test:watch 运行测试套件,以便在文件被修改时自动重新加载。

Docker Tools

我建议使用 docker 进行开发,因为它强制执行环境一致性。

有关使用 Docker 进行贡献的信息,请参阅 ./docker 中的自述文件

Inspiration

我从 react-json-tree 中汲取了大量的设计理念。 感谢 RJT 贡献者组合了一个很棒的组件!

我也受到提出有趣功能请求的用户的启发。 如果您对这个项目或其他您想合作的项目有想法,请与我联系。 我的电子邮件地址列在我的 github 用户页面 上。

alt text

npmnpmBuild StatusCoverage Status

react-json-view

RJV is a React component for displaying and editing javascript arrays and JSON objects.

This component provides a responsive interface for displaying arrays or JSON in a web browser. NPM offers a distribution of the source that's transpiled to ES5; so you can include this component with any web-based javascript application.

Check out the Interactive Demo

Implementation Example

// import the react-json-view component
import ReactJson from 'react-json-view'

// use the component in your app!
<ReactJson src={my_json_object} />

Output Examples

Default Theme

alt text

Hopscotch Theme, with Triangle Icons:

alt text

Installation Instructions

Install this component with NPM.

npm install --save react-json-view

Or add to your package.json config file:

"dependencies": {
    "react-json-view": "latest"
}

Props

NameTypeDefaultDescription
srcJSON ObjectNoneThis property contains your input JSON
namestring or false"root"Contains the name of your root node. Use null or false for no name.
themestring"rjv-default"RJV supports base-16 themes. Check out the list of supported themes in the demo. A custom "rjv-default" theme applies by default.
styleobject{}Style attributes for react-json-view container. Explicit style attributes will override attributes provided by a theme.
iconStylestring"circle"Style of expand/collapse icons. Accepted values are "circle", triangle" or "square".
indentWidthinteger4Set the indent-width for nested objects
collapsedboolean or integerfalseWhen set to true, all nodes will be collapsed by default. Use an integer value to collapse at a particular depth.
collapseStringsAfterLengthintegerfalseWhen an integer value is assigned, strings will be cut off at that length. Collapsed strings are followed by an ellipsis. String content can be expanded and collapsed by clicking on the string value.
shouldCollapse(field)=>{}falseCallback function to provide control over what objects and arrays should be collapsed by default. An object is passed to the callback containing name, src, type ("array" or "object") and namespace.
groupArraysAfterLengthinteger100When an integer value is assigned, arrays will be displayed in groups by count of the value. Groups are displayed with brakcet notation and can be expanded and collapsed by clickong on the brackets.
enableClipboardboolean or (copy)=>{}trueWhen prop is not false, the user can copy objects and arrays to clipboard by clicking on the clipboard icon. Copy callbacks are supported.
displayObjectSizebooleantrueWhen set to true, objects and arrays are labeled with size
displayDataTypesbooleantrueWhen set to true, data type labels prefix values
onEdit(edit)=>{}falseWhen a callback function is passed in, edit functionality is enabled. The callback is invoked before edits are completed. Returning false from onEdit will prevent the change from being made. see: onEdit docs
onAdd(add)=>{}falseWhen a callback function is passed in, add functionality is enabled. The callback is invoked before additions are completed. Returning false from onAdd will prevent the change from being made. see: onAdd docs
defaultValuestring |number |boolean |array |objectnullSets the default value to be used when adding an item to json
onDelete(delete)=>{}falseWhen a callback function is passed in, delete functionality is enabled. The callback is invoked before deletions are completed. Returning false from onDelete will prevent the change from being made. see: onDelete docs
onSelect(select)=>{}falseWhen a function is passed in, clicking a value triggers the onSelect method to be called.
sortKeysbooleanfalseset to true to sort object keys
validationMessagestring"Validation Error"Custom message for validation failures to onEdit, onAdd, or onDelete callbacks

Features

  • onEdit, onAdd and onDelete props allow users to edit the src variable
  • Object, array, string and function values can be collapsed and expanded
  • Object and array nodes display length
  • Object and array nodes support a "Copy to Clipboard" feature
  • String values can be truncated after a specified length
  • Arrays can be subgrouped after a specified length
  • Base-16 Theme Support
  • When onEdit is enabled:
  • Ctrl/Cmd+Click Edit Mode
  • Ctrl/Cmd+Enter Submit

Customizing Style

Stock Themes

RJV now supports base-16 themes!

You can specify a theme name or object when you instantiate your rjv component.

<ReactJson src={my_important_json} theme="monokai" />

Check out the list of supported themes in the component demo.

Monokai theme example

alt text

Solarized theme example

alt text

Use Your Own Theme

You can supply your own base-16 theme object.

To better understand custom themes, take a look at my example implementation and the base-16 theme styling guidelines.

onEdit, onAdd and onDelete Interaction

Pass callback methods to onEdit, onAdd and onDelete props. Your method will be invoked when a user attempts to update your src object.

The following object will be passed to your method:

{
    updated_src: src, //new src value
    name: name, //new var name
    namespace: namespace, //list, namespace indicating var location
    new_value: new_value, //new variable value
    existing_value: existing_value, //existing variable value
}

Returning false from a callback method will prevent the src from being affected.

Contributing to the source code

Run the Dev Server

# clone this repository
git clone git@github.com:mac-s-g/react-json-view.git && cd react-json-view
# install dependencies
npm install --save-dev
# run the dev server with hot reloading
npm run dev

Webpack Dev Server should automatically open up http://localhost:2000 in your web browser. If it does not, open a browser and navigate to port 2000. The hot reloader will automatically reload when files are modified in the /src/ directory.

Run the Production Build

# run the build (note: you may need to use `sudo` priveledges to run the build successfully)
npm run build

Please add tests for your code before posting a pull request.

You can run the test suite with npm run test or npm run test:watch to automatically reload when files are modified.

Docker Tools

I recommend using docker for development because it enforces environmental consistency.

For information about contributing with Docker, see the README in ./docker.

Inspiration

I drew a ton of design ideas from react-json-tree. Thanks to the RJT contributors for putting together an awesome component!

I'm also inspired by users who come up with interesting feature requests. Reach out to me with ideas for this project or other projects you want to collaborate on. My email address is listed on my github user page.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文