Git Product home page Git Product logo

builder-helpers's Introduction

Builder.io helpers

Install

npm i -D @oak-digital/builder-helpers
# or
pnpm add -D @oak-digital/builder-helpers
# or
yarn add -D @oak-digital/builder-helpers

Infer props for a react component

With this library it is possible to infer the props for a react component from the component registration object to builder. This means that you do not need to define the props for the react component as they can be inferred with a helper type.

You can use the helper type RegisterObjectToProps to get a type for the props of the react component based on the registration object for builder.

import { RegisterObjectToProps } from '@oak-digital/builder-helpers';
import { Component } from '@builder.io/sdk';
const registerObject = {
    inputs: [
        {
            name: 'text',
            type: 'string',
        },
    ];
} as const satisfies Component
type Props = RegisterObjectToProps<typeof registerObject>;

And then use Props as the props type for the component.

See the example below for how to make an existing component use this type helper.

Here is an example with before and after:

Before:

// counter.tsx
type Props = {
    text: string;
};

const Counter = (props: Props) => {
    const [count, setCount] = React.useState(0);
    return (
        <button onClick={() => setCount(count + 1)}>
            {props.text}: {count}
        </button>
    )
};
export default Counter;
// builder-registry.ts
import Counter from './counter';
Builder.registerComponent(Counter, {
    name: 'Counter',
    inputs: [
        {
            name: 'text',
            type: 'string',
        },
    ];
});

After:

Since the props that the component needs is tightly coupled to the data from builder, we will be defining the builder registration object in the react component file.

// counter.tsx
import { Component } from '@builder.io/sdk';
import { RegisterObjectToProps } from '@oak-digital/builder-helpers';

export const counterRegisterObject = {
    name: 'Counter',
    inputs: [
        {
            name: 'text',
            type: 'string',
        },
    ];
} as const satisfies Component;

type Props = RegisterObjectToProps<typeof counterRegisterObject>;

const Counter = (props: Props) => {
    const [count, setCount] = React.useState(0);
    return (
        <button onClick={() => setCount(count + 1)}>
            {props.text}: {count}
        </button>
    )
};
export default Counter;

NOTE as const is required to make the type inference working correctly.
NOTE satisfies Component gives you intelisense when writing the register object

// builder-registry.ts
import Counter, { counterRegisterObject } from './counter';
Builder.registerComponent(Counter, counterRegisterObject);

builder-helpers's People

Contributors

alexnortung avatar christofferoakdigital avatar

Watchers

 avatar

builder-helpers's Issues

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.