@massds/mayflower-react
Version:
React versions of Mayflower design system UI components
122 lines (98 loc) • 4.03 kB
JavaScript
;
exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _IconChevron = _interopRequireDefault(require("../Icon/IconChevron.js"));
var _index = _interopRequireDefault(require("../ButtonWithIcon/index.js"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
var GenTeaserOrgs = /*#__PURE__*/function (_React$Component) {
_inheritsLoose(GenTeaserOrgs, _React$Component);
function GenTeaserOrgs(props) {
var _this;
_this = _React$Component.call(this, props) || this;
var orgs = props.orgs;
var allOrgs = orgs.split(',');
_this.state = {
shouldTruncate: allOrgs.length > 3,
truncateOrgs: allOrgs.length > 3,
showAll: false
};
_this.handleClick = _this.handleClick.bind(_assertThisInitialized(_this));
_this.handleKeyPress = _this.handleKeyPress.bind(_assertThisInitialized(_this));
return _this;
} // eslint-disable-next-line camelcase
var _proto = GenTeaserOrgs.prototype;
_proto.UNSAFE_componentWillReceiveProps = function UNSAFE_componentWillReceiveProps(nextProps) {
var orgs = nextProps.orgs;
var allOrgs = orgs.split(',');
this.setState({
shouldTruncate: allOrgs.length > 3,
truncateOrgs: allOrgs.length > 3,
showAll: false
});
};
_proto.handleClick = function handleClick() {
this.setState(function (prevState) {
return {
showAll: !prevState.showAll,
truncateOrgs: !prevState.truncateOrgs
};
});
};
_proto.handleKeyPress = function handleKeyPress(e) {
if (e.key === 'Enter') {
this.setState(function (prevState) {
return {
showAll: !prevState.showAll,
truncateOrgs: !prevState.truncateOrgs
};
});
}
};
_proto.render = function render() {
var _this2 = this;
var orgs = this.props.orgs;
var teaserOrgs = orgs.split(',');
if (!this.state.shouldTruncate) {
return /*#__PURE__*/_react["default"].createElement("span", {
className: "ma__gen-teaser__org"
}, teaserOrgs.join(', '));
}
var shownOrgs = teaserOrgs.slice(0, 3);
var hiddenOrgs = teaserOrgs.slice(3);
var toggleProps = {
classes: ['ma__gen-teaser__org__show-more', "" + (!this.state.truncateOrgs ? 'show-fewer' : '')],
onClick: function onClick(e) {
return _this2.handleClick(e);
},
onKeyPress: function onKeyPress(e) {
return _this2.handleKeyPress(e);
},
text: this.state.truncateOrgs ? " & " + hiddenOrgs.length + " more" : ' Show fewer',
theme: 'c-primary',
usage: 'quaternary-simple',
capitalized: true,
expanded: this.state.showAll,
icon: /*#__PURE__*/_react["default"].createElement(_IconChevron["default"], {
width: 16,
height: "16"
})
};
var toggle = /*#__PURE__*/_react["default"].createElement(_index["default"], toggleProps);
var displayedOrgs = this.state.showAll ? teaserOrgs.join(', ') : shownOrgs.join(', ');
return /*#__PURE__*/_react["default"].createElement("span", {
className: "ma__gen-teaser__org"
}, /*#__PURE__*/_react["default"].createElement("span", null, displayedOrgs), toggle);
};
return GenTeaserOrgs;
}(_react["default"].Component);
GenTeaserOrgs.propTypes = process.env.NODE_ENV !== "production" ? {
/** A comma seperate list of organizations. */
orgs: _propTypes["default"].string.isRequired
} : {};
var _default = GenTeaserOrgs;
exports["default"] = _default;
module.exports = exports.default;