0.2.0 • Published 2 years ago

react-controlled-component-helpers v0.2.0

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

react-controlled-component-helpers

Helper functions to make React controlled components less verbose.

Instead of:

<input value={state} onChange={(e: EventTarget) => setState((e.target as HTMLInputElement).value)}/>

Use this:

import {getInputStateProps} from "react-controlled-component-helpers";

<input {...getInputStateProps(state, setState)}/>

The source code is straightforward if you want to check it out:

import {Dispatch, FormEvent, SetStateAction} from "react";

export const getSelectStateProps = (state: string, setState: Dispatch<SetStateAction<string>>) => ({
    value: state,
    onChange: (e: FormEvent<HTMLSelectElement>) => setState((e.target as HTMLSelectElement).value),
});

export const getInputStateProps = (state: string, setState: Dispatch<SetStateAction<string>>) => ({
    value: state,
    onChange: (e: FormEvent<HTMLInputElement>) => setState((e.target as HTMLInputElement).value),
});

export const getTextAreaStateProps = (state: string, setState: Dispatch<SetStateAction<string>>) => ({
    value: state,
    onChange: (e: FormEvent<HTMLTextAreaElement>) => setState((e.target as HTMLTextAreaElement).value),
});

This package only supports a few components at the moment. If you want to help add support for more, contributions are welcome through GitHub!

0.2.0

2 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago