Giter VIP home page Giter VIP logo

rc-api-registry's Introduction

React Components API Registry

See Docs

Installation

npm i rc-api-registry

How to use

components/Banner.tsx

import React, { CSSProperties, FC, ReactNode } from 'react';
import cn from 'classnames';
import { registerComponent } from 'rc-api-registry';

interface IBannerProps {
  title: string;
  description: string;
}

interface IProps extends IBannerProps {
  className?: string;
  style?: CSSProperties;
  children?: ReactNode;
  isLarge?: boolean;
}

const Component: FC<IProps> = ({
  className,
  style,
  title,
  description,
  isLarge,
  children,
}) => (
  <div
    className={cn(className, 'banner')}
    style={style}
  >
    <h1>
      {title} {isLarge && '(large)'}
    </h1>

    <p>{description}</p>

    {children}
  </div>
);

export const Banner = registerComponent<IBannerProps>()(Component, 'Banner');

components/Quote.tsx

import React, { FC, CSSProperties } from 'react';
import cn from 'classnames';
import { registerComponent } from 'rc-api-registry';

export interface IQuoteProps {
  quote: string;
}

export interface IProps extends IQuoteProps {
  className?: string;
  style?: CSSProperties;
}

const Component: FC<IProps> = ({ className, style, quote }) => (
  <div className={cn(className, 'quote')} style={style}>
    {quote}
  </div>
);

export const Quote = registerComponent()(Component, 'Quote');

components/registry.ts

import { ComponentsRegistry } from 'rc-api-registry';
import { Banner } from './Banner';
import { Quote } from './Quote';

export const registry = new ComponentsRegistry([Banner, Quote]);

App.tsx

import React from 'react';
import { TRegistryComponentsAPI } from 'rc-api-registry';
import { registry } from './Components/registry';

const data: TRegistryComponentsAPI<typeof registry> = [
  {
    key: 0,
    componentName: 'Banner',
    props: {
      title: 'Title',
      description: 'Description',
    },
    children: [
      {
        key: 2,
        componentName: 'Quote',
        props: {
          quote: 'Some quote',
        },
      },
    ],
  },
  {
    key: 1,
    componentName: 'Quote',
    props: {
      quote: 'Some quote',
    },
  },
];

const App = () => (
  <>
    {registry.render(
      data,
      undefined,
      ({ key, componentName, Component, props }) => {
        if (componentName === 'Banner') {
          return (
            <Component key={key} {...props}>
              {props.children}

              <div>additional children</div>
            </Component>
          );
        }

        return <Component key={key} {...props} />;
      }
    )}
  </>
);

export default App;

rc-api-registry's People

Contributors

antonbobrov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.