formsy-semantic-ui-react
Version:
Formsy-React wrappers for Semantic-Ui-React's form Components
485 lines (470 loc) • 18.6 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var Formsy = require('formsy-react');
var Formsy__default = _interopDefault(Formsy);
var hoistNonReactStatics = _interopDefault(require('hoist-non-react-statics'));
var React = require('react');
var React__default = _interopDefault(React);
var semanticUiReact = require('semantic-ui-react');
function _extends() {
_extends = Object.assign ? Object.assign.bind() : function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}
function _inheritsLoose(subClass, superClass) {
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
_setPrototypeOf(subClass, superClass);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function _objectWithoutPropertiesLoose(source, excluded) {
if (source == null) return {};
var target = {};
var sourceKeys = Object.keys(source);
var key, i;
for (i = 0; i < sourceKeys.length; i++) {
key = sourceKeys[i];
if (excluded.indexOf(key) >= 0) continue;
target[key] = source[key];
}
return target;
}
var removeProps = ['as', 'attachToForm', 'defaultChecked', 'defaultSelected', 'defaultValue', 'detachFromForm', 'error', 'errorLabel', 'errorMessage', 'errorMessages', 'formRadioGroup', 'hasValue', 'inline', 'innerRef', 'inputAs', 'inputClassName', 'instantValidation', 'isFormDisabled', 'isFormSubmitted', 'isPristine', 'isRequired', 'isValid', 'isValidValue', 'label', 'passRequiredToField', 'resetValue', 'rootClassName', 'rootElement', 'rootStyle', 'runValidation', 'setValidations', 'setValue', 'showError', 'showRequired', 'validate', 'validationError', 'validationErrors', 'validations', 'value', 'width'];
function filterSuirElementProps(props) {
return Object.fromEntries(Object.keys(props).filter(function (prop) {
return !removeProps.includes(prop);
}).map(function (prop) {
return [prop, props[prop]];
}));
}
var FormsyCheckbox = /*#__PURE__*/function (_Component) {
_inheritsLoose(FormsyCheckbox, _Component);
function FormsyCheckbox() {
var _this;
_this = _Component.apply(this, arguments) || this;
_this.handleChange = function (e, data) {
var checked = data.checked;
_this.props.setValue(checked);
if (_this.props.onChange) {
_this.props.onChange(e, data);
}
};
return _this;
}
var _proto = FormsyCheckbox.prototype;
_proto.componentDidMount = function componentDidMount() {
var _this$props = this.props,
defaultChecked = _this$props.defaultChecked,
setValue = _this$props.setValue;
setValue(!!defaultChecked, false);
};
_proto.render = function render() {
var _this$props2 = this.props,
_this$props2$inputAs = _this$props2.inputAs,
inputAs = _this$props2$inputAs === void 0 ? semanticUiReact.Checkbox : _this$props2$inputAs,
required = _this$props2.required,
isValid = _this$props2.isValid,
isPristine = _this$props2.isPristine,
errorLabel = _this$props2.errorLabel,
errorMessage = _this$props2.errorMessage,
value = _this$props2.value,
as = _this$props2.as,
width = _this$props2.width,
className = _this$props2.className,
disabled = _this$props2.disabled,
inline = _this$props2.inline,
passRequiredToField = _this$props2.passRequiredToField;
var error = !isPristine && !isValid;
var checkboxProps = _extends({}, filterSuirElementProps(this.props), {
label: this.props.label,
checked: !!value,
onChange: this.handleChange
});
if (inputAs === semanticUiReact.Checkbox || inputAs === semanticUiReact.Radio) {
checkboxProps.error = undefined;
}
return React__default.createElement(semanticUiReact.Form.Field, {
as: as,
className: className,
required: required && passRequiredToField,
error: error,
width: width,
inline: inline,
disabled: disabled
}, React.createElement(inputAs, _extends({}, checkboxProps)), error && errorLabel && React.cloneElement(errorLabel, {}, errorMessage));
};
return FormsyCheckbox;
}(React.Component);
var Checkbox = /*#__PURE__*/Formsy.withFormsy(FormsyCheckbox);
var FormsyDropdown = /*#__PURE__*/function (_Component) {
_inheritsLoose(FormsyDropdown, _Component);
function FormsyDropdown() {
var _this;
_this = _Component.apply(this, arguments) || this;
_this.state = {
allowError: false
};
_this.handleChange = function (e, data) {
var _this$props = _this.props,
multiple = _this$props.multiple,
value = _this$props.value,
setValue = _this$props.setValue,
onChange = _this$props.onChange,
name = _this$props.name;
if (multiple && Array.isArray(value) && Array.isArray(data.value) && value.length > data.value.length) {
_this.showError();
}
setValue(data.value);
if (onChange) {
onChange(e, _extends({}, data, {
name: name
}));
}
};
_this.handleBlur = function (e, data) {
var onBlur = _this.props.onBlur;
if (onBlur) {
onBlur(e, data);
}
};
_this.handleClose = function () {
return _this.showError();
};
_this.showError = function () {
return _this.setState({
allowError: true
});
};
return _this;
}
var _proto = FormsyDropdown.prototype;
_proto.componentDidMount = function componentDidMount() {
var _this$props2 = this.props,
defaultValue = _this$props2.defaultValue,
setValue = _this$props2.setValue;
if (defaultValue) setValue(defaultValue);
};
_proto.componentDidUpdate = function componentDidUpdate(prevProps) {
if (prevProps.isFormSubmitted !== this.props.isFormSubmitted && this.props.isFormSubmitted) {
this.showError();
}
};
_proto.render = function render() {
var _this$props3 = this.props,
_this$props3$inputAs = _this$props3.inputAs,
inputAs = _this$props3$inputAs === void 0 ? semanticUiReact.Dropdown : _this$props3$inputAs,
id = _this$props3.id,
required = _this$props3.required,
label = _this$props3.label,
value = _this$props3.value,
defaultValue = _this$props3.defaultValue,
multiple = _this$props3.multiple,
errorLabel = _this$props3.errorLabel,
errorMessage = _this$props3.errorMessage,
isValid = _this$props3.isValid,
isPristine = _this$props3.isPristine,
as = _this$props3.as,
width = _this$props3.width,
className = _this$props3.className,
disabled = _this$props3.disabled,
inline = _this$props3.inline,
_this$props3$passRequ = _this$props3.passRequiredToField,
passRequiredToField = _this$props3$passRequ === void 0 ? true : _this$props3$passRequ;
var shortHandMode = inputAs === semanticUiReact.Form.Dropdown || inputAs === semanticUiReact.Form.Select;
var error = !isPristine && !isValid && this.state.allowError;
var dropdownProps = _extends({}, filterSuirElementProps(this.props), {
onChange: this.handleChange,
onBlur: this.handleBlur,
onClose: this.handleClose,
value: value || defaultValue || multiple && [] || '',
error: !disabled && error,
id: id,
name: undefined
});
var dropdownNode = shortHandMode ? React.createElement(inputAs, dropdownProps).props.control : inputAs;
return React__default.createElement(semanticUiReact.Form.Field, {
as: as,
className: className,
required: required && passRequiredToField,
error: !disabled && error,
width: width,
inline: inline,
disabled: disabled
}, shortHandMode && label && React__default.createElement("label", {
htmlFor: id
}, label), React.createElement(dropdownNode, _extends({}, dropdownProps)), error && errorLabel && React.cloneElement(errorLabel, {}, errorMessage));
};
return FormsyDropdown;
}(React.Component);
var FormsyDropdown$1 = /*#__PURE__*/Formsy.withFormsy(FormsyDropdown);
var FormsyInput = /*#__PURE__*/function (_Component) {
_inheritsLoose(FormsyInput, _Component);
function FormsyInput() {
var _this;
_this = _Component.apply(this, arguments) || this;
_this.state = {
allowError: false
};
_this.handleChange = function (e, data) {
var value = data.value;
_this.props.setValue(value);
if (_this.props.onChange) _this.props.onChange(e, data);
if (_this.props.instantValidation) _this.showError();
};
_this.handleBlur = function (e, data) {
_this.showError();
if (_this.props.onBlur) _this.props.onBlur(e, data);
};
_this.showError = function () {
return _this.setState({
allowError: true
});
};
return _this;
}
var _proto = FormsyInput.prototype;
_proto.componentDidMount = function componentDidMount() {
var _this$props = this.props,
defaultValue = _this$props.defaultValue,
setValue = _this$props.setValue;
if (defaultValue) setValue(defaultValue);
};
_proto.componentDidUpdate = function componentDidUpdate(prevProps) {
if (prevProps.isFormSubmitted !== this.props.isFormSubmitted && this.props.isFormSubmitted) {
this.showError();
}
};
_proto.render = function render() {
var _this$props2 = this.props,
id = _this$props2.id,
_this$props2$inputAs = _this$props2.inputAs,
inputAs = _this$props2$inputAs === void 0 ? semanticUiReact.Input : _this$props2$inputAs,
inputClassName = _this$props2.inputClassName,
required = _this$props2.required,
label = _this$props2.label,
defaultValue = _this$props2.defaultValue,
value = _this$props2.value,
isValid = _this$props2.isValid,
isPristine = _this$props2.isPristine,
errorMessage = _this$props2.errorMessage,
errorLabel = _this$props2.errorLabel,
as = _this$props2.as,
width = _this$props2.width,
className = _this$props2.className,
disabled = _this$props2.disabled,
inline = _this$props2.inline,
_this$props2$passRequ = _this$props2.passRequiredToField,
passRequiredToField = _this$props2$passRequ === void 0 ? true : _this$props2$passRequ;
var allowError = this.state.allowError;
var error = !isPristine && !isValid && allowError;
var inputProps = _extends({}, filterSuirElementProps(this.props), {
value: value || isPristine && defaultValue || '',
onChange: this.handleChange,
onBlur: this.handleBlur,
className: inputClassName,
error: !disabled && error,
label: label,
id: id
});
var isFormField = inputAs === semanticUiReact.Form.Input || inputAs === semanticUiReact.Form.TextArea;
var inputNode = isFormField ? React.createElement(inputAs).props.control : inputAs;
if (isFormField) {
inputProps.label = undefined;
if (inputAs === semanticUiReact.Form.TextArea) {
inputProps.error = undefined;
}
}
var inputElement = !isFormField && React.isValidElement(inputAs) ? React.cloneElement(inputAs, _extends({}, inputProps, inputAs.props)) : React.createElement(inputNode, _extends({}, inputProps));
var shouldShowFormLabel = isFormField || React.isValidElement(inputAs);
return React__default.createElement(semanticUiReact.Form.Field, {
as: as,
className: className,
required: required && passRequiredToField,
error: !disabled && error,
width: width,
inline: inline,
disabled: disabled
}, shouldShowFormLabel && label && React__default.createElement("label", {
htmlFor: id
}, label), inputElement, !disabled && error && errorLabel && React.cloneElement(errorLabel, {}, errorMessage));
};
return FormsyInput;
}(React.Component);
var FormsyInput$1 = /*#__PURE__*/Formsy.withFormsy(FormsyInput);
var FormsyRadioGroup = /*#__PURE__*/function (_Component) {
_inheritsLoose(FormsyRadioGroup, _Component);
function FormsyRadioGroup() {
var _this;
_this = _Component.apply(this, arguments) || this;
_this.handleChange = function (e, data) {
var value = data.value;
_this.props.setValue(value);
if (_this.props.onChange) {
_this.props.onChange(e, data);
}
};
return _this;
}
var _proto = FormsyRadioGroup.prototype;
_proto.componentDidMount = function componentDidMount() {
var _this$props = this.props,
defaultSelected = _this$props.defaultSelected,
setValue = _this$props.setValue;
if (defaultSelected) setValue(defaultSelected);
};
_proto.render = function render() {
var _this2 = this;
var _this$props2 = this.props,
as = _this$props2.as,
label = _this$props2.label,
required = _this$props2.required,
children = _this$props2.children,
name = _this$props2.name,
value = _this$props2.value,
errorLabel = _this$props2.errorLabel,
isValid = _this$props2.isValid,
isPristine = _this$props2.isPristine,
errorMessage = _this$props2.errorMessage,
_this$props2$passRequ = _this$props2.passRequiredToField,
passRequiredToField = _this$props2$passRequ === void 0 ? true : _this$props2$passRequ,
disabled = _this$props2.disabled,
className = _this$props2.className,
unstackable = _this$props2.unstackable,
_this$props2$inline = _this$props2.inline,
inline = _this$props2$inline === void 0 ? true : _this$props2$inline,
width = _this$props2.width;
var error = !isPristine && !isValid;
var formGroupProps = {
as: as,
className: className,
unstackable: unstackable,
inline: inline,
grouped: !inline
};
var labelProps = {
required: required && passRequiredToField,
error: !disabled && error,
label: label,
disabled: disabled
};
var fieldProps = {
width: width,
error: !disabled && error
};
return React__default.createElement(semanticUiReact.Form.Group, Object.assign({}, formGroupProps), label && React__default.createElement(semanticUiReact.Form.Field, Object.assign({}, labelProps)), React.Children.map(children, function (radio) {
if (!radio) {
return null;
}
var props = {
name: name,
checked: value === radio.props.value,
onChange: _this2.handleChange,
disabled: disabled
};
return React__default.createElement(semanticUiReact.Form.Field, Object.assign({}, fieldProps), React.cloneElement(radio, _extends({}, props)));
}), error && errorLabel && React.cloneElement(errorLabel, {}, errorMessage));
};
return FormsyRadioGroup;
}(React.Component);
var FormsyRadioGroup$1 = /*#__PURE__*/Formsy.withFormsy(FormsyRadioGroup);
var FormsySelect = (function (props) {
return React__default.createElement(FormsyDropdown$1, Object.assign({
inputAs: semanticUiReact.Select
}, props));
});
var FormsyTextArea = (function (props) {
return React__default.createElement(FormsyInput$1, Object.assign({
inputAs: semanticUiReact.TextArea
}, props));
});
var _excluded = ["mapping", "validationErrors", "onValid", "onValidSubmit", "onInvalid", "onInvalidSubmit", "onChange", "preventExternalInvalidation", "onError", "onSubmit", "forwardedRef"],
_excluded2 = ["as", "error", "inverted", "loading", "reply", "size", "success", "warning", "widths", "forwardedRef", "className"];
var Form = /*#__PURE__*/function (_Component) {
_inheritsLoose(Form, _Component);
function Form() {
return _Component.apply(this, arguments) || this;
}
var _proto = Form.prototype;
_proto.render = function render() {
var children = this.props.children;
var _this$props = this.props,
onSubmit = _this$props.onSubmit,
forwardedRef = _this$props.forwardedRef,
nonFormsyReactFormProps = _objectWithoutPropertiesLoose(_this$props, _excluded);
var _this$props2 = this.props,
_this$props2$as = _this$props2.as,
as = _this$props2$as === void 0 ? 'div' : _this$props2$as,
nonSemanticUIFormProps = _objectWithoutPropertiesLoose(_this$props2, _excluded2);
return React__default.createElement(Formsy__default, Object.assign({
noValidate: true,
ref: forwardedRef,
onSubmit: onSubmit
}, nonSemanticUIFormProps), React__default.createElement(semanticUiReact.Form, Object.assign({
as: as
}, nonFormsyReactFormProps), children));
};
return Form;
}(React.Component);
Form.Button = semanticUiReact.Form.Button;
Form.Radio = semanticUiReact.Form.Radio;
Form.Field = semanticUiReact.Form.Field;
Form.Group = semanticUiReact.Form.Group;
Form.Checkbox = Checkbox;
Form.Input = function (props) {
return React__default.createElement(FormsyInput$1, Object.assign({
inputAs: semanticUiReact.Form.Input
}, props));
};
Form.TextArea = function (props) {
return React__default.createElement(FormsyTextArea, Object.assign({
inputAs: semanticUiReact.Form.TextArea
}, props));
};
Form.Select = function (props) {
return React__default.createElement(FormsySelect, Object.assign({
inputAs: semanticUiReact.Form.Select
}, props));
};
Form.RadioGroup = function (props) {
return React__default.createElement(FormsyRadioGroup$1, Object.assign({}, props));
};
Form.Dropdown = function (props) {
return React__default.createElement(FormsyDropdown$1, Object.assign({
inputAs: semanticUiReact.Form.Dropdown
}, props));
};
var Form$1 = /*#__PURE__*/hoistNonReactStatics( /*#__PURE__*/React__default.forwardRef(function (props, ref) {
return React__default.createElement(Form, Object.assign({}, props, {
forwardedRef: ref
}));
}), Form);
var FormsyRadio = (function (props) {
return React__default.createElement(Checkbox, Object.assign({
inputAs: semanticUiReact.Radio
}, props));
});
exports.Checkbox = Checkbox;
exports.Dropdown = FormsyDropdown$1;
exports.Form = Form$1;
exports.Input = FormsyInput$1;
exports.Radio = FormsyRadio;
exports.RadioGroup = FormsyRadioGroup$1;
exports.Select = FormsySelect;
exports.TextArea = FormsyTextArea;
//# sourceMappingURL=formsy-semantic-ui-react.cjs.development.js.map