Skip to content

Commit 523ae7f

Browse files
authored
Merge pull request #2940 from SUI-Components/atom-panel-forward-ref
feat(components/atom/panel): add forward ref
2 parents f57b061 + bf28bfa commit 523ae7f

File tree

4 files changed

+112
-60
lines changed

4 files changed

+112
-60
lines changed

components/atom/panel/src/ColorPanel.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1+
import {forwardRef} from 'react'
12
import PropTypes from 'prop-types'
23

34
import PolymorphicElement from '@s-ui/react-primitive-polymorphic-element'
45

56
import {ALPHA, BORDER_RADIUS, COLORS, DEFAULT_ALPHA, DEFAULT_COLOR, ELEVATION, getColorClassNames} from './settings.js'
67

7-
const ColorPanel = ({
8-
as = 'div',
9-
alpha = DEFAULT_ALPHA,
10-
color = DEFAULT_COLOR,
11-
children,
12-
id,
13-
className,
14-
...otherProps
15-
}) => {
16-
return (
17-
<PolymorphicElement as={as} id={id} className={getColorClassNames({className, alpha, color, ...otherProps})}>
18-
{children}
19-
</PolymorphicElement>
20-
)
21-
}
8+
const ColorPanel = forwardRef(
9+
(
10+
{as = 'div', alpha = DEFAULT_ALPHA, color = DEFAULT_COLOR, children, id, className, ...otherProps},
11+
forwardedRef
12+
) => {
13+
return (
14+
<PolymorphicElement
15+
ref={forwardedRef}
16+
as={as}
17+
id={id}
18+
className={getColorClassNames({className, alpha, color, ...otherProps})}
19+
>
20+
{children}
21+
</PolymorphicElement>
22+
)
23+
}
24+
)
2225

2326
ColorPanel.displayName = 'ColorPanel'
2427

components/atom/panel/src/ImagePanel.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {forwardRef} from 'react'
12
import PropTypes from 'prop-types'
23

34
import PolymorphicElement from '@s-ui/react-primitive-polymorphic-element'
@@ -13,26 +14,32 @@ import {
1314
getImageStyles
1415
} from './settings.js'
1516

16-
const ImagePanel = ({
17-
as = 'div',
18-
id,
19-
verticalAlign = DEFAULT_VERTICAL_ALIGNMENT,
20-
horizontalAlign = DEFAULT_HORIZONTAL_ALIGNMENT,
21-
children,
22-
className,
23-
...props
24-
}) => {
25-
return (
26-
<PolymorphicElement
27-
as={as}
28-
id={id}
29-
className={getImageClassNames({className, ...props})}
30-
style={getImageStyles(props)}
31-
>
32-
{children}
33-
</PolymorphicElement>
34-
)
35-
}
17+
const ImagePanel = forwardRef(
18+
(
19+
{
20+
as = 'div',
21+
id,
22+
verticalAlign = DEFAULT_VERTICAL_ALIGNMENT,
23+
horizontalAlign = DEFAULT_HORIZONTAL_ALIGNMENT,
24+
children,
25+
className,
26+
...props
27+
},
28+
forwardedRef
29+
) => {
30+
return (
31+
<PolymorphicElement
32+
ref={forwardedRef}
33+
as={as}
34+
id={id}
35+
className={getImageClassNames({className, ...props})}
36+
style={getImageStyles(props)}
37+
>
38+
{children}
39+
</PolymorphicElement>
40+
)
41+
}
42+
)
3643

3744
ImagePanel.displayName = 'ImagePanel'
3845

components/atom/panel/src/index.js

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {forwardRef} from 'react'
12
import PropTypes from 'prop-types'
23

34
import ColorPanel from './ColorPanel.js'
@@ -14,32 +15,38 @@ import {
1415
} from './settings.js'
1516
import ImagePanel from './ImagePanel.js'
1617

17-
const AtomPanel = ({
18-
alpha,
19-
color,
20-
elevation = DEFAULT_ELEVATION,
21-
horizontalAlign,
22-
verticalAlign,
23-
rounded = DEFAULT_BORDER_RADIUS,
24-
src,
25-
className,
26-
...props
27-
}) => {
28-
const [Component, componentProps] = isImagePanel({src})
29-
? [ImagePanel, {src, horizontalAlign, verticalAlign}]
30-
: [ColorPanel, {alpha}]
18+
const AtomPanel = forwardRef(
19+
(
20+
{
21+
alpha,
22+
color,
23+
elevation = DEFAULT_ELEVATION,
24+
horizontalAlign,
25+
verticalAlign,
26+
rounded = DEFAULT_BORDER_RADIUS,
27+
src,
28+
className,
29+
...props
30+
},
31+
forwardedRef
32+
) => {
33+
const [Component, componentProps] = isImagePanel({src})
34+
? [ImagePanel, {src, horizontalAlign, verticalAlign}]
35+
: [ColorPanel, {alpha}]
3136

32-
return (
33-
<Component
34-
className={className}
35-
color={color}
36-
elevation={elevation}
37-
rounded={rounded}
38-
{...componentProps}
39-
{...props}
40-
/>
41-
)
42-
}
37+
return (
38+
<Component
39+
ref={forwardedRef}
40+
className={className}
41+
color={color}
42+
elevation={elevation}
43+
rounded={rounded}
44+
{...componentProps}
45+
{...props}
46+
/>
47+
)
48+
}
49+
)
4350

4451
AtomPanel.displayName = 'AtomPanel'
4552

components/atom/panel/test/index.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/* eslint react/jsx-no-undef:0 */
66
/* eslint no-undef:0 */
77

8+
import {createRef} from 'react'
89
import ReactDOM from 'react-dom'
910

1011
import chai, {expect} from 'chai'
@@ -93,6 +94,40 @@ describe(json.name, () => {
9394
expect(findClassName(container.innerHTML)).to.not.be.null
9495
})
9596

97+
describe('forwardRef', () => {
98+
it('should return forwardRef html DIV element when giving a ref to the component', () => {
99+
// Given
100+
const props = {}
101+
const ref = createRef()
102+
103+
// When
104+
const component = <Component {...props} ref={ref} />
105+
const div = document.createElement('div')
106+
ReactDOM.render(component, div)
107+
108+
// Then
109+
expect(ref.current).to.not.equal(undefined)
110+
expect(ref.current.nodeName).to.equal('DIV')
111+
})
112+
113+
it('should return forwardRef html DIV element when giving a ref to the component', () => {
114+
// Given
115+
const props = {
116+
src: '#'
117+
}
118+
const ref = createRef()
119+
120+
// When
121+
const component = <Component {...props} ref={ref} />
122+
const div = document.createElement('div')
123+
ReactDOM.render(component, div)
124+
125+
// Then
126+
expect(ref.current).to.not.equal(undefined)
127+
expect(ref.current.nodeName).to.equal('DIV')
128+
})
129+
})
130+
96131
describe('ColorPanel', () => {
97132
it('should render without crashing', () => {
98133
// Given

0 commit comments

Comments
 (0)