UNPKG

@navinc/base-react-components

Version:
212 lines (211 loc) 11.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Card = exports.Footer = exports.Kebab = exports.NextButton = exports.BackButton = exports.Undo = exports.DismissCard = exports.CheckMark = exports.CloseIcon = exports.StyledCard = exports.CardBody = exports.Title = exports.CardHeader = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const styled_components_1 = __importDefault(require("styled-components")); const lodash_debounce_1 = __importDefault(require("lodash.debounce")); const prop_types_1 = __importDefault(require("prop-types")); const button_js_1 = __importDefault(require("../../button.js")); const copy_js_1 = __importDefault(require("../../copy.js")); const header_js_1 = __importDefault(require("../../header.js")); const kebab_menu_js_1 = __importDefault(require("./parts/kebab-menu.js")); const depricationWarning = (isInUse, warning) => { isInUse && console.warn(warning); }; /* Marks the bottom of the card's content. */ const WayMark = styled_components_1.default.hr.withConfig({ displayName: "brc-sc-WayMark", componentId: "brc-sc-1mybk47" }) ` border: none; margin: 0; height: 0; `; WayMark.displayName = 'WayMark'; const CARD_BODY_PADDING_BOTTOM = 32; exports.CardHeader = (0, styled_components_1.default)(copy_js_1.default).attrs(() => ({ size: 'sm' })).withConfig({ displayName: "brc-sc-CardHeader", componentId: "brc-sc-1g3db6f" }) ` display: flex; flex-flow: row nowrap; justify-content: space-between; align-items: center; min-height: 24px; padding-bottom: 16px; color: ${({ theme }) => theme.neutral400}; `; exports.CardHeader.displayName = 'CardHeader'; exports.Title = (0, styled_components_1.default)(header_js_1.default).attrs(() => ({ size: 'md' })).withConfig({ displayName: "brc-sc-Title", componentId: "brc-sc-f96nx6" }) ` padding-bottom: 16px; `; exports.Title.displayName = 'Title'; exports.CardBody = styled_components_1.default.div.withConfig({ displayName: "brc-sc-CardBody", componentId: "brc-sc-1nb3yec" }) ` position: relative; /* required to get offsetTop of Waymark from Card */ padding-bottom: ${CARD_BODY_PADDING_BOTTOM}px; ${({ height, shouldNotAnimate }) => shouldNotAnimate ? '' : 'transition: height 250ms ease-in-out;' + `height: ${height}px;`} overflow: hidden; ${copy_js_1.default} { max-width: 520px; } `; exports.CardBody.displayName = 'CardBody'; exports.StyledCard = styled_components_1.default.div.withConfig({ displayName: "brc-sc-StyledCard", componentId: "brc-sc-pqvnml" }) ` position: relative; flex: 1 1 100%; padding: 24px; border-radius: 4px; background-color: ${({ theme }) => theme.white}; overflow: hidden; box-shadow: 0 0 3px 0 ${({ theme }) => theme.neutral300}, 0 1px 2px 0 ${({ theme }) => theme.neutral300}; ${({ isCardHidden, theme }) => isCardHidden && ` & > ${exports.CardHeader} { border-top: 4px solid ${theme.good}; } `}; `; exports.StyledCard.displayName = 'StyledCard'; exports.CloseIcon = styled_components_1.default.img.withConfig({ displayName: "brc-sc-CloseIcon", componentId: "brc-sc-1158fbm" }) ` padding-left: 8px; `; exports.CloseIcon.displayName = 'CloseIcon'; exports.CheckMark = styled_components_1.default.img.withConfig({ displayName: "brc-sc-CheckMark", componentId: "brc-sc-hullcb" }) ` padding-right: 8px; `; exports.CheckMark.displayName = 'CheckMark'; exports.DismissCard = styled_components_1.default.div.withConfig({ displayName: "brc-sc-DismissCard", componentId: "brc-sc-74ktuf" }) ` position: absolute; right: 8px; display: inline-block; height: 100%; padding: 16px; border-left: 2px solid ${({ theme }) => theme.neutral300}; cursor: pointer; `; exports.DismissCard.displayName = 'DismissCard'; exports.Undo = (0, styled_components_1.default)(copy_js_1.default).withConfig({ displayName: "brc-sc-Undo", componentId: "brc-sc-1xe53pa" }) ` cursor: pointer; font-weight: bold; margin-right: 64px; `; exports.Undo.displayName = 'Undo'; exports.BackButton = (0, styled_components_1.default)(button_js_1.default).withConfig({ displayName: "brc-sc-BackButton", componentId: "brc-sc-1w1q2nw" }) ` padding: 8px; margin-right: auto; `; exports.BackButton.displayName = 'BackButton'; exports.NextButton = (0, styled_components_1.default)(button_js_1.default).withConfig({ displayName: "brc-sc-NextButton", componentId: "brc-sc-175zu7w" }) ` &[disabled] { background-color: unset; border-color: transparent; color: ${({ theme }) => theme.neutral400}; cursor: default; } `; exports.NextButton.displayName = 'NextButton'; exports.Kebab = styled_components_1.default.img.attrs(() => ({ src: 'https://dxkdvuv3hanyu.cloudfront.net/design-assets/icons/kebab-menu.svg', })).withConfig({ displayName: "brc-sc-Kebab", componentId: "brc-sc-15rz6dx" }) ` position: absolute; top: 16px; right: 8px; padding: 8px; cursor: pointer; `; exports.Kebab.displayName = 'Kebab'; exports.Footer = styled_components_1.default.div.withConfig({ displayName: "brc-sc-Footer", componentId: "brc-sc-1vr9y3j" }) ` display: flex; align-items: center; justify-content: flex-end; padding-top: 16px; border-top: 1px solid ${({ theme }) => theme.neutral300}; `; exports.Footer.displayName = 'Footer'; class Card extends react_1.Component { constructor() { super(...arguments); this.state = { isKebabMenuVisible: false, isCardHidden: false, isCardDismissed: false, contentHeight: 0, }; this.toggleKebabMenu = (event) => { event.stopPropagation(); this._mounted && this.setState({ isKebabMenuVisible: !this.state.isKebabMenuVisible }); }; this.togglehideCard = (event) => { event.stopPropagation(); if (this.state.isKebabMenuVisible) { this.toggleKebabMenu(event); } this._mounted && this.setState(({ isCardHidden }) => ({ isCardHidden: !isCardHidden })); }; this.dismissCard = (event) => { this._mounted && this.setState(() => ({ isCardDismissed: true })); }; this.setRef = (name) => (ref) => { this[name] = ref; }; this.setHeight = (0, lodash_debounce_1.default)(() => { if (this._mounted && !this.props.shouldNotAnimate) { this.setState(() => { var _a, _b; return ({ contentHeight: ((_b = (_a = this.wayMark) === null || _a === void 0 ? void 0 : _a.offsetTop) !== null && _b !== void 0 ? _b : 0) + CARD_BODY_PADDING_BOTTOM, }); }); } }, 250, { leading: true }); } render() { const { buttonCopy, buttonLabel = buttonCopy, children, className, labelCopy, cardLabel = labelCopy, hasAsyncNextButton, isLoading, onBack, onNext, onHide = () => { }, onToggleKebab = () => { }, titleCopy, title = titleCopy, buttonDisabled, buttonForm, kebabMenuItems = [], hasFooter = !!(onNext || onBack || buttonForm), shouldNotAnimate, } = this.props; const { isKebabMenuVisible, isCardHidden, isCardDismissed } = this.state; if (isCardDismissed) return null; return ((0, jsx_runtime_1.jsxs)(exports.StyledCard, Object.assign({ className: className, isCardHidden: isCardHidden }, { children: [cardLabel && (0, jsx_runtime_1.jsx)(exports.CardHeader, { children: cardLabel }), !!kebabMenuItems.length && ((0, jsx_runtime_1.jsx)(exports.Kebab, { "data-testid": "deprecated-card:kebab", onClick: (event) => { this.toggleKebabMenu(event); onToggleKebab(event); } })), isKebabMenuVisible && ((0, jsx_runtime_1.jsx)(kebab_menu_js_1.default, { kebabMenuItems: kebabMenuItems, onHide: onHide, togglehideCard: this.togglehideCard, toggleKebabMenu: this.toggleKebabMenu })), title && (0, jsx_runtime_1.jsx)(exports.Title, { children: title }), (0, jsx_runtime_1.jsxs)(exports.CardBody, Object.assign({ height: this.state.contentHeight, shouldNotAnimate: shouldNotAnimate }, { children: [children, (0, jsx_runtime_1.jsx)(WayMark, { ref: this.setRef('wayMark') })] })), hasFooter && ((0, jsx_runtime_1.jsxs)(exports.Footer, { children: [onBack && ((0, jsx_runtime_1.jsx)(exports.BackButton, Object.assign({ onClick: onBack, variation: "noOutline" }, { children: (0, jsx_runtime_1.jsx)("img", { src: "https://dxkdvuv3hanyu.cloudfront.net/icons/card_back_arrow.svg", alt: "back arrow" }) }))), !hasAsyncNextButton && onNext && ((0, jsx_runtime_1.jsx)(exports.NextButton, Object.assign({ isLoading: isLoading, onClick: onNext, size: "cardButton", variation: "noOutline", disabled: buttonDisabled, form: buttonForm }, { children: buttonLabel }))), hasAsyncNextButton && onNext && ((0, jsx_runtime_1.jsx)(button_js_1.default, Object.assign({ isLoading: isLoading, onClick: onNext }, { children: buttonLabel })))] }))] }))); } componentDidMount() { this._mounted = true; this.setHeight(); window.addEventListener('resize', this.setHeight); depricationWarning(this.props.buttonCopy, 'The `buttonCopy` property on the `Card` component has been depricated and renamed to `buttonLabel`'); depricationWarning(this.props.titleCopy, 'The `titleCopy` property on the `Card` component has been depricated and renamed to `title`'); depricationWarning(this.props.controlledHeight, 'The `controlledHeight` property on the `Card` component has been depricated. All card components have a transition animation when heights change. The consumer of the Card component does not need to set explicit heights.'); depricationWarning(this.props.labelCopy, 'The `labelCopy` property on the `Card` component has been depricated and renamed `cardLabel`.'); } componentDidUpdate() { var _a, _b; if (this.state.contentHeight !== ((_b = (_a = this.wayMark) === null || _a === void 0 ? void 0 : _a.offsetTop) !== null && _b !== void 0 ? _b : 0)) { this.setHeight(); } } componentWillUnmount() { this._mounted = false; window.removeEventListener('resize', this.setHeight); } } exports.Card = Card; Card.propTypes = { buttonCopy: prop_types_1.default.string, children: prop_types_1.default.node, className: prop_types_1.default.string, hasFooter: prop_types_1.default.bool, labelCopy: prop_types_1.default.string, onBack: prop_types_1.default.func, onDismiss: prop_types_1.default.func, onNext: prop_types_1.default.func, onHide: prop_types_1.default.func, onToggleKebab: prop_types_1.default.func, onUndo: prop_types_1.default.func, titleCopy: prop_types_1.default.string, buttonDisabled: prop_types_1.default.bool, buttonForm: prop_types_1.default.string, controlledHeight: prop_types_1.default.bool, kebabMenuItems: prop_types_1.default.array, shouldNotAnimate: prop_types_1.default.bool, }; exports.default = (0, styled_components_1.default)(Card).withConfig({ componentId: "brc-sc-1dkesig" }) ``; //# sourceMappingURL=index.js.map