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

refactor: Improve typing so it shows the props #1591

Merged
merged 7 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion packages/components/button/src/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Box,
PolymorphicProps,
PolymorphicComponent,
ExpandProps,
} from '@contentful/f36-core';
import { Spinner } from '@contentful/f36-spinner';

Expand Down Expand Up @@ -112,7 +113,7 @@ function _Button<E extends React.ElementType = typeof BUTTON_DEFAULT_TAG>(
* @description: Buttons communicate the action that will occur when the user clicks it
*/
export const Button: PolymorphicComponent<
ButtonInternalProps,
ExpandProps<ButtonInternalProps>,
typeof BUTTON_DEFAULT_TAG,
'disabled'
> = React.forwardRef(_Button);
7 changes: 5 additions & 2 deletions packages/components/button/src/ButtonGroup/ButtonGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { cx } from 'emotion';
import React from 'react';
import { Box } from '@contentful/f36-core';
import { Box, ExpandProps } from '@contentful/f36-core';
import getStyles from './ButtonGroup.styles';
import type { ButtonGroupProps } from './types';

function _ButtonGroup(props: ButtonGroupProps, ref: React.Ref<HTMLDivElement>) {
function _ButtonGroup(
props: ExpandProps<ButtonGroupProps>,
Copy link
Contributor

Choose a reason for hiding this comment

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

can ExpandProps be a part of ButtonGroupProps?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sadly no, because it would expand the Type inside the ButtonGroupProps, but here it would show as ButtonGroupProps only, and not the values inside it

ref: React.Ref<HTMLDivElement>,
) {
const {
variant = 'collapsed',
withDivider,
Expand Down
8 changes: 6 additions & 2 deletions packages/components/button/src/IconButton/IconButton.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import React from 'react';
import { PolymorphicProps, PolymorphicComponent } from '@contentful/f36-core';
import {
PolymorphicProps,
PolymorphicComponent,
ExpandProps,
} from '@contentful/f36-core';
import { Button } from '../Button';
import type { ButtonInternalProps } from '../types';

Expand Down Expand Up @@ -51,7 +55,7 @@ function _IconButton<
}

export const IconButton: PolymorphicComponent<
IconButtonInternalProps,
ExpandProps<IconButtonInternalProps>,
typeof ICON_BUTTON_DEFAULT_TAG,
'disabled'
> = React.forwardRef(_IconButton);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from 'react';
import { cx } from 'emotion';
import { CommonProps } from '@contentful/f36-core';
import { CommonProps, ExpandProps } from '@contentful/f36-core';
import { Button } from '../Button';
import getStyles from './ToggleButton.styles';

Expand All @@ -26,7 +26,7 @@ export interface ToggleButtonProps extends CommonProps {
children: React.ReactNode;
}

function _ToggleButton(props: ToggleButtonProps, ref) {
function _ToggleButton(props: ExpandProps<ToggleButtonProps>, ref) {
const {
testId = 'cf-ui-toggle-button',
children,
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/Primitive/Primitive.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import React from 'react';

export type ExpandProps<T> = T extends object
Copy link
Contributor

Choose a reason for hiding this comment

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

Wow, this is quite cool. Now I need to understand how it works :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What this does is get the type T and if it's an object, it creates a copy and iterate over and returns a object typing {}, so it shows up as that on the definition not as the name.

Copy link
Contributor

Choose a reason for hiding this comment

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

thanks for the explanation

Copy link
Contributor

@bgutsol bgutsol Dec 7, 2021

Choose a reason for hiding this comment

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

Could you add some explanation around this type in the code comment, like the purpose and how it works (pretty much your answer for Alex) for the future us.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 ☝️

? T extends infer O
? { [K in keyof O]: O[K] }
: never
: T;

type Overwrite<T, U> = Omit<T, keyof U> & U;

type PropsWithAs<P, E extends React.ElementType> = P & { as?: E };
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/Primitive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export type {
PolymorphicComponent,
PropsWithHTMLElement,
PolymorphicProps,
ExpandProps,
} from './Primitive';
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export type {
PolymorphicComponent,
PropsWithHTMLElement,
PolymorphicProps,
ExpandProps,
} from './Primitive';
export type {
CommonProps,
Expand Down