Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

feat(Formbot): support for yup via validationSchema; Add Context provider and EasyInput consumer #20

Merged
merged 20 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"release": "standard-version && git push --follow-tags"
},
"dependencies": {
"lodash": "^4.17.11",
"mitt": "^1.1.3",
"polished": "^2.0.0",
"popper.js": "^1.14.7",
Expand Down Expand Up @@ -73,7 +72,8 @@
"rollup-plugin-commonjs": "^9.2.0",
"rollup-plugin-filesize": "^6.0.1",
"standard-version": "^5.0.0",
"styled-components": "^4.1.3"
"styled-components": "^4.1.3",
"yup": "^0.26.10"
},
"resolutions": {
"babel-core": "^7.0.0-bridge.0"
Expand Down
41 changes: 41 additions & 0 deletions src/Form/Fieldset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';
import Box from '../Box';
import styled, { css } from 'styled-components';
import { createComponent } from '../utils';

const Legend = createComponent({
name: 'Legend',
tag: 'legend',
style: ({
theme,
color = theme.colors.primary,
}) => css`
font-weight: 700;
margin-bottom: 1rem;
font-size: 10px;
text-transform: uppercase;
letter-spacing: 0.25px;
color: ${color};
`,
});

const Container = styled.fieldset`
border: 0;
padding: 0;
margin: 0;

* + & {
margin-top: 1.5rem;
}
`;

const Fieldset = ({ legend, children }) => (
<Container>
{legend && <Legend>{legend}</Legend>}

{children}
</Container>
);

export default Fieldset;

59 changes: 30 additions & 29 deletions src/Form/Formbot.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Select from './Select';
import Formbot from './Formbot';
import Button from '../Button';
import FormGroup from './FormGroup';
import Fieldset from './Fieldset';
import CheckboxGroup from './CheckboxGroup';
import RadioGroup from './RadioGroup';
import Switch from './Switch';
Expand Down Expand Up @@ -41,6 +42,11 @@ const radioValues = [
export default function() {
return (
<Formbot
initialValues={{
name: '',
email: '',
message: '',
}}
validations={{
name: val => {
if (val !== 'Bob') {
Expand Down Expand Up @@ -68,7 +74,7 @@ export default function() {
}}>
{({ values, onSubmit, onChange, errors, onBlur }) => (
<form onSubmit={onSubmit}>
<FormGroup>
<Fieldset legend="Some Fields">
<Input
onBlur={onBlur}
error={errors.name}
Expand All @@ -78,8 +84,6 @@ export default function() {
placeholder="Name"
label="Name"
/>
</FormGroup>
<FormGroup>
<Input
onBlur={onBlur}
error={errors.email}
Expand All @@ -89,8 +93,8 @@ export default function() {
placeholder="Email"
label="Email"
/>
</FormGroup>
<FormGroup>
</Fieldset>
<Fieldset legend="More Fields">
<Select
onBlur={onBlur}
error={errors.gender}
Expand All @@ -102,11 +106,10 @@ export default function() {
name="gender"
placeholder="Select a Gender"
/>
</FormGroup>
<FormGroup>
<Input
onBlur={onBlur}
onChange={onChange}
value={values.message}
error={errors.message}
name="message"
multiline
Expand All @@ -115,28 +118,26 @@ export default function() {
placeholder="Your Message"
label="Write a Message"
/>
</FormGroup>
<FormGroup>
<CheckboxGroup
error={errors.checkboxes}
value={values.checkboxes}
name="checkboxes"
id="checkboxes"
onChange={onChange}
choices={checkboxValues}
/>
</FormGroup>
<FormGroup>
<RadioGroup
error={errors.radioGroup}
name="radioGroup"
horizontal
value={values.radio}
id="radio"
onChange={onChange}
choices={radioValues}
/>
</FormGroup>
<FormGroup>
<CheckboxGroup
error={errors.checkboxes}
value={values.checkboxes}
name="checkboxes"
id="checkboxes"
onChange={onChange}
choices={checkboxValues}
/>
<RadioGroup
error={errors.radioGroup}
name="radioGroup"
horizontal
value={values.radio}
id="radio"
onChange={onChange}
choices={radioValues}
/>
</FormGroup>
</Fieldset>
<FormGroup>
<Switch name="switch1" id="switch1" onChange={onChange} toggled={values.switch1} />
</FormGroup>
Expand Down
38 changes: 29 additions & 9 deletions src/Form/Formbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ const VALIDATIONS = {
export default class Formbot extends React.Component {
static propTypes = {
initialValues: PropTypes.shape(),
validations: PropTypes.shape({
fieldName: PropTypes.oneOfType([PropTypes.func, PropTypes.shape()]),
}),
validations: PropTypes.object,
validationSchema: PropTypes.object,
onFocus: PropTypes.func,
onChange: PropTypes.func,
onBlur: PropTypes.func,
Expand All @@ -35,6 +34,7 @@ export default class Formbot extends React.Component {
static defaultProps = {
initialValues: {},
validations: {},
validationSchema: null,
onFocus() {},
onChange() {},
onBlur() {},
Expand All @@ -47,8 +47,12 @@ export default class Formbot extends React.Component {
errors: {},
};

get validatableFields() {
return Object.keys(this.props.validationSchema || this.props.validations || {});
}

get validatable() {
return Object.keys(this.props.validations);
return !!this.validatableFields.length;
}

get isValid() {
Expand Down Expand Up @@ -110,15 +114,27 @@ export default class Formbot extends React.Component {

validateField(field) {
return new Promise(resolve => {
const validation = this.props.validations[field];
const fieldState = this.state.fields[field] || {};
choochootrain marked this conversation as resolved.
Show resolved Hide resolved
if (fieldState.validated) {
resolve();
return;
}

if (!validation) return;
const fromSchema = !!this.props.validationSchema;
const validation = (fromSchema ? this.props.validationSchema : this.props.validations)[field];
choochootrain marked this conversation as resolved.
Show resolved Hide resolved

if (!validation) {
resolve();
return;
}

const fieldValue = this.state.values[field];
let errorMsg;

try {
if (typeof validation === 'function') {
if (fromSchema) {
choochootrain marked this conversation as resolved.
Show resolved Hide resolved
validation.validateSync(fieldValue, { values: this.state.values });
} else if (typeof validation === 'function') {
validation(fieldValue);
} else {
Object.keys(validation).forEach(method => {
Expand All @@ -132,7 +148,11 @@ export default class Formbot extends React.Component {
});
}
} catch (err) {
errorMsg = err.message;
if (fromSchema) {
choochootrain marked this conversation as resolved.
Show resolved Hide resolved
errorMsg = err.errors.length ? err.errors[0] : undefined;
} else {
errorMsg = err.message;
}
} finally {
this.updateField(field, { validated: true }).then(() => {
this.setState(
Expand All @@ -151,7 +171,7 @@ export default class Formbot extends React.Component {

validateAllFields() {
return Promise.all(
this.validatable.map(field => this.updateField(field, {}).then(() => this.validateField(field)))
this.validatableFields.map(field => this.updateField(field, {}).then(() => this.validateField(field)))
);
}

Expand Down
12 changes: 7 additions & 5 deletions src/Form/Formbot.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ name: Formbot
---

import { Playground, PropsTable } from 'docz'
import * as yup from 'yup';
import FormExample from './Formbot.example'
import Formbot from './Formbot'
import Button from '../Button'
Expand All @@ -22,11 +23,12 @@ Quickly build a form using a Formbot and pre-configured form components. Formbot

### Formbot
<Playground>
<Formbot
validations={{
name: {
minLength: 4,
},
<Formbot
initialValues={{
name: '',
}}
validationSchema={{
name: yup.string().required().min(4),
}}>
{({ values, onSubmit, onChange, errors, onBlur }) => (
<form onSubmit={onSubmit}>
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export Column from './Grid/Column';
export Container from './Grid/Container';
export Dropdown from './Dropdown';
export Field from './Form/Field';
export Fieldset from './Form/Fieldset';
choochootrain marked this conversation as resolved.
Show resolved Hide resolved
export Flex from './Flex';
export Formbot from './Form/Formbot';
export FormError from './Form/FormError';
Expand Down