Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to move to create-react-context #423

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ compiled
coverage
.DS_Store
next.d.ts
legacy.d.ts
legacy.d.ts
.rpt2_cache
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@
"gen-docs": "all-contributors generate && doctoc README.md"
},
"dependencies": {
"create-react-context": "^0.1.6",
"lodash.clonedeep": "^4.5.0",
"lodash.isequal": "4.5.0",
"lodash.topath": "4.5.2",
"prop-types": "^15.5.10",
"prop-types": "^15.6.0",
"warning": "^3.0.0"
},
"peerDependencies": {
Expand All @@ -51,7 +52,6 @@
"@types/lodash.clonedeep": "^4.5.3",
"@types/lodash.isequal": "4.5.2",
"@types/lodash.topath": "4.5.3",
"@types/prop-types": "15.5.1",
"@types/react": "16.0.28",
"@types/react-dom": "^16.0.3",
"@types/react-test-renderer": "15.5.2",
Expand Down
38 changes: 7 additions & 31 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ import commonjs from 'rollup-plugin-commonjs';
import filesize from 'rollup-plugin-filesize';
import replace from 'rollup-plugin-replace';
import resolve from 'rollup-plugin-node-resolve';
import sourceMaps from 'rollup-plugin-sourcemaps';

import uglify from 'rollup-plugin-uglify';

const shared = {
input: `compiled/formik.js`,
external: ['react', 'react-native'],
};

const prod = process.env.NODE_ENV === 'production';

export default [
Object.assign({}, shared, {
output: {
name: 'Formik',
format: 'umd',
sourcemap: true,
file:
process.env.NODE_ENV === 'production'
? './dist/formik.umd.min.js'
: './dist/formik.umd.js',
file: prod ? './dist/formik.umd.min.js' : './dist/formik.umd.js',
exports: 'named',
globals: {
react: 'React',
Expand All @@ -37,21 +36,10 @@ export default [
}),
commonjs({
include: /node_modules/,
namedExports: {
'node_modules/prop-types/index.js': [
'object',
'oneOfType',
'string',
'node',
'func',
'bool',
'element',
],
},
}),
sourceMaps(),
process.env.NODE_ENV === 'production' && filesize(),
process.env.NODE_ENV === 'production' &&

prod && filesize(),
prod &&
uglify({
output: { comments: false },
compress: {
Expand Down Expand Up @@ -92,19 +80,7 @@ export default [
resolve(),
commonjs({
include: /node_modules/,
namedExports: {
'node_modules/prop-types/index.js': [
'object',
'oneOfType',
'string',
'node',
'func',
'bool',
'element',
],
},
}),
sourceMaps(),
],
}),
];
41 changes: 14 additions & 27 deletions src/Field.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import * as PropTypes from 'prop-types';
import * as React from 'react';
import { getIn, isPromise } from './utils';

import { FormikProps } from './formik';
import { isFunction, isEmptyChildren } from './utils';
import warning from 'warning';
import { FormikProps } from './types';
import { getIn, isEmptyChildren, isFunction, isPromise } from './utils';
import { connect } from './connect';

export type GenericFieldHTMLAttributes =
| React.InputHTMLAttributes<HTMLInputElement>
Expand Down Expand Up @@ -86,23 +84,10 @@ export type FieldAttributes = GenericFieldHTMLAttributes & FieldConfig;
* Custom Field component for quickly hooking into Formik
* context and wiring up forms.
*/

export class Field<Props extends FieldAttributes = any> extends React.Component<
Props,
{}
> {
static contextTypes = {
formik: PropTypes.object,
};

static propTypes = {
name: PropTypes.string.isRequired,
component: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
render: PropTypes.func,
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
validate: PropTypes.func,
};

class FieldWithContext<
Props extends FieldAttributes = any,
Values = {}
> extends React.Component<Props & { formik: FormikProps<Values> }, {}> {
componentWillMount() {
const { render, children, component } = this.props;

Expand All @@ -123,23 +108,23 @@ export class Field<Props extends FieldAttributes = any> extends React.Component<
}

handleChange = (e: React.ChangeEvent<any>) => {
const { handleChange, validateOnChange } = this.context.formik;
const { handleChange, validateOnChange } = this.props.formik;
handleChange(e); // Call Formik's handleChange no matter what
if (!!validateOnChange && !!this.props.validate) {
this.runFieldValidations(e.target.value);
}
};

handleBlur = (e: any) => {
const { handleBlur, validateOnBlur } = this.context.formik;
const { handleBlur, validateOnBlur } = this.props.formik;
handleBlur(e); // Call Formik's handleBlur no matter what
if (validateOnBlur && this.props.validate) {
this.runFieldValidations(e.target.value);
}
};

runFieldValidations = (value: any) => {
const { setFieldError } = this.context.formik;
const { setFieldError } = this.props.formik;
const { name, validate } = this.props;
// Call validate fn
const maybePromise = (validate as any)(value);
Expand All @@ -162,10 +147,10 @@ export class Field<Props extends FieldAttributes = any> extends React.Component<
render,
children,
component = 'input',
formik,
...props
} = this.props as FieldConfig;
} = this.props as FieldConfig & { formik: FormikProps<Values> };

const { formik } = this.context;
const field = {
value:
props.type === 'radio' || props.type === 'checkbox'
Expand Down Expand Up @@ -200,3 +185,5 @@ export class Field<Props extends FieldAttributes = any> extends React.Component<
});
}
}

export const Field = connect<FieldAttributes, any>(FieldWithContext);
23 changes: 12 additions & 11 deletions src/FieldArray.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as PropTypes from 'prop-types';
import * as React from 'react';
import { FormikProps, FormikState, isFunction } from './formik';
import { isEmptyChildren, getIn, setIn } from './utils';
import { SharedRenderProps } from './types';
import { isFunction, isEmptyChildren, getIn, setIn } from './utils';
import { SharedRenderProps, FormikProps, FormikState } from './types';
import { connect } from './connect';

export type FieldArrayConfig = {
/** Really the path to the array field to be updated */
Expand Down Expand Up @@ -53,15 +52,15 @@ export const insert = (array: any[], index: number, value: any) => {
return copy;
};

export class FieldArray extends React.Component<FieldArrayConfig, {}> {
class FieldArrayWithContext<Values = {}> extends React.Component<
FieldArrayConfig & { formik: FormikProps<Values> },
{}
> {
static defaultProps = {
validateOnChange: true,
};
static contextTypes = {
formik: PropTypes.object,
};

constructor(props: FieldArrayConfig) {
constructor(props: FieldArrayConfig & { formik: FormikProps<Values> }) {
super(props);
// We need TypeScript generics on these, so we'll bind them in the constructor
this.remove = this.remove.bind(this);
Expand All @@ -79,7 +78,7 @@ export class FieldArray extends React.Component<FieldArrayConfig, {}> {
values,
touched,
errors,
} = this.context.formik;
} = this.props.formik;
const { name, validateOnChange } = this.props;
setFormikState(
(prevState: FormikState<any>) => ({
Expand Down Expand Up @@ -194,7 +193,7 @@ export class FieldArray extends React.Component<FieldArrayConfig, {}> {
};

const { component, render, children, name } = this.props;
const props = { ...arrayHelpers, form: this.context.formik, name };
const props = { ...arrayHelpers, form: this.props.formik, name };

return component
? React.createElement(component as any, props)
Expand All @@ -207,3 +206,5 @@ export class FieldArray extends React.Component<FieldArrayConfig, {}> {
: null;
}
}

export const FieldArray = connect<FieldArrayConfig, any>(FieldArrayWithContext);
12 changes: 4 additions & 8 deletions src/Form.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import * as PropTypes from 'prop-types';
import * as React from 'react';
import { connect } from './connect';

export const Form: React.SFC<any> = (props, context) => (
<form onSubmit={context.formik.handleSubmit} {...props} />
);

Form.contextTypes = {
formik: PropTypes.object,
};
export const Form = connect<any>(({ formik: { handleSubmit }, ...props }) => (
<form onSubmit={handleSubmit} {...props} />
));
19 changes: 19 additions & 0 deletions src/connect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as React from 'react';
import createContext from 'create-react-context';
import { FormikProps } from './types';

export const FormikContext = createContext<FormikProps<any>>({} as any);

/**
* Connect any component to Formik context, and inject as a prop called `formik`;
* @param Comp React Component
*/
export function connect<Outer, Values = {}>(
Comp: React.ComponentType<Outer & { formik: FormikProps<Values> }>
) {
return (props: Outer) => (
<FormikContext.Consumer>
{formik => <Comp {...props} formik={formik} />}
</FormikContext.Consumer>
);
}
Loading