Skip to content

Commit a2ba65c

Browse files
authored
feat: colorbox error displayed once #30 (#53)
1 parent 149d7f1 commit a2ba65c

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

src/components/ColorBox/index.jsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,17 @@ const StyledBox = styled.div`
7979
}
8080
& .muicc-colorbox-controls {
8181
display: flex;
82-
flex-direction: row-reverse;
82+
flex-direction: row;
8383
flex-wrap: wrap;
8484
padding: 8px;
8585
}
86+
& .muicc-colorbox-error {
87+
color: #f44336;
88+
line-height: 36.5px;
89+
}
90+
& .muicc-colorbox-controls > button {
91+
margin-left: auto;
92+
}
8693
`;
8794

8895
const ColorBox = ({ value, palette, inputFormats, deferred, onChange: _onChange, disableAlpha, ...props }) => {
@@ -139,6 +146,7 @@ const ColorBox = ({ value, palette, inputFormats, deferred, onChange: _onChange,
139146
value={color}
140147
format={input}
141148
disableAlpha
149+
enableErrorDisplay={false}
142150
className="muicc-colorbox-input"
143151
onChange={handleInputChange}
144152
/>
@@ -190,11 +198,14 @@ const ColorBox = ({ value, palette, inputFormats, deferred, onChange: _onChange,
190198
/>
191199
</>
192200
)}
193-
{deferred && (
194-
<div className="muicc-colorbox-controls">
195-
<Button onClick={handleSet}>{t('Set')}</Button>
196-
</div>
197-
)}
201+
<div className="muicc-colorbox-controls">
202+
{color.error && (
203+
<span className="muicc-colorbox-error" data-testid="colorbox-error">
204+
{t(color.error)}
205+
</span>
206+
)}
207+
{deferred && <Button onClick={handleSet}>{t('Set')}</Button>}
208+
</div>
198209
</StyledBox>
199210
</Box>
200211
);

src/components/ColorInput.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const StyledRoot = styled.div`
3535
}
3636
`;
3737

38-
const ColorInput = ({ value, format, onChange, disableAlpha, forwardRef, ...props }) => {
38+
const ColorInput = ({ value, format, onChange, disableAlpha, enableErrorDisplay, forwardRef, ...props }) => {
3939
const { t, i18n } = useTranslate();
4040
const color = ColorTool.validateColor(value, disableAlpha, t, i18n.language);
4141
let field;
@@ -102,7 +102,7 @@ const ColorInput = ({ value, format, onChange, disableAlpha, forwardRef, ...prop
102102
return (
103103
<StyledFormControl error={!!color.error} data-testid="colorinput">
104104
{field}
105-
{color.error && <FormHelperText id="component-error-text">{color.error}</FormHelperText>}
105+
{enableErrorDisplay && color.error && <FormHelperText id="component-error-text">{color.error}</FormHelperText>}
106106
</StyledFormControl>
107107
);
108108
};
@@ -118,6 +118,7 @@ ColorInput.propTypes = {
118118
/**
119119
Internal usage
120120
*/
121+
enableErrorDisplay: PropTypes.bool,
121122
forwardRef: PropTypes.shape({ current: PropTypes.instanceOf(Element) }),
122123
};
123124

@@ -126,6 +127,7 @@ ColorInput.defaultProps = {
126127
format: 'plain',
127128
forwardRef: undefined,
128129
disableAlpha: false,
130+
enableErrorDisplay: true,
129131
};
130132

131133
export default uncontrolled(ColorInput);

test/ColorBox.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import React from 'react';
1010
import { render, fireEvent } from '@testing-library/react';
1111
import ColorBox from '../src/components/ColorBox';
12+
import * as ColorTool from '../src/helpers/colorTool';
1213

1314
const palette = {
1415
red: '#ff0000',
@@ -114,6 +115,13 @@ test('ColorBox palette onChange', () => {
114115
expect(value.raw).toBe('#ff0000');
115116
});
116117

118+
test('ColorBox error display', () => {
119+
const color = ColorTool.validateColor('#ffx');
120+
const { getByTestId } = render(<ColorBox value={color} />);
121+
const span = getByTestId('colorbox-error');
122+
expect(span.textContent).toBe('Not an hex value');
123+
});
124+
117125
test('ColorBox deferred', () => {
118126
let value;
119127
const onChange = jest.fn().mockImplementation(newValue => {

0 commit comments

Comments
 (0)