1.1.0-alpha.18 • Published 2 years ago

react-mc-runner v1.1.0-alpha.18

Weekly downloads
16
License
MIT
Repository
github
Last release
2 years ago

react-mc-runner

Render the value we got and bound the events.

Installation

Using npm:

$ npm install --save react-mc-runner

Usage

import React, { useState } from 'react';

import ReactMCRunner from 'react-mc-runner';

/**
 * In this case,
 * will only render some "div" with different id.
 * Because we have not set up options.getComponentClass.
 */
const App = (props = {}) => {
  const [value = {}, setValue] = useState({});

  return (
    <ReactMCRunner
      value={value}
      setValue={setValue}
    />
  );
};

export default App;

props.value: object

The value you can render.

Detail for value;

props.setValue: func

Trigger it when value was changed.The argument is a function which return next value;

const App = (props = {}) => {
  const [value = {}, setValue] = useState({});

  // Example: deal with setValue
  const setValueForRunner = (changer) => {
    const nextValue = changer(value);

    setValue(nextValue);
  };

  // Or something native
  const setValueForRunner = setValue;

  return (
    <ReactMCRunner
      value={value}
      setValue={setValueForRunner}
    />
  );
};

props.options: object

The parameters you need to configure to render value in the way you want.

const options = {
  getComponentClass: (component = {}) => 'div',
  getComponentRenderDependencies: (component = {}) => [],
  getComponentPropsSchema: (component = {}) => ({}),
  render: (ComponentClass, component = {}) => (props = {}, ref) => {
    return (
      <ComponentClass ref={ref} {...props} />
    );
  },
};

options.getComponentClass

Return the ComponentClass to render the component.

import React from 'react';

const Input = React.forwardRef((props, ref) => {
  return 
});

const Text = React.forwardRef((props, ref) => {
  return <span ref={ref} {...props} />
});

const Img = React.forwardRef((props, ref) => {
  return <img ref={ref} {...props} />
});

const getComponentClass = (component = {}) => {
  const { name } = component;

  switch (name) {
    case 'input':
      return Input;
    case 'span':
      return Text;
    case 'img':
      return Img;
    default:
      return 'div';
  }
};

options.getComponentRenderDependencies

Ruturn the dependencies for rendering.The ComponentClass will render only if dependencies change.component and relation are already in the dependencies by default.

const selectedComponent = {};
const { id: selectedComponentId } = selectedComponent;

// render when select the component
const getComponentRenderDependencies = (component = {}) => {
  const { id: componentId } = component;

  const selected = selectedComponentId === componentId;

  return [selected];
};

options.getComponentPropsSchema

We use PropsSchema to define PropTypes. For webpack, you can add a plugin to babel to generate automatically.

Base on JSONSchema

Webpack example

options.render

The final touch to render component correctly.This function is useful when you want to do something nasty.

import { findDOMNode } from 'react-dom';

/**
 * ComponentClass: elementType, is what getComponentClass return
 * props: object, base on component.props, relation and bound events.
 */
const render = (ComponentClass, component = {}) => (props = {}, ref) => {
  const { current } = ref;
  const dom = findDOMNode(current);

  return (
    <ComponentClass ref={ref} {...props} />
  );
};
1.1.0-alpha.18

2 years ago

1.1.0-alpha.17

2 years ago

1.1.0-alpha.16

3 years ago

1.1.0-alpha.15

3 years ago

1.1.0-alpha.14

3 years ago

1.1.0-alpha.13

3 years ago

1.1.0-alpha.12

3 years ago

1.1.0-alpha.10

3 years ago

1.1.0-alpha.9

3 years ago

1.1.0-alpha.7

3 years ago

1.1.0-alpha.8

3 years ago

1.1.0-alpha.5

3 years ago

1.1.0-alpha.4

3 years ago

1.1.0-alpha.2

3 years ago

1.0.8

3 years ago

1.0.5-alpha.1

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.0.2

4 years ago

0.0.1-alpha.35

4 years ago

0.0.1-alpha.26

4 years ago

0.0.1-alpha.25

4 years ago

0.0.1-alpha.23

4 years ago

0.0.1-alpha.24

4 years ago

0.0.1-alpha.12

4 years ago

0.0.1-alpha.11

4 years ago

0.0.1-alpha.10

4 years ago

0.0.1-alpha.9

4 years ago