super-simple-react-form
v0.0.9
Published
A Dead Simple Form For React
Downloads
6
Readme
A Dead Simple Form For React
Motivation:
I feel the direction of form libraries in the market is like trying to bundle everything at once. The common direction of these libraries is to create isolated state inside the form, and provide API for developer to manipulate the isolated state.
Problems:
- A need to create custom component to work with the Form library.
- Problem to integrate another React's components on npm with the form's component.
- Components that has been created / modified is locked to certain Form library.
- Need to continuously learn API depends on the flavour of the library.
Then comes the idea:
- Why can't there be a form that just do validation and that it will work with any kind of component?
- Leave the rest of data manipulation to developer. This way we can easily cover infinite cases.
Concept:
- Simple Rules are better than many APIs
- User just need to provide 2 props:
value
,data-validators
- SimpleForm will check for these 2 props, and validate if these 2 props existed.
Example:
import { SimpleForm, validations } from 'super-simple-react-form';
const { required, minValue } = validations;
export default class Component extends React.Component {
constructor(props){
super(props);
this.state = {
input1: '',
};
}
render(){
return (
<SimpleForm onSubmit={this.handleSubmit.bind(this)}>
<div> {/* 1 */}
<input
value={this.state.input1} {/* 2 */}
data-validators={[required, minValue(10)]} {/* 3 */}
onChange={e => this.setState({input1: e.target.value})}
/>
</div>
<div>
<CustomInput
value='hello' {/* 2 */}
data-validators={[required]} {/* 3 */}
/>
</div>
<div errorElement={<CustomErrorElement/>}> {/* 4 */}
<CustomInput
value='hello' {/* 2 */}
data-validators={[required]} {/* 3 */}
/>
</div>
</SimpleForm>
);
}
handleSubmit(e, {isValids}){
e.preventDefault();
console.log(isValids);
}
};
const CustomInput = props => {
return <div>
<input {...props}/>
</div>
}
Rules:
1) Wrap the input element with div, SimpleForm will inject errorElement and render the errorMessages here.
2) Provide the value props.
3) Provide the validator functions to props data-validators
4) Please provide custom errorElement if you wish.
Validations
List of supported Validations:
- minLength(String)
- maxLength(String)
- minValue(Number)
- maxValue(Number)
- oneOfValue(array)
- preventScriptTag(String)
Future Features:
- validate onBlur
- mark the invalid input, as invalid attributes, like the usual HTML
<input invalid/>