Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared type PropsBasedOnComponent #154

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/grid/src/__tests__/Col.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { render } from '@testing-library/react';

import Col from '../components/Col';
import { Col } from '../components/Col';

describe('Grid.Col', () => {
describe('Snapshots tests', () => {
Expand Down
8 changes: 5 additions & 3 deletions packages/grid/src/components/Col.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import React, { useMemo } from 'react';
import cn from 'classnames';

import { PropsBasedOnComponent } from '../../../../shared/types/PropsBasedOnComponent';
import getGridClassNames from '../utils/getGridClassNames';

import { ColProps } from './Col.types';

import guttersStyles from '../styles/gutters.module.css';
import styles from './Col.module.css';

function Col<E extends React.ElementType = 'div'>({
// export Col is necessary with PropsBasedOnComponent due to a Storybook bug
export const Col: PropsBasedOnComponent<ColProps, 'div'> = ({
Comment on lines +12 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we keep the default export in this case (IMO we shouldn't) and what is the Storybook bug?

component,
className,
align,
Expand All @@ -17,7 +19,7 @@ function Col<E extends React.ElementType = 'div'>({
width,
children,
dataTestId,
}: ColProps<E>) {
}) => {
const Component = component ?? 'div';

const gridClassNames = useMemo(
Expand All @@ -38,6 +40,6 @@ function Col<E extends React.ElementType = 'div'>({
{children}
</Component>
);
}
};

export default Col;
13 changes: 2 additions & 11 deletions packages/grid/src/components/Col.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

import { ResponsivePropertyType } from '../Grid.types';

type ColBaseProps<E extends React.ElementType> = {
export interface ColProps {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also update Row and Container as well?

/**
* Additional class
*/
Expand Down Expand Up @@ -43,12 +43,6 @@ type ColBaseProps<E extends React.ElementType> = {
*/
order?: ResponsivePropertyType;

/**
* The component used for the root node. Either a string to use a HTML element or a component
* @default "div"
*/
component?: E;

/**
* Content
*/
Expand All @@ -58,7 +52,4 @@ type ColBaseProps<E extends React.ElementType> = {
* Identifier for automated testing systems
*/
dataTestId?: string;
};

export type ColProps<E extends React.ElementType> = ColBaseProps<E> &
Omit<React.ComponentProps<E>, keyof ColBaseProps<E>>;
}
20 changes: 0 additions & 20 deletions packages/pagination/src/Pagination.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,6 @@ export interface PaginationItemProps {
onClick?: (itemNumber: number) => void;
}

// TODO Move this to a shared folder to be used in more components
/**
* `PropsBasedOnComponent` - This interface inherits props from a designated Component
*/
export interface PropsBasedOnComponent<
ComponentBaseProps,
DefaultElement extends React.ElementType,
> {
<Component extends React.ElementType = DefaultElement>(
props: {
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component?: Component;
} & ComponentBaseProps &
Omit<React.ComponentPropsWithoutRef<Component>, keyof ComponentBaseProps>,
): JSX.Element | null;
}

export type UsePaginationArgs = {
onClick?: CustomOnClick;
currentPage: number;
Expand Down
8 changes: 3 additions & 5 deletions packages/pagination/src/PaginationItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import cn from 'classnames';
import { ChevronLeft, ChevronRight } from '@heycar-uikit/icons';
import Typography from '@heycar-uikit/typography';

import {
PaginationItemProps,
PaginationItemType,
PropsBasedOnComponent,
} from './Pagination.types';
import { PropsBasedOnComponent } from '../../../shared/types/PropsBasedOnComponent';

import { PaginationItemProps, PaginationItemType } from './Pagination.types';

import styles from './styles/default.module.css';

Expand Down
2 changes: 1 addition & 1 deletion packages/pagination/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"include": ["src", "../../typings"],
"include": ["src", "../../typings", "../../shared"],
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
Expand Down
20 changes: 20 additions & 0 deletions shared/types/PropsBasedOnComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';

/**
* `PropsBasedOnComponent` - This interface inherits props from a designated Component
*/
export interface PropsBasedOnComponent<
ComponentBaseProps,
DefaultElement extends React.ElementType,
> {
<Component extends React.ElementType = DefaultElement>(
props: {
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component?: Component;
} & ComponentBaseProps &
Omit<React.ComponentPropsWithoutRef<Component>, keyof ComponentBaseProps>,
): JSX.Element | null;
}