Co-authored-by: Alireza <ar.sedghi@gmail.com> Co-authored-by: Dan Rukas <dan.rukas@gmail.com> Co-authored-by: Tang Cheng <45505657+tctco@users.noreply.github.com>
This commit is contained in:
44 files changed
+516
-258
No files matched your search
@@ -1,11 +1,20 @@
|
||||
import React, { useState, createContext, useContext, useCallback, useEffect, useMemo } from 'react';
|
||||
import ManagedDialog, { ManagedDialogProps } from './ManagedDialog';
|
||||
import React, {
|
||||
useState,
|
||||
createContext,
|
||||
useContext,
|
||||
useCallback,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
} from 'react';
|
||||
import ManagedDialog, { ManagedDialogProps, ManagedDialogRef } from './ManagedDialog';
|
||||
|
||||
interface DialogContextType {
|
||||
show: (options: ManagedDialogProps) => string;
|
||||
hide: (id: string) => void;
|
||||
hideAll: () => void;
|
||||
isEmpty: () => boolean;
|
||||
updatePosition: (id: string, position: { x: number; y: number }) => void;
|
||||
}
|
||||
|
||||
interface DialogService {
|
||||
@@ -35,6 +44,7 @@ const DialogProvider: React.FC<DialogProviderProps> = ({
|
||||
service = null,
|
||||
}) => {
|
||||
const [dialogs, setDialogs] = useState<(ManagedDialogProps & { id: string })[]>([]);
|
||||
const dialogRefs = useRef<Map<string, ManagedDialogRef>>(new Map());
|
||||
|
||||
const show = useCallback((options: ManagedDialogProps) => {
|
||||
const id = options.id;
|
||||
@@ -44,22 +54,32 @@ const DialogProvider: React.FC<DialogProviderProps> = ({
|
||||
|
||||
const hide = useCallback((id: string) => {
|
||||
setDialogs(prev => prev.filter(dialog => dialog.id !== id));
|
||||
dialogRefs.current.delete(id);
|
||||
}, []);
|
||||
|
||||
const hideAll = useCallback(() => {
|
||||
setDialogs([]);
|
||||
dialogRefs.current.clear();
|
||||
}, []);
|
||||
|
||||
const isEmpty = useCallback(() => dialogs.length === 0, [dialogs]);
|
||||
|
||||
const updatePosition = useCallback((id: string, position: { x: number; y: number }) => {
|
||||
const dialogRef = dialogRefs.current.get(id);
|
||||
if (dialogRef) {
|
||||
dialogRef.updatePosition(position);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const contextValue = useMemo(
|
||||
() => ({
|
||||
show,
|
||||
hide,
|
||||
hideAll,
|
||||
isEmpty,
|
||||
updatePosition,
|
||||
}),
|
||||
[show, hide, hideAll, isEmpty]
|
||||
[show, hide, hideAll, isEmpty, updatePosition]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -76,6 +96,11 @@ const DialogProvider: React.FC<DialogProviderProps> = ({
|
||||
{dialogs.map(dialog => (
|
||||
<RenderedDialog
|
||||
key={dialog.id}
|
||||
ref={(ref: ManagedDialogRef) => {
|
||||
if (ref) {
|
||||
dialogRefs.current.set(dialog.id, ref);
|
||||
}
|
||||
}}
|
||||
onClose={hide}
|
||||
isOpen={true}
|
||||
{...dialog}
|
||||
|
||||
Reference in new issue
Block a user