Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 7ada6ac

Browse files
authored
feat(utils): add wrapper for the createComponent function (#503)
* -introduced createStardustComponent method * -renamed export * -addressed comments * -addressed comments * -exported types, refactored typings * -exported types from function file * -updated params for the render method inside the CreateStardustComponentConfig -updated changelog * -addressed comments: lodash , themes imports * -changed displayName to be required
1 parent 4fb1fc2 commit 7ada6ac

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1717

1818
## [Unreleased]
1919

20+
### Features
21+
- Add `renderComponent` function in the public API @mnajdova ([#503](https://github.com/stardust-ui/react/pull/503))
22+
2023
### Fixes
2124
- Fix the behaviour of `AutoControlledComponent` when `undefined` is passed as a prop value @layershifter ([#499](https://github.com/stardust-ui/react/pull/499))
2225

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,8 @@ export { default as dialogBehavior } from './lib/accessibility/Behaviors/Dialog/
125125
// Utilities
126126
//
127127
export { default as mergeThemes } from './lib/mergeThemes'
128+
export { createComponent } from './lib'
129+
export {
130+
RenderStardustResultConfig,
131+
CreateStardustComponentConfig,
132+
} from './lib/createStardustComponent'

src/lib/createStardustComponent.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import createComponentInternal from './createComponent'
2+
import * as React from 'react'
3+
import * as _ from 'lodash'
4+
import { ComponentSlotClasses } from '../themes/types'
5+
6+
export interface RenderStardustResultConfig {
7+
classes: ComponentSlotClasses
8+
rtl: boolean
9+
}
10+
11+
export interface CreateStardustComponentConfig<P> {
12+
displayName: string
13+
render: (props: P & { stardust: RenderStardustResultConfig }) => React.ReactNode
14+
}
15+
16+
const createComponent = <P extends {} = {}, S extends {} = {}>({
17+
displayName,
18+
render,
19+
}: CreateStardustComponentConfig<P>): React.SFC<P> => {
20+
return createComponentInternal<P, S>({
21+
displayName,
22+
render: (config, props) => {
23+
const filteredConfig = _.pick(config, ['classes', 'rtl'])
24+
return render(Object.assign({ stardust: filteredConfig }, props))
25+
},
26+
})
27+
}
28+
29+
export default createComponent

src/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ export { default as leven } from './leven'
3333
export { pxToRem, setHTMLFontSize } from './fontSizeUtility'
3434
export { customPropTypes }
3535
export { default as createAnimationStyles } from './createAnimationStyles'
36+
export { default as createComponent } from './createStardustComponent'

0 commit comments

Comments
 (0)