npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json2html-react

v2.3.2

Published

The main function is to render JSON into a page, including page action and linkage between components.

Downloads

20

Readme

Document

ENCN

Introduction

The main function of json2html-react is to render JSON into a page, including page action and linkage between components.

Instructions for use

  1. Download json2html-react:
npm i -S json2html-react
# or
yarn add json2html-react
# or
pnpm i -S json2html-react
  1. Reference:
import { useState, useEffect } from 'react';
import components from '../utils/components'
import actions from '../utils/actions'
import { RenderJSON, registerAction, registerComponent } from 'json2html-react';
import data from '../examples/mock.json'

export default function DynamicLinkage() {
  const [renderData, setRenderData] = useState(null);

  // Register customized actions
  useEffect(() => {
    registerAction({
      onSubmit: async (d, {form}) => {
        console.log('json data:', d);
        try {
          const values = await form.validateFields();
          console.log('form values', values);
        } catch (errorList) {
          errorList.forEach(({ name, errors }) => {
            // Do something...
          });
        }
      },
    });
  }, []);

  useEffect(() => {
    // Register common actions
    registerAction(actions)
    // Register common components
    registerComponent(components)
  }, [])

  useEffect(() => {
    // JSON data is saved by the backend, there is mock by timeout function.
    const timer = setTimeout(() => {
      setRenderData(data)
    }, 100)

    return () => {
      clearTimeout(timer)
    }
  }, [])

  if (!renderData) {
    return null;
  }

  const options = {
    renderJson: renderData, // Required!JSON data to be rendered.
    css: '', // Not required! There is class/id and so on.
    initialValues: {}, // Not required! Initialize the form values.
    events: { // Not required! There is form components binding events.
      onChange: (v, opt) => {
        const {form, pathName}  = opt || {};
        console.log('form key:', pathName);
        console.log('form value:', v);
        console.log('form:', form);
        console.log('get form vaue by pathName: ', form.getFieldValue(pathName));
      }
    }
  }

  return (<RenderJSON {...options} />)
}

JSON data structure descriptions:

{
  // normal properties
  widget: String, // for component mapping.
  jChildren: Array | Object, // the children components.
  jProps: Object, // the props to the component.
  action: Array | Object, // bind onClick event to the component.
    {
      type: String, // for action mapping.
      data: Any, // data to the action.
    }
  
  // form properties
  dataBind: String, // The corresponding form key of the current component, which is also a part of the path.
  isFormField: Boolean // When it is true, it indicates that the current component is a form component.
  rules: Array, // form rules.
  linkage: String, // Linkage script, returning Object will be passed in the component; If empty, hide the component.
  validateTrigger: String, // The current component verification time, onChange | onBlur, etc. defaults to onChange.
}

form

Currently, all the content related to the form, json2html-react has been processed. The form object will be exposed to events and actions. please refer to rc-field-form

example🌰

  1. Clone project:
git clone https://github.com/alan1111/json2html-react.git
  1. Install dependencies:
npm i
  1. Start the application:
npm run dev
# or
yarn dev
# or
pnpm dev
  1. Visit http://location:3000。

Any other problems?

Welcome to scan the QR code below, you can leave any questions you want to know.