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

fix: add forwardref support #74

Merged
merged 1 commit into from
Sep 24, 2023
Merged
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
18 changes: 18 additions & 0 deletions packages/examples/react/src/components/ForwardRefButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ComponentPropsWithRef, forwardRef } from 'react'

export interface ButtonProps extends ComponentPropsWithRef<'button'> {
variant?: 'red' | 'green'
}

const ForwardRefButton = forwardRef<HTMLButtonElement, ButtonProps>(function ForwardRefButton(
{ variant = 'red', children, ...rest },
ref,
) {
return (
<button ref={ref} style={{ color: variant }} {...rest}>
{children}
</button>
)
})

export { ForwardRefButton }

Check warning on line 18 in packages/examples/react/src/components/ForwardRefButton.tsx

View check run for this annotation

Codecov / codecov/patch

packages/examples/react/src/components/ForwardRefButton.tsx#L2-L18

Added lines #L2 - L18 were not covered by tests
43 changes: 43 additions & 0 deletions packages/examples/react/stories/forwardref.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Story } from '@storylite/storylite'

import { ForwardRefButton } from '../src/components/ForwardRefButton'

import '../src/styles/components.css'

type StoryType = Story<typeof ForwardRefButton>

// all properties (except navigation) defined in the 'default' export are inherited by
// all other stories in this file
export default {
title: 'ForwardRefButton',
component: ForwardRefButton,
decorators: [
(Story, context) => {
return (
<div style={{ padding: 20, borderRadius: '8px', background: '#ddd' }}>
<Story {...context?.args} />
</div>
)
},
],
args: {
// default args
children: 'My Button',
},
navigation: {
// overriden navigation options
icon: <span>⏩</span>,
hidden: true, // hidden in sub-menu
},
} satisfies StoryType

export const DefaultStory: StoryType = {
name: 'Default',
}

export const WithComponentProps: StoryType = {
args: {
variant: 'green',
children: 'Green Variant',
},
}
13 changes: 10 additions & 3 deletions packages/storylite/src/types/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
// | SLFunctionComponent<any>

export type SLFunctionComponent<P extends SLObject = {}> = React.FC<P>
export type SLComponentProps<P extends SLFunctionComponent = SLFunctionComponent<{}>> = {
[key in keyof ComponentProps<SLFunctionComponent<P>>]: ComponentProps<SLFunctionComponent<P>>[key]
}

export type SLComponentProps<T> = T extends
| React.ComponentType<infer P extends SLObject>
| React.Component<infer P extends SLObject>
? {
[key in keyof ComponentProps<SLFunctionComponent<P>>]: ComponentProps<
SLFunctionComponent<P>
>[key]
}
: never

Check warning on line 33 in packages/storylite/src/types/components.ts

View check run for this annotation

Codecov / codecov/patch

packages/storylite/src/types/components.ts#L24-L33

Added lines #L24 - L33 were not covered by tests