0.1.5 • Published 6 years ago
react-form-context v0.1.5
react-form-context
Form validation and context library for React.
Usage
Making a FormContextAware component
- Your component should accept props for
isSubmittingandhasSubmitted(both boolean values).
const MyComponent = ({isSubmitting, hasSubmitted, ...rest}) => {
//Component render code
}- Then, just wrap your component in the
formContextAwareHOC.
import { formContextAware } from 'react-form-context';
// Other imports...
const MyComponent = ({isSubmitting, hasSubmitted, ...rest}) => {
//Component render code
}
export default formContextAware(MyComponent);Making a validated component
- Your component should accept props for
validationErrors(an array of strings) andisValidating(a boolean value).
const MyComponent = ({validationErrors, isValidating, ...rest}) => {
//Component render code
}- Then, just wrap your component in the
withValidationHOC.
import { withValidation } from 'react-form-context';
// Other imports...
const MyComponent = ({validationErrors, isValidating, ...rest}) => {
//Component render code
}
export default withValidation(MyComponent);Using wrapped components
- The included
Formelement creates the context required forwithValidationandformContextAware. - To use a component wrapped with one of these HOC's, just make sure they are nested within
<Form></Form.
import { Form } from 'react-form-context';
// Valid
<Form>
<MyComponent />
</Form>
//Invalid
<MyComponent />