-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a6a7e8
commit 91afa1c
Showing
10 changed files
with
199 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { forwardRef, useRef } from 'react'; | ||
import { Button } from './components/button.ref'; | ||
|
||
export default () => { | ||
const ref = useRef<HTMLButtonElement | null>(null); | ||
|
||
return ( | ||
<Button | ||
ref={ref} | ||
as='button' | ||
/> | ||
); | ||
}; | ||
|
||
export const ForwardedButton = forwardRef<HTMLButtonElement>(({ ...props }, ref) => ( | ||
<Button | ||
{...props} | ||
ref={ref} | ||
as={'button'} | ||
/> | ||
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { ComponentPropsWithoutRef, createElement, forwardRef, FC, ExoticComponent } from 'react'; | ||
import { | ||
PolymorphicExoticProps, | ||
PolymorphicForwardedRef, | ||
PolymorphicFunctionalProps, | ||
PolymorphicProps, | ||
} from '../../index'; | ||
|
||
// Default HTML element if the "as" prop is not provided | ||
export const ButtonDefaultElement = 'button'; | ||
// List of allowed HTML Element that can be passed via "as" prop | ||
export type ButtonAllowedElements = typeof ButtonDefaultElement | 'a' | 'div' | 'span'; | ||
// List of allowed React nodes that can be passed along with the "as" prop | ||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */ | ||
export type ButtonAllowedComponentTypes = ButtonAllowedElements | ExoticComponent | FC<any>; | ||
|
||
// Component-specific props | ||
export type ButtonOwnProps<T extends ButtonAllowedElements> = ComponentPropsWithoutRef<T> & { | ||
variant?: 'primary' | 'secondary' | 'tertiary'; | ||
disabled?: boolean; | ||
}; | ||
|
||
// Extend own props with others inherited from the underlying element type | ||
// Own props take precedence over the inherited ones | ||
export type ButtonProps<T extends ButtonAllowedComponentTypes> = T extends ButtonAllowedElements | ||
? PolymorphicProps<ButtonOwnProps<T>, T, ButtonAllowedElements> | ||
: /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ | ||
T extends FC<any> | ||
? PolymorphicFunctionalProps<ButtonOwnProps<ButtonAllowedElements>, T, ButtonAllowedElements> | ||
: PolymorphicExoticProps<ButtonOwnProps<ButtonAllowedElements>, T, ButtonAllowedElements>; | ||
|
||
const ButtonInner = <T extends ButtonAllowedComponentTypes>( | ||
{ as = ButtonDefaultElement, children, ...rest }: ButtonProps<T>, | ||
ref: PolymorphicForwardedRef<T>, | ||
) => | ||
createElement( | ||
as, | ||
{ | ||
...(rest.disabled && as !== 'button' ? { 'data-disabled': 'disabled' } : null), | ||
...rest, | ||
ref, | ||
}, | ||
children, | ||
); | ||
|
||
// Forward refs with generics is tricky | ||
// see also https://fettblog.eu/typescript-react-generic-forward-refs/ | ||
export const Button = forwardRef<ButtonAllowedComponentTypes>(ButtonInner) as < | ||
T extends ButtonAllowedComponentTypes = typeof ButtonDefaultElement, | ||
>( | ||
// eslint-disable-next-line no-use-before-define | ||
props: ButtonProps<T> & { ref?: PolymorphicForwardedRef<T> }, | ||
) => ReturnType<typeof ButtonInner>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"extends": "@axa-ch/easy-config/ts-config/base", | ||
"extends": "@axa-ch/easy-config/ts-config/base" | ||
} |