vs-form-mui-4x
Version:
A schema-based form generator component for React using material-ui 4.x
105 lines (102 loc) • 4.15 kB
JavaScript
import { Component, createElement, createRef } from 'react';
import 'lodash/capitalize';
import 'lodash/cloneDeep';
import 'lodash/get';
import 'lodash/has';
import 'lodash/isArray';
import 'lodash/isDate';
import 'lodash/isBoolean';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import 'lodash/isInteger';
import isNull from 'lodash/isNull';
import 'lodash/isNumber';
import 'lodash/isObject';
import 'lodash/isPlainObject';
import 'lodash/isRegExp';
import 'lodash/isString';
import isUndefined from 'lodash/isUndefined';
import 'lodash/merge';
import 'lodash/set';
import 'lodash/toInteger';
import toNumber from 'lodash/toNumber';
import 'lodash/trimEnd';
import 'lodash/uniq';
import 'lodash/debounce';
import 'lodash/throttle';
import { D as DataType, d as VsBaseFormControl } from './index-5c5f12ca.js';
import '@material-ui/core/FormControl';
import '@material-ui/core/FormHelperText';
import '@material-ui/core/FormLabel';
import '@material-ui/core/TextField';
import '@material-ui/core/InputAdornment';
import '@material-ui/core/Tooltip';
import '@material-ui/core/styles';
import classNames from 'classnames';
import '@material-ui/core/Icon';
import '@material-ui/core/SvgIcon';
import 'date-fns';
import 'events';
import '@material-ui/core/Typography';
import '@material-ui/core/Grid';
import '@material-ui/core/IconButton';
import Radio from '@material-ui/core/Radio';
import RadioGroup from '@material-ui/core/RadioGroup';
import FormControlLabel from '@material-ui/core/FormControlLabel';
const styles = {
radioHeight: {
height: '30px'
}
};
class VsRadioGroup extends Component {
constructor(props) {
super(props);
this.RadioGroupProps = {};
this.RadioProps = {};
this.FormControlLabelProps = {};
this.renderComp = (dataProps) => {
return (createElement(RadioGroup, Object.assign({ value: this.getValue(dataProps.state.value), onChange: this.changeValue(dataProps) }, this.RadioGroupProps), this.renderItems(dataProps)));
};
this.renderItems = (_dataProps) => {
const items = this.comp.data.items;
return items.map((item, ind) => {
if (ind === 0) {
return createElement(FormControlLabel, Object.assign({ inputRef: this.inputRef, value: item.value.toString(), key: item.value, control: createElement(Radio, Object.assign({}, this.RadioProps)), label: item.text }, this.FormControlLabelProps));
}
else {
return createElement(FormControlLabel, Object.assign({ value: item.value.toString(), key: item.value, control: createElement(Radio, Object.assign({}, this.RadioProps)), label: item.text }, this.FormControlLabelProps));
}
});
};
this.changeValue = (dataProps) => (evt) => {
const value = evt.target.value;
const schemaValue = this.comp.data.dataType === DataType.number ? toNumber(value) : value;
dataProps.updateValue(value, schemaValue);
};
this.initProps();
this.inputRef = createRef();
props.schemaManager.addInputRef(props.comp, this.inputRef);
}
get comp() { return this.props.comp; }
render() {
return (createElement(VsBaseFormControl, Object.assign({}, this.props, { showLabel: true }), this.renderComp));
}
getValue(value) {
return (isUndefined(value) || isNull(value)) ? '' : value.toString();
}
initProps() {
const { FormControlProps, FormHelperTextProps, FormLabelProps, RadioProps, FormControlLabelProps, ...RadioGroupProps } = this.comp.props;
if (RadioGroupProps) {
this.RadioGroupProps = RadioGroupProps;
}
if (RadioProps) {
this.RadioProps = RadioProps;
}
if (FormControlLabelProps) {
this.FormControlLabelProps = FormControlLabelProps;
}
this.RadioProps.className = classNames(this.props.classes.radioHeight, this.RadioProps.className);
}
}
export default VsRadioGroup;
export { styles };