@massds/mayflower-react
Version:
React versions of Mayflower design system UI components
106 lines (81 loc) • 3.59 kB
JavaScript
;
exports.__esModule = true;
exports["default"] = exports.FormProvider = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _context = require("../Input/context.js");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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; }
/* eslint-disable react/no-unused-state */
var Form = function Form(props) {
var formContext = _react["default"].useContext(_context.FormContext);
return /*#__PURE__*/_react["default"].createElement("form", {
className: "ma__form-page",
action: "#"
}, typeof props.children === 'function' && props.children(formContext));
};
Form.propTypes = process.env.NODE_ENV !== "production" ? {
children: _propTypes["default"].node
} : {};
var 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", function () {
var values = {};
Object.keys(_this.state.value).forEach(function (inputId) {
values[inputId] = _this.getValue(inputId);
});
return values;
});
_defineProperty(_assertThisInitialized(_this), "getValue", function (inputId) {
if (_this.hasId(inputId)) {
return _this.state.value[inputId].getValue();
}
return null;
});
_defineProperty(_assertThisInitialized(_this), "setValue", function (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", function (inputId) {
return Object.prototype.hasOwnProperty.call(_this.state.value, inputId);
});
_defineProperty(_assertThisInitialized(_this), "updateState", function (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["default"].createElement(_context.FormContext.Provider, {
value: this.state
}, this.props.children);
};
return FormProvider;
}(_react["default"].Component);
exports.FormProvider = FormProvider;
FormProvider.defaultProps = {
isActive: true
};
FormProvider.propTypes = process.env.NODE_ENV !== "production" ? {
/** Controls if child Input components should hook into FormContext or not */
isActive: _propTypes["default"].bool,
children: _propTypes["default"].node
} : {};
FormProvider.contextType = _context.FormContext;
var _default = Form;
exports["default"] = _default;