UNPKG

@vs-form/vs-form

Version:

A schema-based form generator component for React using material-ui

163 lines (158 loc) 6.66 kB
'use strict'; function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var React = require('react'); var React__default = _interopDefault(React); require('lodash/capitalize'); require('lodash/cloneDeep'); require('lodash/get'); require('lodash/has'); var isArray = _interopDefault(require('lodash/isArray')); require('lodash/isDate'); require('lodash/isBoolean'); require('lodash/isEmpty'); require('lodash/isFunction'); require('lodash/isInteger'); require('lodash/isNull'); require('lodash/isNumber'); require('lodash/isObject'); require('lodash/isPlainObject'); require('lodash/isRegExp'); var isString = _interopDefault(require('lodash/isString')); var isUndefined = _interopDefault(require('lodash/isUndefined')); require('lodash/merge'); require('lodash/set'); require('lodash/toInteger'); require('lodash/toNumber'); require('lodash/trimEnd'); require('lodash/uniq'); require('lodash/debounce'); require('lodash/throttle'); var __chunk_2 = require('./chunk-e8808437.js'); require('@material-ui/core/FormControl'); require('@material-ui/core/FormHelperText'); require('@material-ui/core/FormLabel'); require('@material-ui/core/TextField'); require('@material-ui/core/InputAdornment'); require('./chunk-7581cbb1.js'); require('@material-ui/core/Tooltip'); var __chunk_4 = require('./chunk-364f6801.js'); require('@material-ui/core/styles'); require('classnames'); require('@material-ui/core/Icon'); require('@material-ui/core/SvgIcon'); var __chunk_5 = require('./chunk-41ea4037.js'); require('./chunk-2f21f066.js'); require('date-fns'); require('events'); require('@material-ui/core/Typography'); require('./chunk-819d0a62.js'); require('@material-ui/core/Grid'); var IconButton = _interopDefault(require('@material-ui/core/IconButton')); require('./chunk-f9facc49.js'); var Checkbox = _interopDefault(require('@material-ui/core/Checkbox')); var FormControlLabel = _interopDefault(require('@material-ui/core/FormControlLabel')); var FormGroup = _interopDefault(require('@material-ui/core/FormGroup')); const cssFlex = { display: 'flex', }; const cssShiftRight = { flexGrow: 1 }; class VsChecklistBox extends React.Component { constructor(props) { super(props); this.FormGroupProps = {}; this.CheckboxProps = {}; this.FormControlLabelProps = {}; this.IconButtonProps = {}; this.componentEventParams = this.props.schemaManager.getComponentEventParams(this.props.comp); this.renderComp = (dataProps) => { return (React.createElement(FormGroup, Object.assign({}, this.FormGroupProps), this.renderItems(dataProps))); }; this.renderItems = (dataProps) => { return this.comp.data.items.map((item) => { return (React.createElement("div", { key: item.value, style: cssFlex }, React.createElement(FormControlLabel, Object.assign({ label: item.text, control: React.createElement(Checkbox, Object.assign({ checked: dataProps.state.value.indexOf(item.value) !== -1, onChange: this.handleToggle(item.value, dataProps) }, this.CheckboxProps)), inputRef: this.inputRef }, this.FormControlLabelProps)), React.createElement("div", { style: cssShiftRight }), this.actionButton(dataProps, item))); }); }; this.actionButton = (dataProps, item) => { let iconComp = this.getIcon(item.actionIcon); if (!iconComp) { iconComp = this.getIcon(this.comp.actionIcon); } if (iconComp) { return (React.createElement(IconButton, Object.assign({ onClick: this.actionBtnClick(dataProps, item) }, this.IconButtonProps), iconComp)); } else { return null; } }; this.getIcon = (icon) => { if (isArray(icon) && icon.length > 0) { const iconComp = this.props.schema.components[icon[0]]; if (iconComp) { return React.createElement(__chunk_4.Item, Object.assign({}, iconComp.props, { schemaManager: this.props.schemaManager, schema: this.props.schema, node: iconComp.node, comp: iconComp })); } else { return React.createElement("div", null, "Icon not found ", icon[0]); } } else if (isString(icon)) { return React.createElement(__chunk_5.BaseIcon, { icon: icon, color: "primary" }); } else { return null; } }; this.handleToggle = (value, dataProps) => () => { const currentIndex = dataProps.state.value.indexOf(value); const newValue = [...dataProps.state.value]; if (currentIndex === -1) { newValue.push(value); } else { newValue.splice(currentIndex, 1); } dataProps.updateValue(newValue); }; this.actionBtnClick = (dataProps, item) => () => { if (this.comp.onActionClick) { const params = { value: dataProps.state.value, ...this.componentEventParams, item }; this.comp.onActionClick(params); } }; this.initProps(); this.inputRef = React.createRef(); props.schemaManager.addInputRef(props.comp, this.inputRef); } get comp() { return this.props.comp; } render() { return (React.createElement(__chunk_2.VsBaseFormControl, Object.assign({ showLabel: true }, this.props), this.renderComp)); } initProps() { const { FormControlProps, FormHelperTextProps, FormLabelProps, CheckboxProps, FormControlLabelProps, IconButtonProps, ...FormGroupProps } = this.comp.props; if (FormGroupProps) { this.FormGroupProps = FormGroupProps; } if (FormControlLabelProps) { this.FormControlLabelProps = FormControlLabelProps; } if (CheckboxProps) { this.CheckboxProps = CheckboxProps; } if (IconButtonProps) { this.IconButtonProps = IconButtonProps; } if (isUndefined(this.FormGroupProps.row)) { this.FormGroupProps.row = this.comp.rowDisplay; } if (isUndefined(this.CheckboxProps.disableRipple)) { this.CheckboxProps.disableRipple = true; } } } exports.default = VsChecklistBox;