@massds/mayflower-react
Version:
React versions of Mayflower design system UI components
218 lines (188 loc) • 8.77 kB
JavaScript
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; }
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; }
/**
* HelpTip module.
* @module @massds/mayflower-react/HelpTip
* @requires module:@massds/mayflower-assets/scss/03-organisms/help-tip
* @requires module:@massds/mayflower-assets/scss/01-atoms/svg-icons
* @requires module:@massds/mayflower-assets/scss/01-atoms/svg-loc-icons
*/
import React from "react";
import PropTypes from "prop-types";
import classNames from "classnames";
import Collapse from "../Collapse/index.mjs"; // eslint-disable-next-line import/no-unresolved
import IconQuestionmark from "../Icon/IconQuestionmark.mjs"; // eslint-disable-next-line import/no-unresolved
import IconClose from "../Icon/IconClose.mjs";
import Paragraph from "../Paragraph/index.mjs";
let HelpTip = /*#__PURE__*/function (_React$Component) {
_inheritsLoose(HelpTip, _React$Component);
function HelpTip(props) {
var _this;
_this = _React$Component.call(this, props) || this;
_defineProperty(_assertThisInitialized(_this), "getSplitText", (text, triggers) => {
let splitText = text;
triggers.forEach((trigger, index) => {
if (index === 0) {
splitText = splitText.split(trigger);
} else {
const nextPart = splitText[index].split(trigger);
splitText.pop();
splitText = splitText.concat(nextPart);
}
});
return splitText;
});
_defineProperty(_assertThisInitialized(_this), "toggleOpenForKeyUp", (e, index) => {
if (e.which === 13 || e.which === 32) {
// 13 is enter, 32 is spacebar
e.preventDefault();
e.stopPropagation();
_this.toggleOpen(index);
}
});
_defineProperty(_assertThisInitialized(_this), "toggleOpen", index => {
const isOpen = _this.state.isOpen;
isOpen[index] = !_this.state.isOpen[index];
_this.setState({
isOpen: isOpen
});
});
_defineProperty(_assertThisInitialized(_this), "buildDangerouslyIfHasMarkup", (text, hasMarkup) =>
/* eslint-disable react/no-danger */
hasMarkup ? /*#__PURE__*/React.createElement("span", {
dangerouslySetInnerHTML: {
__html: text
}
}) : text);
const openArray = _this.props.triggerText.map(() => false);
_this.state = {
isOpen: openArray
};
return _this;
}
var _proto = HelpTip.prototype;
_proto.render = function render() {
var _classNames, _classNames2, _classNames3, _classNames4;
const _this$props = this.props,
hasMarkup = _this$props.hasMarkup,
triggerText = _this$props.triggerText,
helpText = _this$props.helpText,
children = _this$props.children,
id = _this$props.id,
theme = _this$props.theme,
text = _this$props.text,
disabled = _this$props.disabled;
const baseClass = classNames((_classNames = {
'ma__help-tip': true,
'mobile-tray': !this.props.bypassMobileStyle
}, _classNames["ma__help-tip__text--" + theme] = theme, _classNames));
const helpTextClasses = classNames((_classNames2 = {
'ma__help-tip__text': !children,
'ma__help-tip__text--children': children
}, _classNames2["ma__help-tip__text--" + theme] = theme, _classNames2));
const helpTextContainer = classNames((_classNames3 = {
'ma__help-tip__container': true
}, _classNames3["ma__help-tip__container--" + theme] = theme, _classNames3));
const helpTextDirect = classNames((_classNames4 = {
'ma__help-tip__text-direct': true
}, _classNames4["ma__help-tip__text-direct--" + theme] = theme, _classNames4));
const splitText = this.getSplitText(text, triggerText);
const childArray = children && (Array.isArray(children) ? children : [children]);
return /*#__PURE__*/React.createElement("span", {
className: baseClass,
id: id
}, triggerText.map((trigger, index) => {
const triggerTextClasses = classNames({
'ma__help-tip__trigger': true,
'ma__help-tip__trigger--active': this.state.isOpen[index],
'ma__help-tip__trigger--disabled': disabled
});
return (
/*#__PURE__*/
/* eslint-disable-next-line react/no-array-index-key */
React.createElement("span", {
key: "help-tip-label-" + id + "-" + index,
className: "ma__help-tip__label",
id: "label-" + id + "-" + index
}, index === 0 && this.buildDangerouslyIfHasMarkup(splitText[index], hasMarkup), /*#__PURE__*/React.createElement("span", {
className: "ma_help-tip__label-a11y",
id: "context-a11y-" + id + "-" + index,
"aria-hidden": "true"
}, this.state.isOpen[index] ? 'Close tooltip info about ' : 'Open tooltip info about ', this.buildDangerouslyIfHasMarkup(trigger, hasMarkup)), /*#__PURE__*/React.createElement("span", {
className: triggerTextClasses,
id: "trigger-" + id + "-" + index,
onClick: () => this.toggleOpen(index),
onKeyUp: e => this.toggleOpenForKeyUp(e, index),
tabIndex: disabled ? -1 : 0,
role: "button",
"aria-describedby": "context-a11y-" + id + "-" + index,
"aria-expanded": this.state.isOpen[index],
"aria-controls": "help-tip-content-" + id + "-" + index
}, this.buildDangerouslyIfHasMarkup(trigger, hasMarkup), /*#__PURE__*/React.createElement(IconQuestionmark, {
height: 15,
width: 15
})), this.buildDangerouslyIfHasMarkup(splitText[index + 1], hasMarkup))
);
}), triggerText.map((trigger, index) => !disabled &&
/*#__PURE__*/
/* eslint-disable-next-line react/no-array-index-key */
React.createElement(Collapse, {
key: "help-tip-collapse-" + id + "-" + index,
"in": this.state.isOpen[index],
dimension: "height",
className: helpTextContainer
}, /*#__PURE__*/React.createElement("div", {
className: "ma__help-tip__content",
id: "help-tip-content-" + id + "-" + index,
"aria-hidden": !this.state.isOpen[index]
}, /*#__PURE__*/React.createElement("div", {
tabIndex: "0",
role: "button",
className: "ma__help-tip__close-mobile",
onClick: () => this.toggleOpen(index),
onKeyUp: e => this.toggleOpenForKeyUp(e, index),
"aria-label": this.state.isOpen[index] && 'Close help tip.'
}, /*#__PURE__*/React.createElement(IconClose, null)), /*#__PURE__*/React.createElement("div", {
tabIndex: "0",
role: "button",
className: "ma__help-tip__close-desktop",
onClick: () => this.toggleOpen(index),
onKeyUp: e => this.toggleOpenForKeyUp(e, index),
"aria-label": this.state.isOpen[index] && 'Close help tip.'
}, /*#__PURE__*/React.createElement(IconClose, null)), (helpText || childArray) && /*#__PURE__*/React.createElement("div", {
className: helpTextClasses,
"aria-live": "polite"
}, helpText && helpText[index] && /*#__PURE__*/React.createElement(Paragraph, {
className: helpTextDirect,
text: helpText[index]
}), childArray && childArray[index] && childArray[index])))));
};
return HelpTip;
}(React.Component);
HelpTip.propTypes = process.env.NODE_ENV !== "production" ? {
/** The text that will have one or more help tips inserted in it. */
text: PropTypes.string,
/** The text that will be a clickable inline toolitp */
triggerText: PropTypes.arrayOf(PropTypes.string),
/** The help text that is displayed on clicking the trigger text.
You can also render children in the help text */
helpText: PropTypes.arrayOf(PropTypes.string),
/** The id for the whole component */
id: PropTypes.string.isRequired,
/** Whether you want the help text to slide up on mobile screens */
bypassMobileStyle: PropTypes.bool,
/** Whether text contains html markup */
hasMarkup: PropTypes.bool,
/** Themes correspond to site color scheme i.e. sass variables */
theme: PropTypes.oneOf(['c-primary', 'c-primary-alt', 'c-highlight', 'c-gray-dark', 'c-error-red', 'c-white']),
/** Disable helptip trigger text */
disabled: PropTypes.bool,
children: PropTypes.node
} : {};
HelpTip.defaultProps = {
hasMarkup: true,
theme: 'c-primary',
bypassMobileStyle: false
};
export default HelpTip;