preact-material-components
Version:
preact wrapper for "Material Components for the web"
93 lines (74 loc) • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _preact = require("preact");
var _MaterialComponent = _interopRequireDefault(require("../MaterialComponent"));
var _checkbox = require("@material/checkbox/");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
/*
* Default props for check box
*/
const defaultProps = {
checked: false,
indeterminate: false
};
/**
*/
class Checkbox extends _MaterialComponent.default {
constructor() {
super();
this.componentName = 'checkbox';
this._mdcProps = ['disabled'];
}
componentDidMount() {
this.MDComponent = new _checkbox.MDCCheckbox(this.control);
toggleCheckbox(defaultProps, this.props, this.MDComponent);
}
componentWillUnmount() {
this.MDComponent.destroy && this.MDComponent.destroy();
}
componentWillUpdate(nextProps) {
toggleCheckbox(this.props, nextProps, this.MDComponent);
}
materialDom(allprops) {
const className = allprops.className,
props = _objectWithoutProperties(allprops, ["className"]);
return (0, _preact.h)("div", {
className: 'mdc-checkbox ' + className,
ref: this.setControlRef
}, (0, _preact.h)("input", _extends({
type: "checkbox",
className: "mdc-checkbox__native-control"
}, props)), (0, _preact.h)("div", {
className: "mdc-checkbox__background"
}, (0, _preact.h)("svg", {
version: "1.1",
className: "mdc-checkbox__checkmark",
xmlns: "http://www.w3.org/2000/svg",
viewBox: "0 0 24 24"
}, (0, _preact.h)("path", {
className: "mdc-checkbox__checkmark-path",
fill: "none",
stroke: "white",
d: "M1.73,12.91 8.1,19.28 22.79,4.59"
})), (0, _preact.h)("div", {
className: "mdc-checkbox__mixedmark"
})));
}
}
/*
* Function to add declarative opening/closing to drawer
*/
exports.default = Checkbox;
function toggleCheckbox(oldprops, newprops, cbox) {
if ('checked' in oldprops && 'checked' in newprops && oldprops.checked !== newprops.checked) {
cbox.checked = newprops.checked;
}
if ('indeterminate' in oldprops && 'indeterminate' in newprops && oldprops.indeterminate !== newprops.indeterminate) {
cbox.indeterminate = newprops.indeterminate;
}
}