rmwc
Version:
A thin React wrapper for Material Design (Web) Components
230 lines (185 loc) • 9.07 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Checkbox = exports.CheckboxLabel = exports.CheckboxMixedmark = exports.CheckboxCheckmarkPath = exports.CheckboxCheckmark = exports.CheckboxBackground = exports.CheckboxNativeControl = exports.CheckboxRoot = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
var _react = require('react');
var React = _interopRequireWildcard(_react);
var _mdc = require('@material/checkbox/dist/mdc.checkbox');
var _FormField = require('../FormField');
var _FormField2 = _interopRequireDefault(_FormField);
var _Base = require('../Base');
var _withFoundation2 = require('../Base/withFoundation');
var _randomId = require('../Base/utils/randomId');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var CheckboxRoot = exports.CheckboxRoot = (0, _Base.simpleTag)({
displayName: 'CheckboxRoot',
classNames: function classNames(props) {
return ['mdc-checkbox', {
'mdc-checkbox--disabled': props.disabled
}];
},
consumeProps: ['disabled']
});
var CheckboxNativeControl = exports.CheckboxNativeControl = (0, _Base.simpleTag)({
displayName: 'CheckboxNativeControl',
tag: 'input',
classNames: 'mdc-checkbox__native-control',
defaultProps: {
type: 'checkbox'
}
});
var CheckboxBackground = exports.CheckboxBackground = (0, _Base.simpleTag)({
displayName: 'CheckboxBackground',
classNames: 'mdc-checkbox__background'
});
var CheckboxCheckmark = exports.CheckboxCheckmark = (0, _Base.simpleTag)({
displayName: 'CheckboxCheckmark',
tag: 'svg',
classNames: 'mdc-checkbox__checkmark',
defaultProps: {
viewBox: '0 0 24 24'
}
});
var CheckboxCheckmarkPath = exports.CheckboxCheckmarkPath = (0, _Base.simpleTag)({
displayName: 'CheckboxCheckmarkPath',
tag: 'path',
classNames: 'mdc-checkbox__checkmark-path',
defaultProps: {
fill: 'none',
stroke: 'white',
d: 'M1.73,12.91 8.1,19.28 22.79,4.59'
}
});
var CheckboxMixedmark = exports.CheckboxMixedmark = (0, _Base.simpleTag)({
displayName: 'CheckboxMixedmark',
classNames: 'mdc-checkbox__mixedmark'
});
var CheckboxLabel = exports.CheckboxLabel = (0, _Base.simpleTag)({
displayName: 'CheckboxLabel',
tag: 'label'
});
/**
* A Checkbox component
*/
var Checkbox = exports.Checkbox = function (_withFoundation) {
_inherits(Checkbox, _withFoundation);
function Checkbox(props) {
_classCallCheck(this, Checkbox);
var _this = _possibleConstructorReturn(this, (Checkbox.__proto__ || Object.getPrototypeOf(Checkbox)).call(this, props));
_this.generatedId = (0, _randomId.randomId)('checkbox');
return _this;
}
_createClass(Checkbox, [{
key: 'componentDidMount',
value: function componentDidMount() {
var _this2 = this;
_get(Checkbox.prototype.__proto__ || Object.getPrototypeOf(Checkbox.prototype), 'componentDidMount', this).call(this);
this.ripple_ = this.initRipple_();
// Fixes bug #247
// Basically we need to do the following hacks
// - register syncWithProps so it runs on change
// - deregister the original change handler and re-register so it
// runs after sync with props
this.boundChangeHandler = function () {
return _this2.syncWithProps(_this2.props);
};
this.foundation_ && this.foundation_.adapter_.deregisterChangeHandler(this.foundation_.changeHandler_);
this.foundation_ && this.foundation_.adapter_.registerChangeHandler(this.boundChangeHandler);
this.foundation_ && this.foundation_.adapter_.registerChangeHandler(this.foundation_.changeHandler_);
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
this.foundation_ && this.foundation_.adapter_.deregisterChangeHandler(this.boundChangeHandler);
_get(Checkbox.prototype.__proto__ || Object.getPrototypeOf(Checkbox.prototype), 'componentWillUnmount', this).call(this);
}
}, {
key: 'syncWithProps',
value: function syncWithProps(nextProps) {
var _this3 = this;
// checked
(0, _withFoundation2.syncFoundationProp)(nextProps.checked, this.checked, function () {
return _this3.checked = !!nextProps.checked;
});
// indeterminate
(0, _withFoundation2.syncFoundationProp)(nextProps.indeterminate, this.indeterminate, function () {
return _this3.indeterminate = !!nextProps.indeterminate;
});
// disabled
(0, _withFoundation2.syncFoundationProp)(nextProps.disabled, this.disabled, function () {
return _this3.disabled = !!nextProps.disabled;
});
// value
(0, _withFoundation2.syncFoundationProp)(nextProps.value, this.value, function () {
return _this3.value = nextProps.value;
});
}
}, {
key: 'render',
value: function render() {
var _props = this.props,
_props$label = _props.label,
label = _props$label === undefined ? '' : _props$label,
id = _props.id,
children = _props.children,
checked = _props.checked,
indeterminate = _props.indeterminate,
apiRef = _props.apiRef,
rest = _objectWithoutProperties(_props, ['label', 'id', 'children', 'checked', 'indeterminate', 'apiRef']);
var root_ = this.foundationRefs.root_;
var labelId = id || this.generatedId;
var checkbox = React.createElement(
CheckboxRoot,
{ elementRef: root_, disabled: rest.disabled },
React.createElement(CheckboxNativeControl, Object.assign({ id: labelId, checked: checked }, rest)),
React.createElement(
CheckboxBackground,
null,
React.createElement(
CheckboxCheckmark,
null,
React.createElement(CheckboxCheckmarkPath, null)
),
React.createElement(CheckboxMixedmark, null)
)
);
/**
* We have to conditionally wrap our checkbox in a formfield
* If we have a label
*/
if (label.length || children) {
return React.createElement(
_FormField2.default,
null,
checkbox,
React.createElement(
CheckboxLabel,
{ id: labelId + 'label', htmlFor: labelId },
label,
children
)
);
} else {
return checkbox;
}
}
}]);
return Checkbox;
}((0, _withFoundation2.withFoundation)({
constructor: _mdc.MDCCheckbox,
adapter: {}
}));
Object.defineProperty(Checkbox, 'displayName', {
enumerable: true,
writable: true,
value: 'Checkbox'
});
exports.default = Checkbox;