0.0.2 • Published 6 years ago
@zecos/inputz v0.0.2
@zecos/inputz
@zecos/inputz is a library for quickly creating React UI components with little to no boilerplate.
Installation
yarn add @zecos/inputz
npm i -S @zecos/inputz
Example
// text.tsx
export const text = createInput(({helpers, state}) => {
const {
id,
name,
label,
value,
onChange,
onBlur,
} = helpers
const {touched, errors} = state
return (
<div>
<label className={styles.textLabel} htmlFor={name}>
{label}
{touched && errors[0].toString()}
</label>
<input
name={name}
aria-label={label}
value={value}
onChange={onChange}
onBlur={onblur
id={id}
/>
</div>
)
})// Form.tsx
import React from "react"
import { nameValidator } from "@zecos/validatorz"
import { text } from "./text"
export const Form = () => {
const [FirstName, firstNameState, firstNameActions] = text({
name: "firstName",
validate: nameValdiator,
})
return (
<form className="form">
<FirstName /><br />
First Name Value: {firstNameState.value}
</form>
)
}For full example, see @zecos/inputz-basic, or better yet, fork it and create your own UI!
How it works
createInput takes a functional component that takes an object with the following properties:
props: Properties actually passed to the component- in our example, it would look something like
<FirstName x="hello" />, andpropswould be{x: "hello"}
- in our example, it would look something like
state: the field state, which includesvalue: value of the fieldtouched: whether or not the user has focused and blurred the inputerrors: the errors returned by yourvalidatefunctionpristine: whether or not the field data has been manipulated
actions:getState: returns the same thing as1setValue: set the value of the field (also runs validation and sets pristine to false)reset: sets the field back to its original state (pristine, untouched, with the original init values)setTouched: set thetouchedvalue totrue
helpers: premade functions and properties to make your life easiertitle: the form name in title casecamel: the form name in camel casesnake: the form name in snake casearia-label: the form name in title case (for convenience)onChange: a function that sets the field value to the event's target valueonBlur: a function that sets the field'stouchedproperty totruevalue: value of the fieldlabel: the form name in title casename: the form name in snake case (for convenience)id: the form name in snake case (for convenience) state: IFieldzSingleState actions: ReactFieldzSingleActions
0.0.2
6 years ago