Skip to content

Commit eedc86f

Browse files
authored
Fixes bug on color picker defaults on TSVB (#69889)
* Fixes bug on color picker defaults on TSVB * Add test to ensure that the input text of the picker is set up correctly
1 parent f486801 commit eedc86f

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/plugins/vis_type_timeseries/public/application/components/color_picker.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import { ColorPicker, ColorPickerProps } from './color_picker';
2222
import { mount } from 'enzyme';
2323
import { ReactWrapper } from 'enzyme';
2424
import { EuiColorPicker, EuiIconTip } from '@elastic/eui';
25+
// @ts-ignore
26+
import { findTestSubject } from '@elastic/eui/lib/test';
2527

2628
describe('ColorPicker', () => {
2729
const defaultProps: ColorPickerProps = {
@@ -42,6 +44,22 @@ describe('ColorPicker', () => {
4244
expect(component.find('.tvbColorPicker__clear').length).toBe(0);
4345
});
4446

47+
it('should render the correct value to the input text if the prop value is hex', () => {
48+
const props = { ...defaultProps, value: '#68BC00' };
49+
component = mount(<ColorPicker {...props} />);
50+
component.find('.tvbColorPicker button').simulate('click');
51+
const input = findTestSubject(component, 'topColorPickerInput');
52+
expect(input.props().value).toBe('#68BC00');
53+
});
54+
55+
it('should render the correct value to the input text if the prop value is rgba', () => {
56+
const props = { ...defaultProps, value: 'rgba(85,66,177,1)' };
57+
component = mount(<ColorPicker {...props} />);
58+
component.find('.tvbColorPicker button').simulate('click');
59+
const input = findTestSubject(component, 'topColorPickerInput');
60+
expect(input.props().value).toBe('85,66,177,1');
61+
});
62+
4563
it('should render the correct aria label to the color swatch button', () => {
4664
const props = { ...defaultProps, value: 'rgba(85,66,177,0.59)' };
4765
component = mount(<ColorPicker {...props} />);

src/plugins/vis_type_timeseries/public/application/components/color_picker.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ export interface ColorPickerProps {
4343
}
4444

4545
export function ColorPicker({ name, value, disableTrash = false, onChange }: ColorPickerProps) {
46-
const initialColorValue = value ? value.replace(COMMAS_NUMS_ONLY_RE, '') : '';
47-
const [color, setColor] = useState(initialColorValue);
46+
const initialColorValue = value?.includes('rgba')
47+
? value.replace(COMMAS_NUMS_ONLY_RE, '')
48+
: value;
49+
const [color, setColor] = useState(initialColorValue || '');
4850

4951
const handleColorChange: EuiColorPickerProps['onChange'] = (text: string, { rgba, hex }) => {
5052
setColor(text);

0 commit comments

Comments
 (0)