Skip to content

Commit 5990788

Browse files
authored
fix: component props bug (#29)
* fix: ignore checkbox input native size prop * perf: prevent layout calc for scrollbar size 0 * fix: as component function types
1 parent e0b3381 commit 5990788

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

src/components/as.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { ElementType, forwardRef, ReactElement } from "react";
2-
import { ComponentProps, AsProp, RefOfType } from "./types";
2+
import { AsInProps, AsOutProps, RefOfType } from "./types";
33

4-
export const as = <T extends ElementType, ExtraProps = unknown>(
4+
export const as = <DefaultType extends ElementType, ExtraProps = object>(
55
fc: (
6-
props: Omit<ComponentProps<T>, keyof ExtraProps | keyof AsProp<T>> & AsProp<T> & ExtraProps,
7-
ref: RefOfType<T>
8-
) => ReactElement
6+
props: AsInProps<DefaultType, ExtraProps>,
7+
ref: RefOfType<DefaultType>
8+
) => ReactElement | null
99
) =>
10-
forwardRef(fc) as unknown as <E extends ElementType = T>(
11-
props: Omit<ComponentProps<E>, keyof ExtraProps | keyof AsProp<E>> & AsProp<E> & ExtraProps
12-
) => ReactElement;
10+
forwardRef(fc) as unknown as <T extends ElementType = DefaultType>(
11+
props: AsOutProps<T, ExtraProps>
12+
) => ReturnType<typeof fc>;

src/components/checkbox/Checkbox.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import React, { AllHTMLAttributes, forwardRef } from "react";
44
import { Icon, Icons } from "../icon";
55
import * as css from "./Checkbox.css";
66

7-
type CheckboxProps = Omit<AllHTMLAttributes<HTMLInputElement>, "children" | "onChange" | "type"> &
7+
type CheckboxProps = Omit<
8+
AllHTMLAttributes<HTMLInputElement>,
9+
"children" | "onChange" | "type" | "size"
10+
> &
811
css.CheckboxVariants & {
912
defaultChecked?: boolean;
1013
checked?: boolean;

src/components/scroll/Scroll.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ export const Scroll = as<"div", css.ScrollVariants>(
1313
useLayoutEffect(() => {
1414
if (scrollLocalRef.current) {
1515
const $scroll = scrollLocalRef.current;
16-
const xScrollbarWidth = $scroll.offsetHeight - $scroll.clientHeight;
17-
const yScrollbarWidth = $scroll.offsetWidth - $scroll.clientWidth;
16+
if (size === "0") {
17+
$scroll.setAttribute("data-x-scrollbar-width", "0");
18+
$scroll.setAttribute("data-y-scrollbar-width", "0");
19+
} else {
20+
const xScrollbarWidth = $scroll.offsetHeight - $scroll.clientHeight;
21+
const yScrollbarWidth = $scroll.offsetWidth - $scroll.clientWidth;
1822

19-
$scroll.setAttribute("data-x-scrollbar-width", `${xScrollbarWidth}`);
20-
$scroll.setAttribute("data-y-scrollbar-width", `${yScrollbarWidth}`);
23+
$scroll.setAttribute("data-x-scrollbar-width", `${xScrollbarWidth}`);
24+
$scroll.setAttribute("data-y-scrollbar-width", `${yScrollbarWidth}`);
25+
}
2126
}
2227
}, [size]);
2328

src/components/types.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { ComponentPropsWithRef, ElementType, PropsWithChildren } from "react";
1+
import {
2+
ComponentPropsWithoutRef,
3+
ComponentPropsWithRef,
4+
ElementType,
5+
PropsWithChildren,
6+
} from "react";
27

38
export type MainColor = "Primary" | "Secondary" | "Success" | "Warning" | "Critical";
49

@@ -18,4 +23,16 @@ export type AsProp<E extends ElementType> = {
1823
as?: E;
1924
};
2025

21-
export type ComponentProps<E extends ElementType> = PropsWithChildren<ComponentPropsWithRef<E>>;
26+
export type AsInProps<E extends ElementType, ExtraProps = object> = Omit<
27+
PropsWithChildren<ComponentPropsWithoutRef<E>>,
28+
keyof ExtraProps | keyof AsProp<ElementType>
29+
> &
30+
ExtraProps &
31+
AsProp<E>;
32+
33+
export type AsOutProps<E extends ElementType, ExtraProps = object> = Omit<
34+
PropsWithChildren<ComponentPropsWithRef<E>>,
35+
keyof ExtraProps | keyof AsProp<ElementType>
36+
> &
37+
ExtraProps &
38+
AsProp<E>;

0 commit comments

Comments
 (0)