Skip to content

Commit d9710ec

Browse files
committed
Add reusable types
Signed-off-by: Winner95 <Winner95@users.noreply.github.com>
1 parent 84fa158 commit d9710ec

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

fixtures/AccessibleButton.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
3+
import { TStringOrComponent, PolymorphicProps } from './Types';
4+
5+
type AriaProps = {
6+
'aria-label'?: string;
7+
'aria-labelledby'?: string;
8+
'aria-describedby'?: string;
9+
role?: string;
10+
};
11+
12+
type AccessibleButtonProps<T extends TStringOrComponent = 'button'> =
13+
PolymorphicProps<
14+
T,
15+
{
16+
/** required for accessibility */
17+
label: string;
18+
isToggleButton?: boolean;
19+
} & AriaProps
20+
>;
21+
22+
export const AccessibleButton: React.FC<AccessibleButtonProps> = ({
23+
label,
24+
}) => {
25+
return <button>{label}</button>;
26+
};

fixtures/SecondaryIconButton.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react';
2+
3+
import { TStringOrComponent, PolymorphicProps } from './Types';
4+
5+
type SecondaryIconButtonOwnProps = {
6+
icon: React.ReactElement;
7+
type?: 'button' | 'submit' | 'reset';
8+
color?: 'solid' | 'neutral' | 'warning';
9+
};
10+
11+
type SecondaryIconButtonProps<T extends TStringOrComponent = 'button'> =
12+
PolymorphicProps<T, SecondaryIconButtonOwnProps>;
13+
14+
export const SecondaryIconButton: React.FC<SecondaryIconButtonProps> = ({
15+
color,
16+
}) => {
17+
return <button>{color}</button>;
18+
};

fixtures/TooltipTarget.tsx

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
import React, { JSX } from 'react';
22

3-
type TStringOrComponent = string | React.JSXElementConstructor<any>;
4-
5-
type PolymorphicProps<
6-
T extends TStringOrComponent,
7-
Props = {}
8-
> = Props &
9-
(T extends React.JSXElementConstructor<infer P>
10-
? P
11-
: T extends keyof JSX.IntrinsicElements
12-
? JSX.IntrinsicElements[T]
13-
: {}) & {
14-
as?: T;
15-
};
3+
import { TStringOrComponent, PolymorphicProps } from './Types';
164

175
type TooltipTargetOwnProps = {
186
isDisabled?: boolean;

fixtures/Types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { JSX } from 'react';
2+
3+
export type TStringOrComponent = string | React.JSXElementConstructor<any>;
4+
5+
export type PolymorphicProps<
6+
T extends TStringOrComponent,
7+
Props = {}
8+
> = Props &
9+
(T extends React.JSXElementConstructor<infer P>
10+
? P
11+
: T extends keyof JSX.IntrinsicElements
12+
? JSX.IntrinsicElements[T]
13+
: {}) & {
14+
as?: T;
15+
};

0 commit comments

Comments
 (0)