Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {
Children,
type PropsWithChildren,
type ReactElement,
isValidElement,
} from "react";
import { Popover, type PopoverProps } from "metabase/ui";
type StepValue = string | number;
type StepProps = PropsWithChildren<{
value: StepValue;
}>;
const Step = ({ children }: PropsWithChildren<StepProps>) => {
return children;
};
const Target = ({ children }: PropsWithChildren) => {
return <>{children}</>;
};
type MultiStepPopoverProps = PropsWithChildren<{
currentStep: StepValue;
}>;
const MultiStepPopoverContent = ({
currentStep,
children,
...popoverProps
}: MultiStepPopoverProps & PopoverProps) => {
const findChild = <T extends ReactElement>(
predicate: (child: ReactElement) => child is T,
): T | null => {
const child = Children.toArray(children).find(
(child): child is T => isValidElement(child) && predicate(child),
);
return child || null;
};
const currentStepContent = findChild(
(child): child is ReactElement<StepProps> =>
child.type === Step && child.props.value === currentStep,
)?.props.children;
const targetElement = findChild(
(child): child is ReactElement => child.type === Target,
)?.props.children;
return (
<Popover {...popoverProps}>
<Popover.Target>{targetElement}</Popover.Target>;
<Popover.Dropdown>{currentStepContent}</Popover.Dropdown>;
</Popover>
);
};
export const MultiStepPopover = Object.assign(MultiStepPopoverContent, {
Step,
Target,
});