fk-react-ui-components
Version:
Step 1 : Create a file in [ Seeds / Plants / Trees ] <br> Step 2 : It should export an Object with component name and story Component [Refer other components] <br> Step 3 : Story Component should return a react component <br> Step 3 : Created file should
184 lines (150 loc) • 8.27 kB
JavaScript
;
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 _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _reactClickOutside = require('react-click-outside');
var _reactClickOutside2 = _interopRequireDefault(_reactClickOutside);
var _Notification = require('./Notification');
var _Notification2 = _interopRequireDefault(_Notification);
var _styles = require('./styles');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
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 NotificationsBar = function (_Component) {
_inherits(NotificationsBar, _Component);
function NotificationsBar(props) {
_classCallCheck(this, NotificationsBar);
var _this = _possibleConstructorReturn(this, (NotificationsBar.__proto__ || Object.getPrototypeOf(NotificationsBar)).call(this, props));
_this.onRemoveClick = function (e, id) {
_this.setState({
notifications: NotificationsBar.deleteNotifications(id, _this.state.notifications)
});
_this.props.onRemoveClick(id);
};
_this.showMoreNotifications = function () {
_this.setState({
isOpen: !_this.state.isOpen
});
};
_this.state = {
isOpen: false,
notifications: NotificationsBar.attachIdToNotifications(props.notifications)
};
return _this;
}
_createClass(NotificationsBar, [{
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (nextProps.notifications.length) {
this.setState({
notifications: this.state.notifications.concat(NotificationsBar.attachIdToNotifications(nextProps.notifications))
});
this.props.clearNotifications();
}
}
}, {
key: 'handleClickOutside',
value: function handleClickOutside() {
this.setState({
isOpen: false
});
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
var allNotifications = NotificationsBar.getChronologicallyOrderedNotifications([].concat(_toConsumableArray(this.state.notifications)));
var firstNotification = allNotifications[0] || {};
allNotifications.shift();
if (allNotifications.length) {
return _react2.default.createElement(
_styles.NotificationBarWrapper,
null,
_react2.default.createElement(
_styles.NotificationBarHeader,
null,
_react2.default.createElement(
_styles.ShowMoreWrapper,
{ onClick: this.showMoreNotifications },
_react2.default.createElement(
_styles.MoreNotificationsCount,
null,
allNotifications.length
),
this.state.isOpen ? _react2.default.createElement(_styles.CaretIconOpen, { className: 'fa fa-caret-up' }) : _react2.default.createElement(_styles.CaretIconClosed, { className: 'fa fa-caret-down' })
),
_react2.default.createElement(_Notification2.default, _extends({}, firstNotification, {
onRemoveClick: this.onRemoveClick
}))
),
_react2.default.createElement(
_styles.AllNotifications,
{ isOpen: this.state.isOpen },
allNotifications.map(function (notification) {
return _react2.default.createElement(
_styles.NotificationWrapper,
{ key: notification.id },
_react2.default.createElement(_Notification2.default, _extends({}, notification, {
onRemoveClick: _this2.onRemoveClick
}))
);
})
)
);
} else if (firstNotification.id) {
return _react2.default.createElement(
_styles.NotificationBarWrapper,
null,
_react2.default.createElement(
_styles.NotificationBarHeader,
null,
_react2.default.createElement(_Notification2.default, _extends({}, firstNotification, {
onRemoveClick: this.onRemoveClick
}))
)
);
}
return null;
}
}]);
return NotificationsBar;
}(_react.Component);
NotificationsBar.notificationId = 0;
NotificationsBar.getChronologicallyOrderedNotifications = function (notifications) {
return notifications.sort(function (a, b) {
return new Date(b.timestamp) - new Date(a.timestamp);
});
};
NotificationsBar.attachIdToNotifications = function (notifications) {
return notifications.map(function (notification) {
return _extends({
id: ++NotificationsBar.notificationId
}, notification);
});
};
NotificationsBar.deleteNotifications = function (id, notifications) {
return notifications.filter(function (notification) {
return notification.id !== id;
});
};
NotificationsBar.propTypes = {
notifications: _propTypes.PropTypes.arrayOf(_propTypes.PropTypes.shape({
state: _propTypes.PropTypes.string,
message: _propTypes.PropTypes.string,
timestamp: _propTypes.PropTypes.oneOfType([_propTypes.PropTypes.number, _propTypes.PropTypes.instanceOf(Date)])
})),
onRemoveClick: _propTypes.PropTypes.func,
clearNotifications: _propTypes.PropTypes.func.isRequired
};
NotificationsBar.defaultProps = {
notifications: [],
onRemoveClick: function onRemoveClick() {}
};
exports.default = (0, _reactClickOutside2.default)(NotificationsBar);