Skip to content

Commit

Permalink
fix: component props bug (#29)
Browse files Browse the repository at this point in the history
* fix: ignore checkbox input native size prop

* perf: prevent layout calc for scrollbar size 0

* fix: as component function types
  • Loading branch information
ajbura authored Dec 28, 2023
1 parent e0b3381 commit 5990788
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
16 changes: 8 additions & 8 deletions src/components/as.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ElementType, forwardRef, ReactElement } from "react";
import { ComponentProps, AsProp, RefOfType } from "./types";
import { AsInProps, AsOutProps, RefOfType } from "./types";

export const as = <T extends ElementType, ExtraProps = unknown>(
export const as = <DefaultType extends ElementType, ExtraProps = object>(
fc: (
props: Omit<ComponentProps<T>, keyof ExtraProps | keyof AsProp<T>> & AsProp<T> & ExtraProps,
ref: RefOfType<T>
) => ReactElement
props: AsInProps<DefaultType, ExtraProps>,
ref: RefOfType<DefaultType>
) => ReactElement | null
) =>
forwardRef(fc) as unknown as <E extends ElementType = T>(
props: Omit<ComponentProps<E>, keyof ExtraProps | keyof AsProp<E>> & AsProp<E> & ExtraProps
) => ReactElement;
forwardRef(fc) as unknown as <T extends ElementType = DefaultType>(
props: AsOutProps<T, ExtraProps>
) => ReturnType<typeof fc>;
5 changes: 4 additions & 1 deletion src/components/checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import React, { AllHTMLAttributes, forwardRef } from "react";
import { Icon, Icons } from "../icon";
import * as css from "./Checkbox.css";

type CheckboxProps = Omit<AllHTMLAttributes<HTMLInputElement>, "children" | "onChange" | "type"> &
type CheckboxProps = Omit<
AllHTMLAttributes<HTMLInputElement>,
"children" | "onChange" | "type" | "size"
> &
css.CheckboxVariants & {
defaultChecked?: boolean;
checked?: boolean;
Expand Down
13 changes: 9 additions & 4 deletions src/components/scroll/Scroll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ export const Scroll = as<"div", css.ScrollVariants>(
useLayoutEffect(() => {
if (scrollLocalRef.current) {
const $scroll = scrollLocalRef.current;
const xScrollbarWidth = $scroll.offsetHeight - $scroll.clientHeight;
const yScrollbarWidth = $scroll.offsetWidth - $scroll.clientWidth;
if (size === "0") {
$scroll.setAttribute("data-x-scrollbar-width", "0");
$scroll.setAttribute("data-y-scrollbar-width", "0");
} else {
const xScrollbarWidth = $scroll.offsetHeight - $scroll.clientHeight;
const yScrollbarWidth = $scroll.offsetWidth - $scroll.clientWidth;

$scroll.setAttribute("data-x-scrollbar-width", `${xScrollbarWidth}`);
$scroll.setAttribute("data-y-scrollbar-width", `${yScrollbarWidth}`);
$scroll.setAttribute("data-x-scrollbar-width", `${xScrollbarWidth}`);
$scroll.setAttribute("data-y-scrollbar-width", `${yScrollbarWidth}`);
}
}
}, [size]);

Expand Down
21 changes: 19 additions & 2 deletions src/components/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ComponentPropsWithRef, ElementType, PropsWithChildren } from "react";
import {
ComponentPropsWithoutRef,
ComponentPropsWithRef,
ElementType,
PropsWithChildren,
} from "react";

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

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

export type ComponentProps<E extends ElementType> = PropsWithChildren<ComponentPropsWithRef<E>>;
export type AsInProps<E extends ElementType, ExtraProps = object> = Omit<
PropsWithChildren<ComponentPropsWithoutRef<E>>,
keyof ExtraProps | keyof AsProp<ElementType>
> &
ExtraProps &
AsProp<E>;

export type AsOutProps<E extends ElementType, ExtraProps = object> = Omit<
PropsWithChildren<ComponentPropsWithRef<E>>,
keyof ExtraProps | keyof AsProp<ElementType>
> &
ExtraProps &
AsProp<E>;

0 comments on commit 5990788

Please sign in to comment.