Giter Site home page Giter Site logo

r-u-lazy's Introduction

R-u-lazy

A React Framework for lazy people.

There are other projects that help you out build smarter forms such as Formik and React Final Form

Install

# with npm
npm install r-u-lazy

# with yarn
yarn add r-u-lazy

How to use

import React from 'react';
import {FormElementBuilder, FormBuilder} from 'r-u-lazy';

// Create the object that is passed form form to every component

interface FormExtraProps {
    disabled?: boolean;
}

// Create your own lazy elements
// Start by defining the properties of the element
interface ButtonProps {
    label: string;
    disabled: boolean;
}

// Then Make it lazy
// This function will return the elmeent you can place in the Element field and
// an interceptor that you will use in the creation of the form
export let { Element: Button, Interceptor: ButtonInterceptor } = FormElementBuilder<ButtonProps, FormExtraProps, 'button'>(
    // Give the element an internal name
    'button',
    // Then actualy create the component it recives it's owns properties form the props 
    // part of the object and the form properties form the formProps part of the object
    ({ props: { label, disabled }, formProps }) => {
        return (
            <button disabled={disabled ?? formProps.extra.disabled}>
                {label === 'foo' ? 'bar' : label}
            </button>
        );
    }
);
// Now you have a button that only works insides Forms and that if the label is 'foo' it will display 'bar' and display anything else 

// Create Properties for the input filed
export interface InputProps {
    disabled?: boolean;
    value?: string;
    onChange?: React.ChangeEventHandler<HTMLInputElement>;
}

// Because this field needs access to the data we use the FormElementDataBuilder so that we can get access to the data property which will give us access to the field in the object with the name of d form the new object that is put in the form
export let { Element: Input, Interceptor: InputInterceptor } =
    FormElementDataBuilder<
        InputProps,
        unknown,
        FormExtraProps,
        //Change this for the type of the changing function you decided
        // You can also use this type for a generic function that returns a void
        // UnknownFunction<void, unknown>,
        // You can also use a function that that is provided with this package
        // that has some nice functionality 
        DefaultSetFunctionType,
        'input'
    >(
        'input',
        ({
            props: { value, onChange, disabled },
            formProps,
            // Data form the object that is put on the form 
            data,
            // setData form the function that is put on the form 
            setData,
        }) => {
            return (
                <input
                    value={value ?? data ?? ''}
                    disabled={disabled ?? formProps.extra.disabled}
                    onChange={e => {
                        if (onChange) {
                            onChange(e);
                            return;
                        }
                        if (setData) {
                            setData({
                                value: e.target.value,
                                originalValue: e,
                            });
                            return;
                        }
                    }}
                />
            );
        }
    );

// Create your Form and Div elements 
// Since we want the button and the input to work in this form we add the interceptor to the interceptor list
export const { Form, Div } = FormBuilder<FormExtraProps, DefaultSetFunctionType, unknown>(
    [ButtonInterceptor, InputInterceptor]
);

// This is a form example

let FormExample = () => {
    let { obj, setObj } = useDataObject({ name: '', age: 0 });
    return (
        <Form obj={obj} setObj={setObj}>
            <Input d="name" />
        </Form>
    );
};

TODOs

  • Improve Documentation
  • Add some default fields
  • Complete the button field
  • Complete the input field

r-u-lazy's People

Contributors

andr3h3nriqu3s11 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.