@6thquake/react-material
Version:
React components that implement Google's Material Design.
70 lines (55 loc) • 2.24 kB
JavaScript
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _extends from "@babel/runtime/helpers/extends";
import React, { Component } from 'react';
import FormItem from './FormItem';
const convertValidationsToObject = validations => {
if (typeof validations === 'string') {
return validations.split(/,(?![^{[]*[}\]])/g).reduce((validationsAccumulator, validation) => {
let args = validation.split(':');
const validateMethod = args.shift();
args = args.map(arg => {
try {
return JSON.parse(arg);
} catch (e) {
return arg; // It is a string if it can not parse it
}
});
if (args.length > 1) {
throw new Error('Formsy does not support multiple args on string validations. Use object format of validations instead.');
} // Avoid parameter reassignment
const validationsAccumulatorCopy = _extends({}, validationsAccumulator);
validationsAccumulatorCopy[validateMethod] = args.length ? args[0] : true;
return validationsAccumulatorCopy;
}, {});
}
return validations || {};
};
function withFormItem(WrappedComponent) {
class FormItemComponent extends Component {
render() {
const _this$props = this.props,
{
label,
InputLabelProps
} = _this$props,
rest = _objectWithoutPropertiesLoose(_this$props, ["label", "InputLabelProps"]);
let required = false;
if (this.props.required || convertValidationsToObject(this.props.validations).isRequired) {
required = true;
}
const others = {};
if (!InputLabelProps || InputLabelProps.direction === 'standard') {
others.label = label;
}
return React.createElement(FormItem, {
label: label,
required: required,
InputLabelProps: InputLabelProps
}, React.createElement(WrappedComponent, _extends({}, rest, others)));
}
}
const name = WrappedComponent.displayName || WrappedComponent.name || (typeof WrappedComponent === 'string' ? WrappedComponent : 'Component');
FormItemComponent.displayName = `formItemHOC-${name}`;
return FormItemComponent;
}
export default withFormItem;