rmwc
Version:
A thin React wrapper for Material Design (Web) Components
159 lines (128 loc) • 6.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.IconButton = exports.IconButtonRoot = 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/icon-button/dist/mdc.iconButton');
var _Icon = require('../Icon');
var _utils = require('../Icon/utils');
var _Base = require('../Base');
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 IconButtonRoot = exports.IconButtonRoot = (0, _Base.simpleTag)({
displayName: 'IconButtonRoot',
tag: _Icon.Icon,
classNames: 'mdc-icon-button',
defaultProps: {
role: 'button',
tabIndex: '0'
}
});
var IconButton = exports.IconButton = function (_withFoundation) {
_inherits(IconButton, _withFoundation);
function IconButton() {
_classCallCheck(this, IconButton);
return _possibleConstructorReturn(this, (IconButton.__proto__ || Object.getPrototypeOf(IconButton)).apply(this, arguments));
}
_createClass(IconButton, [{
key: 'isOn',
/** Takes into account our checked prop */
value: function isOn() {
if (this.props.checked !== undefined) {
return this.props.checked;
}
return this.foundation_ && this.on;
}
}, {
key: 'initialize',
value: function initialize() {
this.ripple_ = this.initRipple_();
_get(IconButton.prototype.__proto__ || Object.getPrototypeOf(IconButton.prototype), 'initialize', this).call(this);
}
}, {
key: 'syncWithProps',
value: function syncWithProps(nextProps) {
var _this2 = this;
// checked
(0, _Base.syncFoundationProp)(nextProps.checked, this.on, function () {
_this2.on = !!nextProps.checked;
_this2.foundation_ && _this2.foundation_.refreshToggleData();
});
}
}, {
key: 'renderIcon',
value: function renderIcon() {
var _props = this.props,
use = _props.use,
children = _props.children,
onContent = _props.onContent,
offContent = _props.offContent,
onLabel = _props.onLabel,
offLabel = _props.offLabel,
checked = _props.checked,
rest = _objectWithoutProperties(_props, ['use', 'children', 'onContent', 'offContent', 'onLabel', 'offLabel', 'checked']);
var iconContent = this.isOn() && onContent ? onContent : offContent || use || children;
var strategy = (0, _utils.getIconStrategy)(iconContent, 'auto', null);
if (['url', 'component'].indexOf(strategy || '') !== -1) {
return React.createElement(_Icon.Icon, Object.assign({}, rest, { use: iconContent }));
}
return iconContent;
}
}, {
key: 'render',
value: function render() {
var _props2 = this.props,
checked = _props2.checked,
onContent = _props2.onContent,
offContent = _props2.offContent,
onLabel = _props2.onLabel,
offLabel = _props2.offLabel,
label = _props2.label,
apiRef = _props2.apiRef,
use = _props2.use,
rest = _objectWithoutProperties(_props2, ['checked', 'onContent', 'offContent', 'onLabel', 'offLabel', 'label', 'apiRef', 'use']);
var ariaPressed = this.isOn();
var ariaLabel = label || offLabel;
return React.createElement(IconButtonRoot, Object.assign({
tag: 'button'
}, rest, {
use: this.renderIcon(),
elementRef: this.foundationRefs.root_,
'aria-label': ariaLabel,
'aria-pressed': ariaPressed,
'aria-hidden': 'true',
'data-toggle-on-content': onContent,
'data-toggle-on-label': onLabel,
'data-toggle-off-content': offContent,
'data-toggle-off-label': offLabel
}));
}
}]);
return IconButton;
}((0, _Base.withFoundation)({
constructor: _mdc.MDCIconButtonToggle,
adapter: {
/**
* A hack to get uncontrolled Icons to render
* setText is only used to set the iconContent.
* We are controlling the content through React
* So here, we just use it as a queue to re-render
*/
setText: function setText() {
this.forceUpdate();
}
}
}));
Object.defineProperty(IconButton, 'displayName', {
enumerable: true,
writable: true,
value: 'IconButton'
});
exports.default = IconButton;