Skip to content

Commit 7a48b4b

Browse files
fix: 兼容低版本react 采用显式forward 船惨
1 parent 9143a1f commit 7a48b4b

File tree

10 files changed

+210
-65
lines changed

10 files changed

+210
-65
lines changed

apps/kk-adapt-app/src/components/ui/button.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,31 @@ const buttonVariants = cva(
3636
}
3737
);
3838

39-
function Button({
39+
const Button = React.forwardRef<
40+
HTMLButtonElement,
41+
React.ComponentProps<'button'> &
42+
VariantProps<typeof buttonVariants> & {
43+
asChild?: boolean;
44+
}
45+
>(({
4046
asChild = false,
4147
className,
4248
size,
4349
variant,
4450
...props
45-
}: React.ComponentProps<'button'> &
46-
VariantProps<typeof buttonVariants> & {
47-
asChild?: boolean;
48-
}) {
51+
}, ref) => {
4952
const Comp = asChild ? Slot : 'button';
5053

5154
return (
5255
<Comp
56+
ref={ref}
5357
className={cn(buttonVariants({ className, size, variant }))}
5458
data-slot="button"
5559
{...props}
5660
/>
5761
);
58-
}
62+
});
63+
64+
Button.displayName = 'Button';
5965

6066
export { Button, buttonVariants };

apps/kk-adapt-app/src/components/ui/checkbox.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import { CheckIcon } from 'lucide-react';
77

88
import { cn } from '@/lib/utils';
99

10-
function Checkbox({
10+
const Checkbox = React.forwardRef<
11+
React.ElementRef<typeof CheckboxPrimitive.Root>,
12+
React.ComponentProps<typeof CheckboxPrimitive.Root>
13+
>(({
1114
className,
1215
...props
13-
}: React.ComponentProps<typeof CheckboxPrimitive.Root>) {
16+
}, ref) => {
1417
return (
1518
<CheckboxPrimitive.Root
19+
ref={ref}
1620
className={cn(
1721
'peer size-4 shrink-0 rounded-[4px] border border-input shadow-xs transition-shadow outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 data-[state=checked]:border-primary data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground dark:bg-input/30 dark:aria-invalid:ring-destructive/40 dark:data-[state=checked]:bg-primary',
1822
className
@@ -28,6 +32,8 @@ function Checkbox({
2832
</CheckboxPrimitive.Indicator>
2933
</CheckboxPrimitive.Root>
3034
);
31-
}
35+
});
36+
37+
Checkbox.displayName = 'Checkbox';
3238

3339
export { Checkbox };

apps/kk-adapt-app/src/components/ui/context-menu.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@ function ContextMenu({
1313
return <ContextMenuPrimitive.Root data-slot="context-menu" {...props} />;
1414
}
1515

16-
function ContextMenuTrigger({
17-
...props
18-
}: React.ComponentProps<typeof ContextMenuPrimitive.Trigger>) {
16+
const ContextMenuTrigger = React.forwardRef<
17+
React.ElementRef<typeof ContextMenuPrimitive.Trigger>,
18+
React.ComponentProps<typeof ContextMenuPrimitive.Trigger>
19+
>(({ ...props }, ref) => {
1920
return (
20-
<ContextMenuPrimitive.Trigger data-slot="context-menu-trigger" {...props} />
21+
<ContextMenuPrimitive.Trigger
22+
ref={ref}
23+
data-slot="context-menu-trigger"
24+
{...props}
25+
/>
2126
);
22-
}
27+
});
28+
29+
ContextMenuTrigger.displayName = 'ContextMenuTrigger';
2330

2431
function ContextMenuGroup({
2532
...props

apps/kk-adapt-app/src/components/ui/dropdown-menu.tsx

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,43 @@ function DropdownMenuPortal({
2121
);
2222
}
2323

24-
function DropdownMenuTrigger({
25-
...props
26-
}: React.ComponentProps<typeof DropdownMenuPrimitive.Trigger>) {
24+
const DropdownMenuTrigger = React.forwardRef<
25+
React.ElementRef<typeof DropdownMenuPrimitive.Trigger>,
26+
React.ComponentProps<typeof DropdownMenuPrimitive.Trigger>
27+
>(({ ...props }, ref) => {
28+
2729
return (
2830
<DropdownMenuPrimitive.Trigger
31+
ref={ref}
2932
data-slot="dropdown-menu-trigger"
3033
{...props}
3134
/>
3235
);
33-
}
36+
});
37+
38+
DropdownMenuTrigger.displayName = 'DropdownMenuTrigger';
3439

35-
function DropdownMenuContent({
40+
const DropdownMenuContent = React.forwardRef<
41+
React.ElementRef<typeof DropdownMenuPrimitive.Content>,
42+
React.ComponentProps<typeof DropdownMenuPrimitive.Content>
43+
>(({
3644
className,
3745
sideOffset = 4,
3846
...props
39-
}: React.ComponentProps<typeof DropdownMenuPrimitive.Content>) {
47+
}, ref) => {
48+
49+
const contentRef = React.useCallback((node: HTMLDivElement | null) => {
50+
if (typeof ref === 'function') {
51+
ref(node);
52+
} else if (ref) {
53+
ref.current = node;
54+
}
55+
}, [ref]);
56+
4057
return (
4158
<DropdownMenuPrimitive.Portal>
4259
<DropdownMenuPrimitive.Content
60+
ref={contentRef}
4361
className={cn(
4462
'z-50 max-h-(--radix-dropdown-menu-content-available-height) min-w-[8rem] origin-(--radix-dropdown-menu-content-transform-origin) overflow-x-hidden overflow-y-auto rounded-md border bg-popover p-1 text-popover-foreground shadow-md data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95',
4563
className
@@ -50,7 +68,9 @@ function DropdownMenuContent({
5068
/>
5169
</DropdownMenuPrimitive.Portal>
5270
);
53-
}
71+
});
72+
73+
DropdownMenuContent.displayName = 'DropdownMenuContent';
5474

5575
function DropdownMenuGroup({
5676
...props

apps/kk-adapt-app/src/components/ui/input.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import * as React from 'react';
22

33
import { cn } from '@/lib/utils';
44

5-
function Input({ className, type, ...props }: React.ComponentProps<'input'>) {
5+
const Input = React.forwardRef<
6+
HTMLInputElement,
7+
React.ComponentProps<'input'>
8+
>(({ className, type, ...props }, ref) => {
69
return (
710
<input
11+
ref={ref}
812
className={cn(
913
'flex h-9 w-full min-w-0 rounded-md border border-input bg-transparent px-3 py-1 text-base shadow-xs transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 md:text-sm dark:bg-input/30',
1014
'focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50',
@@ -16,6 +20,8 @@ function Input({ className, type, ...props }: React.ComponentProps<'input'>) {
1620
{...props}
1721
/>
1822
);
19-
}
23+
});
24+
25+
Input.displayName = 'Input';
2026

2127
export { Input };

apps/kk-adapt-app/src/components/ui/separator.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ import * as SeparatorPrimitive from '@radix-ui/react-separator';
66

77
import { cn } from '@/lib/utils';
88

9-
function Separator({
9+
const Separator = React.forwardRef<
10+
React.ElementRef<typeof SeparatorPrimitive.Root>,
11+
React.ComponentProps<typeof SeparatorPrimitive.Root>
12+
>(({
1013
className,
1114
decorative = true,
1215
orientation = 'horizontal',
1316
...props
14-
}: React.ComponentProps<typeof SeparatorPrimitive.Root>) {
17+
}, ref) => {
1518
return (
1619
<SeparatorPrimitive.Root
20+
ref={ref}
1721
orientation={orientation}
1822
className={cn(
1923
'shrink-0 bg-border data-[orientation=horizontal]:h-px data-[orientation=horizontal]:w-full data-[orientation=vertical]:h-full data-[orientation=vertical]:w-px',
@@ -24,6 +28,8 @@ function Separator({
2428
{...props}
2529
/>
2630
);
27-
}
31+
});
32+
33+
Separator.displayName = 'Separator';
2834

2935
export { Separator };
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1+
import * as React from 'react';
2+
13
import { cn } from '@/lib/utils';
24

3-
function Skeleton({ className, ...props }: React.ComponentProps<'div'>) {
5+
const Skeleton = React.forwardRef<
6+
HTMLDivElement,
7+
React.ComponentProps<'div'>
8+
>(({ className, ...props }, ref) => {
49
return (
510
<div
11+
ref={ref}
612
className={cn('animate-pulse rounded-md bg-accent', className)}
713
data-slot="skeleton"
814
{...props}
915
/>
1016
);
11-
}
17+
});
18+
19+
Skeleton.displayName = 'Skeleton';
1220

1321
export { Skeleton };

apps/kk-adapt-app/src/components/ui/toggle.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,26 @@ const toggleVariants = cva(
3232
}
3333
);
3434

35-
function Toggle({
35+
const Toggle = React.forwardRef<
36+
React.ElementRef<typeof TogglePrimitive.Root>,
37+
React.ComponentProps<typeof TogglePrimitive.Root> &
38+
VariantProps<typeof toggleVariants>
39+
>(({
3640
className,
3741
size,
3842
variant,
3943
...props
40-
}: React.ComponentProps<typeof TogglePrimitive.Root> &
41-
VariantProps<typeof toggleVariants>) {
44+
}, ref) => {
4245
return (
4346
<TogglePrimitive.Root
47+
ref={ref}
4448
className={cn(toggleVariants({ className, size, variant }))}
4549
data-slot="toggle"
4650
{...props}
4751
/>
4852
);
49-
}
53+
});
54+
55+
Toggle.displayName = 'Toggle';
5056

5157
export { Toggle, toggleVariants };

apps/kk-adapt-app/src/registry/ui/floating-toolbar.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ import { cn } from '@/lib/utils';
2121

2222
import { Toolbar } from './toolbar';
2323

24-
export function FloatingToolbar({
24+
export const FloatingToolbar = React.forwardRef<
25+
HTMLDivElement,
26+
React.ComponentProps<typeof Toolbar> & {
27+
state?: FloatingToolbarState;
28+
}
29+
>(({
2530
children,
2631
className,
2732
state,
2833
...props
29-
}: React.ComponentProps<typeof Toolbar> & {
30-
state?: FloatingToolbarState;
31-
}) {
34+
}, ref) => {
3235
const editorId = useEditorId();
3336
const focusedEditorId = useEventEditorValue('focus');
3437
const isFloatingLinkOpen = !!usePluginOption({ key: KEYS.link }, 'mode');
@@ -64,7 +67,7 @@ export function FloatingToolbar({
6467
ref: floatingRef,
6568
} = useFloatingToolbar(floatingToolbarState);
6669

67-
const ref = useComposedRef<HTMLDivElement>(props.ref, floatingRef);
70+
const composedRef = useComposedRef(ref, floatingRef);
6871

6972
if (hidden) return null;
7073

@@ -73,7 +76,7 @@ export function FloatingToolbar({
7376
<Toolbar
7477
{...props}
7578
{...rootProps}
76-
ref={ref}
79+
ref={composedRef}
7780
className={cn(
7881
'absolute z-50 scrollbar-hide overflow-x-auto rounded-md border bg-popover p-1 whitespace-nowrap opacity-100 shadow-md print:hidden',
7982
'max-w-[80vw]',
@@ -84,4 +87,6 @@ export function FloatingToolbar({
8487
</Toolbar>
8588
</div>
8689
);
87-
}
90+
});
91+
92+
FloatingToolbar.displayName = 'FloatingToolbar';

0 commit comments

Comments
 (0)