Skip to content

Commit

Permalink
fix: add forwardref support (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrson83 authored Sep 24, 2023
1 parent 051f22f commit d88a77a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
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 }
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 @@ export type SLNode =
// | 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

0 comments on commit d88a77a

Please sign in to comment.