Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Canvas][fatal bug] Fix props confusion in TextStylePicker #73732

Merged
merged 1 commit into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import React, { useState } from 'react';
import { storiesOf } from '@storybook/react';
import { action } from '@storybook/addon-actions';

import { TextStylePicker } from '../text_style_picker';
import { TextStylePicker, StyleProps } from '../text_style_picker';

const Interactive = () => {
const [props, setProps] = useState({});
return <TextStylePicker onChange={setProps} {...props} />;
const [style, setStyle] = useState<StyleProps>({});
const onChange = (styleChange: StyleProps) => {
setStyle(styleChange);
action('onChange')(styleChange);
};
return <TextStylePicker onChange={onChange} {...style} />;
};

storiesOf('components/TextStylePicker', module)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

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

const { TextStylePicker: strings } = ComponentStrings;

interface BaseProps {
export interface StyleProps {
family?: FontValue;
size?: number;
align?: 'left' | 'center' | 'right';
Expand All @@ -25,9 +25,9 @@ interface BaseProps {
italic?: boolean;
}

interface Props extends BaseProps {
export interface Props extends StyleProps {
colors?: string[];
onChange: (props: BaseProps) => void;
onChange: (style: StyleProps) => void;
}

type StyleType = 'bold' | 'italic' | 'underline';
Expand Down Expand Up @@ -68,20 +68,26 @@ const styleButtons = [
},
];

export const TextStylePicker: FC<Props> = (props) => {
const [style, setStyle] = useState<Props>(props);

const {
align = 'left',
export const TextStylePicker: FC<Props> = ({
align = 'left',
color,
colors,
family,
italic = false,
onChange,
size = 14,
underline = false,
weight = 'normal',
}) => {
const [style, setStyle] = useState<StyleProps>({
align,
color,
colors,
family,
italic = false,
onChange,
size = 14,
underline = false,
weight = 'normal',
} = style;
italic,
size,
underline,
weight,
});

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

useEffect(() => onChange(style), [onChange, style]);

const doChange = (propName: keyof Props, value: string | boolean | number) => {
setStyle({ ...style, [propName]: value });
const doChange = (propName: keyof StyleProps, value: string | boolean | number) => {
const newStyle = { ...style, [propName]: value };
setStyle(newStyle);
onChange(newStyle);
};

const onStyleChange = (optionId: string) => {
Expand Down