Skip to content

Commit b79941e

Browse files
Fix props confusion in TextStylePicker
1 parent 8c00476 commit b79941e

2 files changed

Lines changed: 33 additions & 23 deletions

File tree

x-pack/plugins/canvas/public/components/text_style_picker/__stories__/text_style_picker.stories.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ import React, { useState } from 'react';
88
import { storiesOf } from '@storybook/react';
99
import { action } from '@storybook/addon-actions';
1010

11-
import { TextStylePicker } from '../text_style_picker';
11+
import { TextStylePicker, StyleProps } from '../text_style_picker';
1212

1313
const Interactive = () => {
14-
const [props, setProps] = useState({});
15-
return <TextStylePicker onChange={setProps} {...props} />;
14+
const [style, setStyle] = useState<StyleProps>({});
15+
const onChange = (styleChange: StyleProps) => {
16+
setStyle(styleChange);
17+
action('onChange')(styleChange);
18+
};
19+
return <TextStylePicker onChange={onChange} {...style} />;
1620
};
1721

1822
storiesOf('components/TextStylePicker', module)

x-pack/plugins/canvas/public/components/text_style_picker/text_style_picker.tsx

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import React, { FC, useState, useEffect } from 'react';
7+
import React, { FC, useState } from 'react';
88
import PropTypes from 'prop-types';
99
import { EuiFlexGroup, EuiFlexItem, EuiSelect, EuiSpacer, EuiButtonGroup } from '@elastic/eui';
1010
import { FontValue } from 'src/plugins/expressions';
@@ -15,7 +15,7 @@ import { fontSizes } from './font_sizes';
1515

1616
const { TextStylePicker: strings } = ComponentStrings;
1717

18-
interface BaseProps {
18+
export interface StyleProps {
1919
family?: FontValue;
2020
size?: number;
2121
align?: 'left' | 'center' | 'right';
@@ -25,9 +25,9 @@ interface BaseProps {
2525
italic?: boolean;
2626
}
2727

28-
interface Props extends BaseProps {
28+
export interface Props extends StyleProps {
2929
colors?: string[];
30-
onChange: (props: BaseProps) => void;
30+
onChange: (style: StyleProps) => void;
3131
}
3232

3333
type StyleType = 'bold' | 'italic' | 'underline';
@@ -68,20 +68,26 @@ const styleButtons = [
6868
},
6969
];
7070

71-
export const TextStylePicker: FC<Props> = (props) => {
72-
const [style, setStyle] = useState<Props>(props);
73-
74-
const {
75-
align = 'left',
71+
export const TextStylePicker: FC<Props> = ({
72+
align = 'left',
73+
color,
74+
colors,
75+
family,
76+
italic = false,
77+
onChange,
78+
size = 14,
79+
underline = false,
80+
weight = 'normal',
81+
}) => {
82+
const [style, setStyle] = useState<StyleProps>({
83+
align,
7684
color,
77-
colors,
7885
family,
79-
italic = false,
80-
onChange,
81-
size = 14,
82-
underline = false,
83-
weight = 'normal',
84-
} = style;
86+
italic,
87+
size,
88+
underline,
89+
weight,
90+
});
8591

8692
const stylesSelectedMap: Record<StyleType, boolean> = {
8793
['bold']: weight === 'bold',
@@ -94,10 +100,10 @@ export const TextStylePicker: FC<Props> = (props) => {
94100
fontSizes.sort((a, b) => a - b);
95101
}
96102

97-
useEffect(() => onChange(style), [onChange, style]);
98-
99-
const doChange = (propName: keyof Props, value: string | boolean | number) => {
100-
setStyle({ ...style, [propName]: value });
103+
const doChange = (propName: keyof StyleProps, value: string | boolean | number) => {
104+
const newStyle = { ...style, [propName]: value };
105+
setStyle(newStyle);
106+
onChange(newStyle);
101107
};
102108

103109
const onStyleChange = (optionId: string) => {

0 commit comments

Comments
 (0)