UNPKG

@materialr/snackbar

Version:

Material snackbar implementation for React

190 lines (157 loc) 7.49 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _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; }; 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 _snackbar = require('@material/snackbar'); var _constants = require('@material/snackbar/constants'); var _classnames2 = require('classnames'); var _classnames3 = _interopRequireDefault(_classnames2); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _react = require('react'); var _react2 = _interopRequireDefault(_react); require('@material/snackbar/mdc-snackbar.scss'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } 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 Snackbar = function (_React$Component) { _inherits(Snackbar, _React$Component); function Snackbar(props) { _classCallCheck(this, Snackbar); var _this = _possibleConstructorReturn(this, (Snackbar.__proto__ || Object.getPrototypeOf(Snackbar)).call(this, props)); _this.elementRoot = undefined; _this.snackbar = undefined; _this.getClassNames = _this.getClassNames.bind(_this); _this.snackbarShow = _this.snackbarShow.bind(_this); return _this; } _createClass(Snackbar, [{ key: 'componentDidMount', value: function componentDidMount() { var HIDE_EVENT = _constants.strings.HIDE_EVENT, SHOW_EVENT = _constants.strings.SHOW_EVENT; var _props = this.props, onHide = _props.onHide, onShow = _props.onShow; this.snackbar = new _snackbar.MDCSnackbar(this.elementRoot); if (onHide) { this.snackbar.listen(HIDE_EVENT, onHide); } if (onShow) { this.snackbar.listen(SHOW_EVENT, onShow); } this.snackbarShow(); } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { var HIDE_EVENT = _constants.strings.HIDE_EVENT, SHOW_EVENT = _constants.strings.SHOW_EVENT; var _props2 = this.props, onHide = _props2.onHide, onShow = _props2.onShow; if (onHide) { this.snackbar.unlisten(HIDE_EVENT, onHide); } if (onShow) { this.snackbar.unlisten(SHOW_EVENT, onShow); } this.snackbar.destroy(); } }, { key: 'getClassNames', value: function getClassNames() { var _props3 = this.props, alignStart = _props3.alignStart, className = _props3.className; return (0, _classnames3.default)(_defineProperty({ 'mdc-snackbar': true, 'mdc-snackbar--align-start': alignStart }, className, !!className)); } }, { key: 'snackbarShow', value: function snackbarShow() { var _props4 = this.props, actionHandler = _props4.actionHandler, actionText = _props4.actionText, message = _props4.message, multiline = _props4.multiline, multilineActionOnBottom = _props4.multilineActionOnBottom, timeout = _props4.timeout; this.snackbar.show({ actionHandler: actionHandler, actionOnBottom: multilineActionOnBottom, actionText: actionText, message: message, multiline: multiline, timeout: timeout }); } }, { key: 'render', value: function render() { var _this2 = this; var getClassNames = this.getClassNames, _props5 = this.props, actionHandler = _props5.actionHandler, actionText = _props5.actionText, alignStart = _props5.alignStart, className = _props5.className, message = _props5.message, multiline = _props5.multiline, multilineActionOnBottom = _props5.multilineActionOnBottom, onHide = _props5.onHide, onShow = _props5.onShow, timeout = _props5.timeout, props = _objectWithoutProperties(_props5, ['actionHandler', 'actionText', 'alignStart', 'className', 'message', 'multiline', 'multilineActionOnBottom', 'onHide', 'onShow', 'timeout']); return _react2.default.createElement( 'div', _extends({ 'aria-live': 'assertive', 'aria-atomic': 'true', className: getClassNames(), ref: function ref(elementRoot) { _this2.elementRoot = elementRoot; } }, props), _react2.default.createElement('div', { className: 'mdc-snackbar__text' }), _react2.default.createElement( 'div', { className: 'mdc-snackbar__action-wrapper' }, _react2.default.createElement('button', { className: 'mdc-snackbar__action-button', type: 'button' }) ) ); } }]); return Snackbar; }(_react2.default.Component); Snackbar.propTypes = { actionHandler: _propTypes2.default.func, actionText: _propTypes2.default.string, alignStart: _propTypes2.default.bool, className: _propTypes2.default.string, message: _propTypes2.default.string.isRequired, multiline: _propTypes2.default.bool, multilineActionOnBottom: _propTypes2.default.bool, onHide: _propTypes2.default.func, onShow: _propTypes2.default.func, timeout: _propTypes2.default.number }; Snackbar.defaultProps = { actionHandler: undefined, actionText: undefined, alignStart: false, className: undefined, multiline: false, multilineActionOnBottom: false, onHide: undefined, onShow: undefined, timeout: 2750 }; exports.default = Snackbar;