vs-form-mui-4x
Version:
A schema-based form generator component for React using material-ui 4.x
53 lines (50 loc) • 2.1 kB
JavaScript
import { Component } from 'react';
import isUndefined from 'lodash/isUndefined';
import { B as ButtonAction } from './index-5c5f12ca.js';
import classNames from 'classnames';
import { b as buttonAction } from './strings-9d3c2f25.js';
class VsButtonBase extends Component {
constructor(props) {
super(props);
this.componentEventParams = this.props.schemaManager.getComponentEventParams(this.props.comp);
this.btnClick = (event) => {
const comp = this.props.comp;
if (comp.action === ButtonAction.save) {
event.preventDefault();
this.props.schemaManager.submit();
}
if (comp.action === ButtonAction.cancel) {
this.props.schemaManager.cancelValues();
}
if (comp.onClick) {
comp.onClick(this.componentEventParams);
}
};
this.initProps = () => {
if (this.comp.action) {
if (isUndefined(this.comp.disabled)) {
this.props.buttonProps.disabled = !this.props.schemaManager.dataStateChanged;
}
if (!this.comp.label) {
this.comp.label = buttonAction[this.comp.action];
}
if (isUndefined(this.comp.icon)) ;
if (this.comp.action === ButtonAction.save) {
this.props.buttonProps.type = 'submit';
}
}
if (isUndefined(this.props.buttonProps.color)) {
this.props.buttonProps.color = 'primary';
}
this.props.buttonProps.onClick = this.btnClick;
this.props.IconProps.className = classNames(this.props.IconProps.className, 'mdi', 'mdi-' + this.comp.icon, this.props.classes.icon);
};
this.initProps();
}
get comp() { return this.props.comp; }
render() {
const { btnClick } = this;
return this.props.children({ btnClick, ...this.props });
}
}
export { VsButtonBase as V };