UNPKG

@massds/mayflower-react

Version:

React versions of Mayflower design system UI components

99 lines (80 loc) 3.32 kB
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /** * Form module. * @module @massds/mayflower-react/Form * @requires module:@massds/mayflower-assets/scss/01-atoms/forms * @requires module:@massds/mayflower-assets/scss/04-templates/form-page */ import React from "react"; import PropTypes from "prop-types"; import { FormContext } from "../Input/context.mjs"; /* eslint-disable react/no-unused-state */ const Form = props => { const formContext = React.useContext(FormContext); return /*#__PURE__*/React.createElement("form", { className: "ma__form-page", action: "#" }, typeof props.children === 'function' && props.children(formContext)); }; Form.propTypes = process.env.NODE_ENV !== "production" ? { children: PropTypes.node } : {}; let FormProvider = /*#__PURE__*/function (_React$Component) { _inheritsLoose(FormProvider, _React$Component); function FormProvider(props) { var _this; _this = _React$Component.call(this, props) || this; _defineProperty(_assertThisInitialized(_this), "getValues", () => { const values = {}; Object.keys(_this.state.value).forEach(inputId => { values[inputId] = _this.getValue(inputId); }); return values; }); _defineProperty(_assertThisInitialized(_this), "getValue", inputId => { if (_this.hasId(inputId)) { return _this.state.value[inputId].getValue(); } return null; }); _defineProperty(_assertThisInitialized(_this), "setValue", (input, afterUpdate) => { if (Object.prototype.hasOwnProperty.call(_this.state.value, input.id)) { _this.state.value[input.id].setValue(input.value, afterUpdate); } }); _defineProperty(_assertThisInitialized(_this), "hasId", inputId => Object.prototype.hasOwnProperty.call(_this.state.value, inputId)); _defineProperty(_assertThisInitialized(_this), "updateState", newState => { _this.setState(newState); }); _this.state = { isActive: _this.props.isActive, value: {}, getValue: _this.getValue, hasId: _this.hasId, setValue: _this.setValue, updateState: _this.updateState, getValues: _this.getValues }; return _this; } var _proto = FormProvider.prototype; _proto.render = function render() { return /*#__PURE__*/React.createElement(FormContext.Provider, { value: this.state }, this.props.children); }; return FormProvider; }(React.Component); FormProvider.defaultProps = { isActive: true }; FormProvider.propTypes = process.env.NODE_ENV !== "production" ? { /** Controls if child Input components should hook into FormContext or not */ isActive: PropTypes.bool, children: PropTypes.node } : {}; FormProvider.contextType = FormContext; export { FormProvider }; export default Form;