Skip to content

Commit

Permalink
fix: use React.forwardRef in createSystemComponent
Browse files Browse the repository at this point in the history
This includes a breaking change in `createSystemComponent`:

Before: `createSystemComponent(React.createElement)`
After: `createSystemComponent(React)`

Be careful!
  • Loading branch information
gregberge committed Jul 9, 2019
1 parent a9d9438 commit 6c7da13
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/styled-components/src/styled.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function styled(component) {
return getCreateStyle(scStyled(component))
}

const InnerBox = createSystemComponent(React.createElement)
const InnerBox = createSystemComponent(React)

export const Box = styled(InnerBox)(createBox)

Expand All @@ -31,6 +31,6 @@ styled.box = styled(Box)
Object.keys(scStyled).forEach(key => {
styled[key] = styled(key)
styled[`${key}Box`] = styled(
Box.withComponent(createSystemComponent(React.createElement, key)),
Box.withComponent(createSystemComponent(React, key)),
)
})
11 changes: 7 additions & 4 deletions packages/system/src/systemComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import { omit } from './util'
import { system as allSystem } from './styles/index'

export const createSystemComponent = (
createElement,
{ createElement, forwardRef },
defaultComponent = 'div',
system = allSystem,
) => {
function SystemComponent({ as, ...props }) {
const SystemComponent = forwardRef(function SystemComponent(
{ as, ...props },
ref,
) {
const omittedProps = omit(props, system.meta.props)
const Component = as || defaultComponent
return createElement(Component, omittedProps)
}
return createElement(Component, { ref, ...omittedProps })
})
SystemComponent.displayName = 'SystemComponent'
return SystemComponent
}
15 changes: 13 additions & 2 deletions packages/system/src/systemComponent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,28 @@ afterEach(cleanup)
describe('systemComponent', () => {
describe('#createSystemComponent', () => {
it('creates a div that omit props', () => {
const Box = createSystemComponent(React.createElement)
const Box = createSystemComponent(React)
const { container } = render(<Box display="block" />)
expect(container.firstChild.tagName).toBe('DIV')
expect(container.firstChild).not.toHaveAttribute('display')
})

it('supports "as" prop', () => {
const Box = createSystemComponent(React.createElement)
const Box = createSystemComponent(React)
const { container } = render(<Box as="header" display="block" />)
expect(container.firstChild.tagName).toBe('HEADER')
expect(container.firstChild).not.toHaveAttribute('display')
})

it('forwards ref', () => {
const Box = createSystemComponent(React)
const ref = jest.fn()
const { container } = render(
<Box ref={ref} as="header" display="block" />,
)
expect(container.firstChild.tagName).toBe('HEADER')
expect(container.firstChild).not.toHaveAttribute('display')
expect(ref).toHaveBeenCalled()
})
})
})
2 changes: 1 addition & 1 deletion website/src/pages/docs/system-components.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ import React from 'react'
import styled from 'styled-components'
import { system, createSystemComponent } from '@xstyled/system'

const InnerBox = createSystemComponent(React.createElement)
const InnerBox = createSystemComponent(React)
const Box = styled(InnerBox)(system)
```

Expand Down
2 changes: 1 addition & 1 deletion website/src/pages/docs/system-props.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const colors = compose(
backgroundColor,
)

const InnerBox = createSystemComponent(React.createElement, 'div', colors)
const InnerBox = createSystemComponent(React, 'div', colors)

const Box = styled(InnerBox)`
${colors}
Expand Down

0 comments on commit 6c7da13

Please sign in to comment.