Skip to content

Commit 5690d0c

Browse files
committed
forward ref
1 parent edf5356 commit 5690d0c

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

src/components/ColorInput/ColorInput.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const StyledColorInput = styled.input`
2121
top: 0;
2222
opacity: 0;
2323
`;
24+
2425
// TODO replace with SVG icon
2526
const ColorPreview = styled.div`
2627
box-sizing: border-box;
@@ -43,6 +44,7 @@ const ColorPreview = styled.div`
4344
border: 2px solid ${({ theme }) => theme.text};
4445
`}
4546
`;
47+
4648
const ChevronIcon = styled.span`
4749
position: relative;
4850
width: 0px;
@@ -64,29 +66,33 @@ const ChevronIcon = styled.span`
6466
border-top: 6px solid ${({ theme }) => theme.text};
6567
`}
6668
`;
69+
6770
// TODO make sure all aria and role attributes are in place
68-
const ColorInput = ({
69-
value,
70-
defaultValue,
71-
onChange,
72-
disabled,
73-
variant,
74-
...otherProps
75-
}) => {
71+
const ColorInput = React.forwardRef(function ColorInput(props, ref) {
72+
const {
73+
value,
74+
defaultValue,
75+
onChange,
76+
disabled,
77+
variant,
78+
...otherProps
79+
} = props;
80+
7681
const [valueDerived, setValueState] = useControlledOrUncontrolled({
7782
value,
7883
defaultValue
7984
});
85+
8086
const handleChange = e => {
8187
const color = e.target.value;
8288
setValueState(color);
8389
if (onChange) {
8490
onChange(e);
8591
}
8692
};
93+
8794
return (
88-
// we need only button styles and behaviour when button is :active or disabled,
89-
// so we display it as a div and reset type attribute
95+
// we only need button styles, so we display it as a div and reset type attribute
9096
<Button isDisabled={disabled} as='div' type={null} variant={variant}>
9197
<ColorPreview
9298
color={valueDerived}
@@ -100,12 +106,14 @@ const ColorInput = ({
100106
disabled={disabled}
101107
value={valueDerived || '#008080'}
102108
type='color'
109+
ref={ref}
103110
{...otherProps}
104111
/>
105112
<ChevronIcon isDisabled={disabled} />
106113
</Button>
107114
);
108-
};
115+
});
116+
109117
ColorInput.defaultProps = {
110118
value: undefined,
111119
defaultValue: undefined,
Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react';
1+
import React from 'react';
22
import { storiesOf } from '@storybook/react';
33

44
import styled from 'styled-components';
@@ -16,23 +16,17 @@ const StyledCutout = styled(Cutout)`
1616
`;
1717
storiesOf('ColorInput', module)
1818
.addDecorator(story => <Wrapper>{story()}</Wrapper>)
19-
.add('uncontrolled', () => <ColorInput defaultValue='#00f' />)
20-
.add('controlled', () => <ControlledColorInput />)
19+
.add('uncontrolled', () =>
20+
React.createElement(() => <ColorInput defaultValue='#00f' />)
21+
)
2122

22-
.add('disabled', () => <ColorInput disabled defaultValue='#00f' />)
23-
.add('flat', () => (
24-
<StyledCutout>
25-
<ColorInput variant='flat' defaultValue='#00f' />
26-
</StyledCutout>
27-
))
28-
.add('flat disabled', () => (
29-
<StyledCutout>
30-
<ColorInput variant='flat' disabled defaultValue='#00f' />
31-
</StyledCutout>
32-
));
33-
34-
const ControlledColorInput = () => {
35-
const [color, setColor] = useState('#008080');
36-
const handleChange = e => setColor(e.target.value);
37-
return <ColorInput value={color} onChange={handleChange} />;
38-
};
23+
.add('disabled', () =>
24+
React.createElement(() => <ColorInput disabled defaultValue='#00f' />)
25+
)
26+
.add('flat', () =>
27+
React.createElement(() => (
28+
<StyledCutout>
29+
<ColorInput variant='flat' defaultValue='#00f' />
30+
</StyledCutout>
31+
))
32+
);

0 commit comments

Comments
 (0)