@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
146 lines • 8.38 kB
JavaScript
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import classNames from 'classnames';
import React from 'react';
import { CleanIconButton } from '../clean-icon-button';
import { FilledButton } from '../filled-button';
import { ExclamationCircleIconOutline, ExclamationTriangleIconOutline, InformationCircleIconOutline, XMarkIconOutline, } from '../icons';
import { Modal, ModalCloseReason } from '../modal';
import { OutlinedButton } from '../outlined-button';
import { TextButton } from '../text-button';
import { Typography } from '../typography';
import { DialogContext, useDialogContext } from './dialog-context';
import { DialogCloseReason, } from './dialog-types';
const convertCloseReason = (reason) => {
const dialogReason = reason;
if (Object.values(DialogCloseReason).includes(dialogReason)) {
return dialogReason;
}
throw new Error(`Unknown reason '${reason}' received from popup`);
};
/* Stroke width on regular icons is 1.5px */
const typeIcons = {
danger: _jsx(ExclamationCircleIconOutline, { htmlAttributes: { strokeWidth: 3 } }),
info: _jsx(InformationCircleIconOutline, { htmlAttributes: { strokeWidth: 3 } }),
warning: (_jsx(ExclamationTriangleIconOutline, { htmlAttributes: { strokeWidth: 3 } })),
};
const DialogComponent = (_a) => {
var { isOpen, onClose, children, variant, size, container, rootProps, hasDisabledCloseButton = false, htmlAttributes, className, style, ref } = _a, restProps = __rest(_a, ["isOpen", "onClose", "children", "variant", "size", "container", "rootProps", "hasDisabledCloseButton", "htmlAttributes", "className", "style", "ref"]);
const handleCloseClick = (e) => {
e.preventDefault();
onClose === null || onClose === void 0 ? void 0 : onClose(e, DialogCloseReason.CLOSE_BUTTON_CLICK);
};
const handleClose = (e, reason) => {
// Dialog should not be closed when backdrop is clicked
if (reason === ModalCloseReason.BACKDROP_CLICK) {
return;
}
onClose === null || onClose === void 0 ? void 0 : onClose(e, reason ? convertCloseReason(reason) : undefined);
};
if (!isOpen) {
return null;
}
const classes = classNames('ndl-dialog', className, {
'ndl-with-close-button': !hasDisabledCloseButton,
'ndl-with-icon': !!variant,
});
return (_jsx(Modal, Object.assign({ isOpen: isOpen, onClose: handleClose, container: container, rootProps: rootProps, size: size, htmlAttributes: htmlAttributes, className: classes, style: style, ref: ref }, restProps, { children: _jsxs(DialogContext.Provider, { value: { variant }, children: [!hasDisabledCloseButton && (_jsx(CleanIconButton, { size: "medium", className: "ndl-dialog-close", onClick: handleCloseClick, description: "Close", htmlAttributes: {
'data-testid': 'ndl-dialog-close',
role: 'button',
}, children: _jsx(XMarkIconOutline, {}) })), _jsxs("div", { className: "ndl-dialog-content-wrapper", children: [variant && (_jsx("div", { className: `ndl-dialog-type-icon ndl-${variant}`, "data-testid": "ndl-dialog-type-icon", children: typeIcons[variant] })), _jsx("div", { className: "n-flex n-flex-col n-flex-1 n-w-full", children: children })] })] }) })));
};
const Actions = (_a) => {
var { as, children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["as", "children", "className", "style", "htmlAttributes", "ref"]);
const { variant } = useDialogContext();
const classes = classNames('ndl-dialog-actions', className);
const Component = as !== null && as !== void 0 ? as : 'div';
let childrenToRender = children;
// For danger dialogs, we want to convert primary buttons to danger buttons by default
if (variant === 'danger') {
childrenToRender = React.Children.map(children, (element) => {
if (!React.isValidElement(element)) {
return;
}
if (element.type === OutlinedButton ||
element.type === FilledButton ||
element.type === TextButton) {
const buttonElement = element;
if (buttonElement.props.variant === 'primary' ||
buttonElement.props.variant === undefined) {
return React.cloneElement(buttonElement, Object.assign(Object.assign({}, buttonElement.props), { variant: 'danger' }));
}
}
return element;
});
}
return (_jsx(Component, Object.assign({ className: classes, style: style, ref: ref }, restProps, htmlAttributes, { children: childrenToRender })));
};
const Header = (_a) => {
var { as, children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["as", "children", "className", "style", "htmlAttributes", "ref"]);
const classes = classNames('ndl-dialog-header', className);
const Component = as !== null && as !== void 0 ? as : 'span';
return (_jsx(Typography, Object.assign({ as: Component, variant: "title-2", className: classes, style: style, ref: ref }, restProps, htmlAttributes, { children: children })));
};
const Subtitle = (_a) => {
var { as, children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["as", "children", "className", "style", "htmlAttributes", "ref"]);
const classes = classNames('ndl-dialog-subtitle', className);
const Component = as !== null && as !== void 0 ? as : 'div';
return (_jsx(Component, Object.assign({ className: classes, style: style, ref: ref }, restProps, htmlAttributes, { children: children })));
};
const Description = (_a) => {
var { as, children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["as", "children", "className", "style", "htmlAttributes", "ref"]);
const classes = classNames('ndl-dialog-description', className);
const Component = as !== null && as !== void 0 ? as : 'div';
return (_jsx(Component, Object.assign({ className: classes, style: style, ref: ref }, restProps, htmlAttributes, { children: children })));
};
const Content = (_a) => {
var { as, children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["as", "children", "className", "style", "htmlAttributes", "ref"]);
const classes = classNames('ndl-dialog-content', className);
const Component = as !== null && as !== void 0 ? as : 'div';
return (_jsx(Component, Object.assign({ className: classes, style: style, ref: ref }, restProps, htmlAttributes, { children: children })));
};
const Image = (_a) => {
var { src, alt, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["src", "alt", "className", "style", "htmlAttributes", "ref"]);
const classes = classNames('ndl-dialog-image', className);
return (_jsx("img", Object.assign({ src: src, alt: alt, className: classes, style: style, ref: ref }, restProps, htmlAttributes)));
};
const Dialog = Object.assign(DialogComponent, {
Actions,
Content,
Description,
Header,
Image,
Subtitle,
});
export { Dialog };
//# sourceMappingURL=Dialog.js.map