preact-material-components
Version:
preact wrapper for "Material Components for the web"
81 lines (63 loc) • 2.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _preact = require("preact");
var _MaterialComponent = _interopRequireDefault(require("../MaterialComponent"));
var _radio = require("@material/radio/");
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
};
/**
* @prop mini = false
* @prop plain = false
*/
class Radio extends _MaterialComponent.default {
constructor() {
super();
this.componentName = 'radio';
}
componentDidMount() {
this.MDComponent = _radio.MDCRadio.attachTo(this.control);
toggleRadio(defaultProps, this.props, this.MDComponent);
}
componentWillUnmount() {
this.MDComponent.destroy && this.MDComponent.destroy();
}
componentWillUpdate(nextProps) {
toggleRadio(this.props, nextProps, this.MDComponent);
}
materialDom(allprops) {
const className = allprops.className,
props = _objectWithoutProperties(allprops, ["className"]);
return (0, _preact.h)("div", {
className: className,
ref: this.setControlRef
}, (0, _preact.h)("input", _extends({
className: "mdc-radio__native-control",
type: "radio"
}, props)), (0, _preact.h)("div", {
className: "mdc-radio__background"
}, (0, _preact.h)("div", {
className: "mdc-radio__outer-circle"
}), (0, _preact.h)("div", {
className: "mdc-radio__inner-circle"
})));
}
}
/*
* Function to add declarative checked to radio
*/
exports.default = Radio;
function toggleRadio(oldprops, newprops, radio) {
if ('checked' in oldprops && 'checked' in newprops && oldprops.checked !== newprops.checked) {
radio.checked = newprops.checked;
}
}