-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: map props to hooks (#2564)
* Refactor: map props to hooks * Appeace linter * Display name * Test: run postinstall manually * Restire yarn action * Memoize component * Only map * Add tests * Add a comment and rename the param * Add an export dialog test
- Loading branch information
Showing
8 changed files
with
135 additions
and
19 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
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
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,34 @@ | ||
import { render, waitFor, screen } from '@/tests/test-utils' | ||
import madProps from '../mad-props' | ||
|
||
describe('madProps', () => { | ||
it('should map a set of props to hooks', async () => { | ||
const Component = ({ foo, bar }: { foo: string; bar: string }) => <>{foo + bar}</> | ||
|
||
const MappedComponent = madProps(Component, { | ||
foo: () => 'foo', | ||
bar: () => 'bar', | ||
}) | ||
|
||
render(<MappedComponent />) | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('foobar')).toBeInTheDocument() | ||
}) | ||
}) | ||
|
||
it('should accept additional props', async () => { | ||
const Component = ({ foo, bar, baz }: { foo: string; bar: string; baz: string }) => <>{foo + bar + baz}</> | ||
|
||
const MappedComponent = madProps(Component, { | ||
foo: () => 'foo', | ||
bar: () => 'bar', | ||
}) | ||
|
||
render(<MappedComponent baz="baz" />) | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('foobarbaz')).toBeInTheDocument() | ||
}) | ||
}) | ||
}) |
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,40 @@ | ||
import type { ComponentType, FC } from 'react' | ||
import React, { memo } from 'react' | ||
|
||
type HookMap<P> = { | ||
[K in keyof P]?: () => P[K] | ||
} | ||
|
||
/** | ||
* Injects props into a component using hooks. | ||
* This allows to keep the original component pure and testable. | ||
* | ||
* @param Component The component to wrap | ||
* @param hookMap A map of hooks to use, keys are the props to inject, values are the hooks | ||
* @returns A new component with the props injected | ||
*/ | ||
const madProps = <P extends Record<string, unknown>, H extends keyof P>( | ||
Component: ComponentType<P>, | ||
hookMap: HookMap<Pick<P, H>>, | ||
): FC<Omit<P, H>> => { | ||
const MadComponent = (externalProps: Omit<P, H>) => { | ||
let newProps: P = { ...externalProps } as P | ||
|
||
for (const key in hookMap) { | ||
const hook = hookMap[key] | ||
if (hook !== undefined) { | ||
newProps[key as H] = hook() | ||
} | ||
} | ||
|
||
return <Component {...newProps} /> | ||
} | ||
|
||
MadComponent.displayName = Component.displayName || Component.name | ||
|
||
// Wrapping MadComponent with React.memo and casting to FC<Omit<P, H>> | ||
// The casting is only needed because of memo, the component itself satisfies the type | ||
return memo(MadComponent) as unknown as FC<Omit<P, H>> | ||
} | ||
|
||
export default madProps |