ui(components): New Dialog and Modal components for ui-next (#4772)
Co-authored-by: sedghi <ar.sedghi@gmail.com>
This commit is contained in:
1 parent
62b4fa3e89
commit
a3b51aa8db
90 files changed
+3459
-1357
No files matched your search
@@ -0,0 +1,89 @@
|
||||
import React, { useState, createContext, useContext, useCallback, useEffect, useMemo } from 'react';
|
||||
import ManagedDialog, { ManagedDialogProps } from './ManagedDialog';
|
||||
|
||||
interface DialogContextType {
|
||||
show: (options: ManagedDialogProps) => string;
|
||||
hide: (id: string) => void;
|
||||
hideAll: () => void;
|
||||
isEmpty: () => boolean;
|
||||
}
|
||||
|
||||
interface DialogService {
|
||||
setServiceImplementation: (implementation: DialogContextType) => void;
|
||||
getCustomComponent?: () => React.ComponentType<ManagedDialogProps> | null;
|
||||
}
|
||||
|
||||
interface DialogProviderProps {
|
||||
children: React.ReactNode;
|
||||
dialog?: React.ComponentType<ManagedDialogProps>;
|
||||
service?: DialogService | null;
|
||||
}
|
||||
|
||||
const DialogContext = createContext<DialogContextType | null>(null);
|
||||
|
||||
export const useDialog = () => {
|
||||
const context = useContext(DialogContext);
|
||||
if (!context) {
|
||||
throw new Error('useDialog must be used within a DialogProvider');
|
||||
}
|
||||
return context;
|
||||
};
|
||||
|
||||
const DialogProvider: React.FC<DialogProviderProps> = ({
|
||||
children,
|
||||
dialog: DialogComponent = ManagedDialog,
|
||||
service = null,
|
||||
}) => {
|
||||
const [dialogs, setDialogs] = useState<(ManagedDialogProps & { id: string })[]>([]);
|
||||
|
||||
const show = useCallback((options: ManagedDialogProps) => {
|
||||
const id = options.id;
|
||||
setDialogs(prev => [...prev, { ...options, id }]);
|
||||
return id;
|
||||
}, []);
|
||||
|
||||
const hide = useCallback((id: string) => {
|
||||
setDialogs(prev => prev.filter(dialog => dialog.id !== id));
|
||||
}, []);
|
||||
|
||||
const hideAll = useCallback(() => {
|
||||
setDialogs([]);
|
||||
}, []);
|
||||
|
||||
const isEmpty = useCallback(() => dialogs.length === 0, [dialogs]);
|
||||
|
||||
const contextValue = useMemo(
|
||||
() => ({
|
||||
show,
|
||||
hide,
|
||||
hideAll,
|
||||
isEmpty,
|
||||
}),
|
||||
[show, hide, hideAll, isEmpty]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (service) {
|
||||
service.setServiceImplementation(contextValue);
|
||||
}
|
||||
}, [service, contextValue]);
|
||||
|
||||
const CustomDialog = service?.getCustomComponent?.();
|
||||
const RenderedDialog = CustomDialog || DialogComponent;
|
||||
|
||||
return (
|
||||
<DialogContext.Provider value={contextValue}>
|
||||
{dialogs.map(dialog => (
|
||||
<RenderedDialog
|
||||
key={dialog.id}
|
||||
onClose={hide}
|
||||
isOpen={true}
|
||||
{...dialog}
|
||||
/>
|
||||
))}
|
||||
{children}
|
||||
</DialogContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export { DialogProvider };
|
||||
@@ -0,0 +1,76 @@
|
||||
import React from 'react';
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '../components/Dialog/Dialog';
|
||||
import { cn } from '../lib/utils';
|
||||
|
||||
export interface ManagedDialogProps {
|
||||
id: string;
|
||||
isOpen?: boolean;
|
||||
title?: string;
|
||||
description?: string;
|
||||
content: React.ComponentType<{ hide?: () => void }>;
|
||||
contentProps?: Record<string, unknown>;
|
||||
isDraggable?: boolean;
|
||||
shouldCloseOnEsc?: boolean;
|
||||
shouldCloseOnOverlayClick?: boolean;
|
||||
defaultPosition?: { x: number; y: number };
|
||||
onClose?: (id: string) => void;
|
||||
unstyled?: boolean;
|
||||
showOverlay?: boolean;
|
||||
containerClassName?: string;
|
||||
}
|
||||
|
||||
const ManagedDialog: React.FC<ManagedDialogProps> = ({
|
||||
id,
|
||||
isOpen,
|
||||
title,
|
||||
content: DialogContentComponent,
|
||||
contentProps,
|
||||
isDraggable,
|
||||
shouldCloseOnEsc = false,
|
||||
shouldCloseOnOverlayClick = false,
|
||||
showOverlay = true,
|
||||
defaultPosition,
|
||||
onClose,
|
||||
unstyled,
|
||||
containerClassName,
|
||||
}) => {
|
||||
return (
|
||||
<Dialog
|
||||
open={isOpen}
|
||||
modal={false} // keep modal behavior off for independent windows
|
||||
onOpenChange={open => {
|
||||
if (!open) {
|
||||
onClose(id);
|
||||
}
|
||||
}}
|
||||
isDraggable={isDraggable}
|
||||
shouldCloseOnEsc={shouldCloseOnEsc}
|
||||
shouldCloseOnOverlayClick={shouldCloseOnOverlayClick}
|
||||
showOverlay={showOverlay}
|
||||
>
|
||||
<DialogContent
|
||||
className={cn(unstyled ? 'p-0' : '', containerClassName)}
|
||||
style={{
|
||||
...(defaultPosition
|
||||
? {
|
||||
position: 'fixed',
|
||||
left: `${defaultPosition.x}px`,
|
||||
top: `${defaultPosition.y}px`,
|
||||
transform: 'translate(0, 0)',
|
||||
margin: 0,
|
||||
animation: 'none',
|
||||
}
|
||||
: {}),
|
||||
}}
|
||||
>
|
||||
{!unstyled && <DialogHeader>{title && <DialogTitle>{title}</DialogTitle>}</DialogHeader>}
|
||||
<DialogContentComponent
|
||||
{...contentProps}
|
||||
hide={() => onClose(id)}
|
||||
/>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
};
|
||||
|
||||
export default ManagedDialog;
|
||||
@@ -0,0 +1,102 @@
|
||||
import React, { useState, createContext, useContext, useCallback, useEffect } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
interface ModalOptions {
|
||||
title?: string;
|
||||
shouldCloseOnEsc?: boolean;
|
||||
content?: React.ComponentType;
|
||||
contentProps?: Record<string, unknown>;
|
||||
containerClassName?: string;
|
||||
}
|
||||
|
||||
interface ModalContextType {
|
||||
show: (options: Partial<ModalOptions>) => void;
|
||||
hide: () => void;
|
||||
containerClassName?: string;
|
||||
}
|
||||
|
||||
interface ModalComponentProps {
|
||||
isOpen: boolean;
|
||||
onClose: () => void;
|
||||
title?: string;
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
const ModalContext = createContext<ModalContextType | null>(null);
|
||||
|
||||
export const useModal = () => {
|
||||
const ctx = useContext(ModalContext);
|
||||
if (!ctx) {
|
||||
throw new Error('useModal must be used within a ModalProvider');
|
||||
}
|
||||
return ctx;
|
||||
};
|
||||
|
||||
const DEFAULT_OPTIONS: ModalOptions = {
|
||||
title: '',
|
||||
shouldCloseOnEsc: true,
|
||||
};
|
||||
|
||||
interface ModalService {
|
||||
setServiceImplementation: (implementation: ModalContextType) => void;
|
||||
getCustomComponent: () => React.ComponentType<ModalComponentProps> | null;
|
||||
}
|
||||
|
||||
interface ModalProviderProps {
|
||||
children: React.ReactNode;
|
||||
modal: React.ComponentType<ModalComponentProps>;
|
||||
service?: ModalService | null;
|
||||
}
|
||||
|
||||
const ModalProvider: React.FC<ModalProviderProps> = ({
|
||||
children,
|
||||
modal: ModalComponent,
|
||||
service = null,
|
||||
}) => {
|
||||
const { t } = useTranslation('Modals');
|
||||
const [options, setOptions] = useState<ModalOptions>(DEFAULT_OPTIONS);
|
||||
|
||||
const ModalContent = options.content;
|
||||
|
||||
const show = useCallback((props: Partial<ModalOptions>) => {
|
||||
setOptions(prev => ({ ...prev, ...props }));
|
||||
}, []);
|
||||
|
||||
const hide = useCallback(() => {
|
||||
setOptions(DEFAULT_OPTIONS);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (service) {
|
||||
service.setServiceImplementation({ show, hide });
|
||||
}
|
||||
}, [hide, service, show]);
|
||||
|
||||
const { title } = options;
|
||||
|
||||
const CustomModal = service?.getCustomComponent();
|
||||
const RenderedModal = CustomModal || ModalComponent;
|
||||
|
||||
return (
|
||||
<ModalContext.Provider value={{ show, hide }}>
|
||||
{ModalContent && (
|
||||
<RenderedModal
|
||||
isOpen={true}
|
||||
onClose={hide}
|
||||
title={t(title)}
|
||||
{...options}
|
||||
>
|
||||
<ModalContent
|
||||
{...options.contentProps}
|
||||
show={show}
|
||||
hide={hide}
|
||||
/>
|
||||
</RenderedModal>
|
||||
)}
|
||||
{children}
|
||||
</ModalContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export { ModalProvider };
|
||||
export const ModalConsumer = ModalContext.Consumer;
|
||||
@@ -1,7 +1,13 @@
|
||||
import NotificationProvider, { useNotification } from './NotificationProvider';
|
||||
import { ViewportGridContext, ViewportGridProvider, useViewportGrid } from './ViewportGridProvider';
|
||||
import { ToolboxProvider, useToolbox } from './ToolboxContext';
|
||||
import { ModalProvider, useModal } from './ModalProvider';
|
||||
import { DialogProvider, useDialog } from './DialogProvider';
|
||||
import ManagedDialog from './ManagedDialog';
|
||||
|
||||
export { useNotification, NotificationProvider };
|
||||
export { ViewportGridContext, ViewportGridProvider, useViewportGrid };
|
||||
export { ToolboxProvider, useToolbox };
|
||||
export { ModalProvider, useModal };
|
||||
export { DialogProvider, useDialog };
|
||||
export { ManagedDialog };
|
||||
Reference in new issue
Block a user