Button.js 1.46 KB
import template from "./Button.html";
import style from "antd/lib/button/style/index.css";

class Button {
    
    useModules = ["esNgAntd"];

    state = {
        disabled: null,
        className: "",
    };

    watch = {
        loading:  (newVal)=> {
            if (newVal !== undefined) {
                if (newVal === "true") {
                    this.state.className += " ant-btn-loading";
                } else {
                    this.state.className = this.state.className.replace(
                        " ant-btn-loading",
                        ""
                    );
                }
            }
        },
    };

    constructor($element) {
        esNgAntd.createStyle("ant-btn", style);

        let className = ["ant-btn"];
        if (this.props.type) {
            className.push("ant-btn-" + this.props.type);
        }
        if (this.props.size && ["lg", "sm", "xs"].includes(this.props.size)) {
            className.push("ant-btn-" + this.props.size);
        }
        if (this.props.ghost) {
            className.push("ant-btn-background-ghost");
        }
        this.state.className = className.join(" ");

        if (this.props.htmlType) {
            $element[0].setAttribute("type", this.props.htmlType)
        }
    }

    render() {
        return template;
    }
}

Button.propTypes = {
    type: PropTypes.string,
    size: PropTypes.string,
    htmlType: PropTypes.string,
    ghost: PropTypes.boolean,
    loading: PropTypes.boolean,
};