diff --git a/src/Form.jsx b/src/Form.jsx index f63a182..ccf06aa 100644 --- a/src/Form.jsx +++ b/src/Form.jsx @@ -1,7 +1,8 @@ -import { PureComponent, Component } from 'react'; +import React, { PureComponent, Component } from 'react'; import PropTypes from 'prop-types'; import { noop, buildFormValidator, buildHandlersCache } from './utils'; +import { FormContext } from './context'; import update from 'update-js'; import get from 'lodash.get'; @@ -31,6 +32,13 @@ export default class Form extends (PureComponent || Component) { state = { errors: {} }; validator = buildFormValidator(this); _handlersCache = buildHandlersCache(); + contextValue = { + get: this.get, + set: this.set, + getError: this.getError, + getErrors: this.getErrors, + // ... and other "public" methods + }; componentWillReceiveProps() { if (this._nextErrors) { @@ -263,11 +271,11 @@ export default class Form extends (PureComponent || Component) { const $bound = this._bind$(); const { children: renderer } = this.props; - if (typeof renderer === 'function') { - return renderer($bound); - } - - return this.$render($bound); + return ( + + {typeof renderer === 'function' ? renderer($bound) : this.$render($bound)} + + ); } _bind$() { diff --git a/src/context.js b/src/context.js new file mode 100644 index 0000000..c2f4602 --- /dev/null +++ b/src/context.js @@ -0,0 +1,5 @@ +import { createContext, useContext } from 'react'; + +export const FormContext = createContext({}); + +export const useFormContext = () => useContext(FormContext); \ No newline at end of file diff --git a/src/index.js b/src/index.js index 367fd61..e894d27 100644 --- a/src/index.js +++ b/src/index.js @@ -1,2 +1,3 @@ export default from './Form'; export { bindState } from './utils'; +export * from './context';