Skip to content

Commit 031031f

Browse files
authored
Merge pull request #40 from smooth-code/create-system-component-forward-ref
fix: use React.forwardRef in createSystemComponent
2 parents a9d9438 + 6c7da13 commit 031031f

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

packages/styled-components/src/styled.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function styled(component) {
2222
return getCreateStyle(scStyled(component))
2323
}
2424

25-
const InnerBox = createSystemComponent(React.createElement)
25+
const InnerBox = createSystemComponent(React)
2626

2727
export const Box = styled(InnerBox)(createBox)
2828

@@ -31,6 +31,6 @@ styled.box = styled(Box)
3131
Object.keys(scStyled).forEach(key => {
3232
styled[key] = styled(key)
3333
styled[`${key}Box`] = styled(
34-
Box.withComponent(createSystemComponent(React.createElement, key)),
34+
Box.withComponent(createSystemComponent(React, key)),
3535
)
3636
})

packages/system/src/systemComponent.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ import { omit } from './util'
22
import { system as allSystem } from './styles/index'
33

44
export const createSystemComponent = (
5-
createElement,
5+
{ createElement, forwardRef },
66
defaultComponent = 'div',
77
system = allSystem,
88
) => {
9-
function SystemComponent({ as, ...props }) {
9+
const SystemComponent = forwardRef(function SystemComponent(
10+
{ as, ...props },
11+
ref,
12+
) {
1013
const omittedProps = omit(props, system.meta.props)
1114
const Component = as || defaultComponent
12-
return createElement(Component, omittedProps)
13-
}
15+
return createElement(Component, { ref, ...omittedProps })
16+
})
1417
SystemComponent.displayName = 'SystemComponent'
1518
return SystemComponent
1619
}

packages/system/src/systemComponent.test.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,28 @@ afterEach(cleanup)
88
describe('systemComponent', () => {
99
describe('#createSystemComponent', () => {
1010
it('creates a div that omit props', () => {
11-
const Box = createSystemComponent(React.createElement)
11+
const Box = createSystemComponent(React)
1212
const { container } = render(<Box display="block" />)
1313
expect(container.firstChild.tagName).toBe('DIV')
1414
expect(container.firstChild).not.toHaveAttribute('display')
1515
})
1616

1717
it('supports "as" prop', () => {
18-
const Box = createSystemComponent(React.createElement)
18+
const Box = createSystemComponent(React)
1919
const { container } = render(<Box as="header" display="block" />)
2020
expect(container.firstChild.tagName).toBe('HEADER')
2121
expect(container.firstChild).not.toHaveAttribute('display')
2222
})
23+
24+
it('forwards ref', () => {
25+
const Box = createSystemComponent(React)
26+
const ref = jest.fn()
27+
const { container } = render(
28+
<Box ref={ref} as="header" display="block" />,
29+
)
30+
expect(container.firstChild.tagName).toBe('HEADER')
31+
expect(container.firstChild).not.toHaveAttribute('display')
32+
expect(ref).toHaveBeenCalled()
33+
})
2334
})
2435
})

website/src/pages/docs/system-components.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import React from 'react'
7171
import styled from 'styled-components'
7272
import { system, createSystemComponent } from '@xstyled/system'
7373

74-
const InnerBox = createSystemComponent(React.createElement)
74+
const InnerBox = createSystemComponent(React)
7575
const Box = styled(InnerBox)(system)
7676
```
7777

website/src/pages/docs/system-props.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const colors = compose(
5656
backgroundColor,
5757
)
5858

59-
const InnerBox = createSystemComponent(React.createElement, 'div', colors)
59+
const InnerBox = createSystemComponent(React, 'div', colors)
6060

6161
const Box = styled(InnerBox)`
6262
${colors}

0 commit comments

Comments
 (0)