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

Commit

Permalink
fix(factories): remove render from default options (#735)
Browse files Browse the repository at this point in the history
* fix(factories): remove `render` from default options

* add changelog entry

* update entry
  • Loading branch information
layershifter committed Jan 16, 2019
1 parent bb22b77 commit 7ea813c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### BREAKING
- Rename `DropdownLabel` to `DropdownSelectedItem` and extract styles @layershifter ([#725](https://github.com/stardust-ui/react/pull/725))

### Fixes
- Remove `render` from default factories options @layershifter ([#735](https://github.com/stardust-ui/react/pull/735))

### Documentation
- Fix ignored initial state of knobs @layershifter ([#720](https://github.com/stardust-ui/react/pull/720))
- Fix unclearable example's code @layershifter ([#720](https://github.com/stardust-ui/react/pull/720))
Expand Down
13 changes: 6 additions & 7 deletions src/lib/factories.ts
Expand Up @@ -32,7 +32,6 @@ const CREATE_SHORTHAND_DEFAULT_OPTIONS: CreateShorthandOptions = {
defaultProps: {},
overrideProps: {},
generateKey: true,
render: (Component, props) => React.createElement(Component, props),
}

// It's only necessary to map props that don't use 'children' as value ('children' is the default)
Expand All @@ -49,7 +48,7 @@ const mappedProps: { [key in HTMLTag]: ShorthandProp } = {
/** A more robust React.createElement. It can create elements from primitive values. */
export function createShorthand(
Component: React.ReactType,
mappedProp: string,
mappedProp?: string,
valueOrRenderCallback?: ShorthandValue | ShorthandRenderCallback,
options: CreateShorthandOptions = CREATE_SHORTHAND_DEFAULT_OPTIONS,
): React.ReactElement<Props> | null | undefined {
Expand All @@ -58,8 +57,8 @@ export function createShorthand(
if (valIsRenderFunction) {
return createShorthandFromRenderCallback(
Component,
mappedProp,
valueOrRenderCallback as ShorthandRenderCallback,
mappedProp,
options,
)
}
Expand Down Expand Up @@ -97,9 +96,9 @@ export function createShorthandFactory<T extends React.ReactType>(

function createShorthandFromValue(
Component: React.ReactType,
mappedProp: string,
mappedProp?: string,
value?: ShorthandValue,
options: CreateShorthandOptions = CREATE_SHORTHAND_DEFAULT_OPTIONS,
options?: CreateShorthandOptions,
) {
if (typeof Component !== 'function' && typeof Component !== 'string') {
throw new Error('createShorthand() Component must be a string or function.')
Expand Down Expand Up @@ -207,9 +206,9 @@ function createShorthandFromValue(

function createShorthandFromRenderCallback(
Component: React.ReactType,
mappedProp: string,
renderCallback: ShorthandRenderCallback,
options: CreateShorthandOptions = CREATE_SHORTHAND_DEFAULT_OPTIONS,
mappedProp?: string,
options?: CreateShorthandOptions,
) {
const render: ShorthandRenderer = (shorthandValue, renderTree) => {
return createShorthandFromValue(Component, mappedProp, shorthandValue, {
Expand Down
6 changes: 6 additions & 0 deletions test/specs/lib/factories-test.tsx
Expand Up @@ -164,6 +164,12 @@ describe('factories', () => {
expect(goodUsage).not.toThrowError()
})

test('does not throw if do not passed `mappedProp`', () => {
const goodUsage = () => createShorthandFactory(() => <div />)

expect(goodUsage).not.toThrowError()
})

test('throw if passed Component that is not a string nor function', () => {
consoleUtil.disableOnce()
const badComponents: any = [undefined, null, true, false, [], {}, 123]
Expand Down

0 comments on commit 7ea813c

Please sign in to comment.