Make toolbar_button a shared component (#88386)

This commit is contained in:
Andrea Del Rio 2021-01-25 14:14:47 -08:00 committed by GitHub
parent 2ae2692f2b
commit fb536f5428
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 292 additions and 27 deletions

View file

@ -15,6 +15,7 @@ export * from './ui_settings';
export * from './field_icon'; export * from './field_icon';
export * from './field_button'; export * from './field_button';
export * from './table_list_view'; export * from './table_list_view';
export * from './toolbar_button';
export * from './split_panel'; export * from './split_panel';
export * from './react_router_navigate'; export * from './react_router_navigate';
export { ValidatedDualRange, Value } from './validated_range'; export { ValidatedDualRange, Value } from './validated_range';

View file

@ -0,0 +1,199 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`font weights bold is applied 1`] = `
<EuiButton
className="kbnToolbarButton kbnToolbarButton--bold kbnToolbarButton--m"
color="text"
contentProps={
Object {
"className": "kbnToolbarButton__content",
}
}
data-test-subj=""
iconSide="right"
iconType="arrowDown"
onClick={[Function]}
size="m"
textProps={
Object {
"className": "kbnToolbarButton__text",
}
}
/>
`;
exports[`font weights normal is applied 1`] = `
<EuiButton
className="kbnToolbarButton kbnToolbarButton--normal kbnToolbarButton--m"
color="text"
contentProps={
Object {
"className": "kbnToolbarButton__content",
}
}
data-test-subj=""
iconSide="right"
iconType="arrowDown"
onClick={[Function]}
size="m"
textProps={
Object {
"className": "kbnToolbarButton__text",
}
}
/>
`;
exports[`hasArrow is rendered 1`] = `
<EuiButton
className="kbnToolbarButton kbnToolbarButton--normal kbnToolbarButton--m"
color="text"
contentProps={
Object {
"className": "kbnToolbarButton__content",
}
}
data-test-subj=""
iconSide="right"
iconType="arrowDown"
onClick={[Function]}
size="m"
textProps={
Object {
"className": "kbnToolbarButton__text",
}
}
/>
`;
exports[`positions center is applied 1`] = `
<EuiButton
className="kbnToolbarButton toolbarButton--groupCenter kbnToolbarButton--normal kbnToolbarButton--m"
color="text"
contentProps={
Object {
"className": "kbnToolbarButton__content",
}
}
data-test-subj=""
iconSide="right"
iconType="arrowDown"
onClick={[Function]}
size="m"
textProps={
Object {
"className": "kbnToolbarButton__text",
}
}
/>
`;
exports[`positions left is applied 1`] = `
<EuiButton
className="kbnToolbarButton toolbarButton--groupLeft kbnToolbarButton--normal kbnToolbarButton--m"
color="text"
contentProps={
Object {
"className": "kbnToolbarButton__content",
}
}
data-test-subj=""
iconSide="right"
iconType="arrowDown"
onClick={[Function]}
size="m"
textProps={
Object {
"className": "kbnToolbarButton__text",
}
}
/>
`;
exports[`positions none is applied 1`] = `
<EuiButton
className="kbnToolbarButton kbnToolbarButton--normal kbnToolbarButton--m"
color="text"
contentProps={
Object {
"className": "kbnToolbarButton__content",
}
}
data-test-subj=""
iconSide="right"
iconType="arrowDown"
onClick={[Function]}
size="m"
textProps={
Object {
"className": "kbnToolbarButton__text",
}
}
/>
`;
exports[`positions right is applied 1`] = `
<EuiButton
className="kbnToolbarButton toolbarButton--groupRight kbnToolbarButton--normal kbnToolbarButton--m"
color="text"
contentProps={
Object {
"className": "kbnToolbarButton__content",
}
}
data-test-subj=""
iconSide="right"
iconType="arrowDown"
onClick={[Function]}
size="m"
textProps={
Object {
"className": "kbnToolbarButton__text",
}
}
/>
`;
exports[`sizes m is applied 1`] = `
<EuiButton
className="kbnToolbarButton kbnToolbarButton--normal kbnToolbarButton--m"
color="text"
contentProps={
Object {
"className": "kbnToolbarButton__content",
}
}
data-test-subj=""
iconSide="right"
iconType="arrowDown"
onClick={[Function]}
size="m"
textProps={
Object {
"className": "kbnToolbarButton__text",
}
}
/>
`;
exports[`sizes s is applied 1`] = `
<EuiButton
className="kbnToolbarButton kbnToolbarButton--normal kbnToolbarButton--s"
color="text"
contentProps={
Object {
"className": "kbnToolbarButton__content",
}
}
data-test-subj=""
iconSide="right"
iconType="arrowDown"
onClick={[Function]}
size="s"
textProps={
Object {
"className": "kbnToolbarButton__text",
}
}
/>
`;

View file

@ -0,0 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* and the Server Side Public License, v 1; you may not use this file except in
* compliance with, at your election, the Elastic License or the Server Side
* Public License, v 1.
*/
export * from './toolbar_button';

View file

@ -1,4 +1,4 @@
.lnsToolbarButton { .kbnToolbarButton {
line-height: $euiButtonHeight; // Keeps alignment of text and chart icon line-height: $euiButtonHeight; // Keeps alignment of text and chart icon
background-color: $euiColorEmptyShade; background-color: $euiColorEmptyShade;
@ -15,11 +15,11 @@
pointer-events: initial; pointer-events: initial;
} }
.lnsToolbarButton__text > svg { .kbnToolbarButton__text > svg {
margin-top: -1px; // Just some weird alignment issue when icon is the child not the `iconType` margin-top: -1px; // Just some weird alignment issue when icon is the child not the `iconType`
} }
.lnsToolbarButton__text:empty { .kbnToolbarButton__text:empty {
margin: 0; margin: 0;
} }
@ -27,34 +27,33 @@
&[class*='fullWidth'] { &[class*='fullWidth'] {
text-align: left; text-align: left;
.lnsToolbarButton__content { .kbnToolbarButton__content {
justify-content: space-between; justify-content: space-between;
} }
} }
} }
.lnsToolbarButton--groupLeft { .kbnToolbarButton--groupLeft {
border-top-right-radius: 0; border-top-right-radius: 0;
border-bottom-right-radius: 0; border-bottom-right-radius: 0;
} }
.lnsToolbarButton--groupCenter { .kbnToolbarButton--groupCenter {
border-radius: 0; border-radius: 0;
border-left: none; border-left: none;
} }
.lnsToolbarButton--groupRight { .kbnToolbarButton--groupRight {
border-top-left-radius: 0; border-top-left-radius: 0;
border-bottom-left-radius: 0; border-bottom-left-radius: 0;
border-left: none; border-left: none;
} }
.lnsToolbarButton--bold { .kbnToolbarButton--bold {
font-weight: $euiFontWeightBold; font-weight: $euiFontWeightBold;
} }
.lnsToolbarButton--s { .kbnToolbarButton--s {
box-shadow: none !important; // sass-lint:disable-line no-important box-shadow: none !important; // sass-lint:disable-line no-important
font-size: $euiFontSizeS; font-size: $euiFontSizeS;
} }

View file

@ -0,0 +1,47 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* and the Server Side Public License, v 1; you may not use this file except in
* compliance with, at your election, the Elastic License or the Server Side
* Public License, v 1.
*/
import React from 'react';
import { shallow } from 'enzyme';
import { ToolbarButton, POSITIONS, WEIGHTS, TOOLBAR_BUTTON_SIZES } from './toolbar_button';
const noop = () => {};
describe('sizes', () => {
TOOLBAR_BUTTON_SIZES.forEach((size) => {
test(`${size} is applied`, () => {
const component = shallow(<ToolbarButton onClick={noop} size={size} />);
expect(component).toMatchSnapshot();
});
});
});
describe('positions', () => {
POSITIONS.forEach((position) => {
test(`${position} is applied`, () => {
const component = shallow(<ToolbarButton onClick={noop} groupPosition={position} />);
expect(component).toMatchSnapshot();
});
});
});
describe('font weights', () => {
WEIGHTS.forEach((weight) => {
test(`${weight} is applied`, () => {
const component = shallow(<ToolbarButton onClick={noop} fontWeight={weight} />);
expect(component).toMatchSnapshot();
});
});
});
describe('hasArrow', () => {
it('is rendered', () => {
const component = shallow(<ToolbarButton onClick={noop} hasArrow />);
expect(component).toMatchSnapshot();
});
});

View file

@ -1,7 +1,9 @@
/* /*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License; * or more contributor license agreements. Licensed under the Elastic License
* you may not use this file except in compliance with the Elastic License. * and the Server Side Public License, v 1; you may not use this file except in
* compliance with, at your election, the Elastic License or the Server Side
* Public License, v 1.
*/ */
import './toolbar_button.scss'; import './toolbar_button.scss';
@ -11,16 +13,24 @@ import { EuiButton, PropsOf, EuiButtonProps } from '@elastic/eui';
const groupPositionToClassMap = { const groupPositionToClassMap = {
none: null, none: null,
left: 'lnsToolbarButton--groupLeft', left: 'toolbarButton--groupLeft',
center: 'lnsToolbarButton--groupCenter', center: 'toolbarButton--groupCenter',
right: 'lnsToolbarButton--groupRight', right: 'toolbarButton--groupRight',
}; };
type ButtonPositions = keyof typeof groupPositionToClassMap;
export const POSITIONS = Object.keys(groupPositionToClassMap) as ButtonPositions[];
type Weights = 'normal' | 'bold';
export const WEIGHTS = ['normal', 'bold'] as Weights[];
export const TOOLBAR_BUTTON_SIZES: Array<EuiButtonProps['size']> = ['s', 'm'];
export type ToolbarButtonProps = PropsOf<typeof EuiButton> & { export type ToolbarButtonProps = PropsOf<typeof EuiButton> & {
/** /**
* Determines prominence * Determines prominence
*/ */
fontWeight?: 'normal' | 'bold'; fontWeight?: Weights;
/** /**
* Smaller buttons also remove extra shadow for less prominence * Smaller buttons also remove extra shadow for less prominence
*/ */
@ -32,7 +42,7 @@ export type ToolbarButtonProps = PropsOf<typeof EuiButton> & {
/** /**
* Adjusts the borders for groupings * Adjusts the borders for groupings
*/ */
groupPosition?: 'none' | 'left' | 'center' | 'right'; groupPosition?: ButtonPositions;
dataTestSubj?: string; dataTestSubj?: string;
}; };
@ -47,9 +57,9 @@ export const ToolbarButton: React.FunctionComponent<ToolbarButtonProps> = ({
...rest ...rest
}) => { }) => {
const classes = classNames( const classes = classNames(
'lnsToolbarButton', 'kbnToolbarButton',
groupPositionToClassMap[groupPosition], groupPositionToClassMap[groupPosition],
[`lnsToolbarButton--${fontWeight}`, `lnsToolbarButton--${size}`], [`kbnToolbarButton--${fontWeight}`, `kbnToolbarButton--${size}`],
className className
); );
return ( return (
@ -60,10 +70,10 @@ export const ToolbarButton: React.FunctionComponent<ToolbarButtonProps> = ({
iconType={hasArrow ? 'arrowDown' : ''} iconType={hasArrow ? 'arrowDown' : ''}
color="text" color="text"
contentProps={{ contentProps={{
className: 'lnsToolbarButton__content', className: 'kbnToolbarButton__content',
}} }}
textProps={{ textProps={{
className: 'lnsToolbarButton__text', className: 'kbnToolbarButton__text',
}} }}
{...rest} {...rest}
size={size} size={size}

View file

@ -9,7 +9,7 @@ import { EuiPopover, EuiToolTip } from '@elastic/eui';
import { i18n } from '@kbn/i18n'; import { i18n } from '@kbn/i18n';
import { NativeRenderer } from '../../../native_renderer'; import { NativeRenderer } from '../../../native_renderer';
import { Visualization, VisualizationLayerWidgetProps } from '../../../types'; import { Visualization, VisualizationLayerWidgetProps } from '../../../types';
import { ToolbarButton } from '../../../shared_components'; import { ToolbarButton } from '../../../../../../../src/plugins/kibana_react/public';
export function LayerSettings({ export function LayerSettings({
layerId, layerId,

View file

@ -24,7 +24,7 @@ import { Visualization, FramePublicAPI, Datasource } from '../../../types';
import { Action } from '../state_management'; import { Action } from '../state_management';
import { getSuggestions, switchToSuggestion, Suggestion } from '../suggestion_helpers'; import { getSuggestions, switchToSuggestion, Suggestion } from '../suggestion_helpers';
import { trackUiEvent } from '../../../lens_ui_telemetry'; import { trackUiEvent } from '../../../lens_ui_telemetry';
import { ToolbarButton } from '../../../shared_components'; import { ToolbarButton } from '../../../../../../../src/plugins/kibana_react/public';
interface VisualizationSelection { interface VisualizationSelection {
visualizationId: string; visualizationId: string;

View file

@ -9,7 +9,7 @@ import React, { useState } from 'react';
import { EuiPopover, EuiPopoverTitle, EuiSelectable, EuiSelectableProps } from '@elastic/eui'; import { EuiPopover, EuiPopoverTitle, EuiSelectable, EuiSelectableProps } from '@elastic/eui';
import { IndexPatternRef } from './types'; import { IndexPatternRef } from './types';
import { trackUiEvent } from '../lens_ui_telemetry'; import { trackUiEvent } from '../lens_ui_telemetry';
import { ToolbarButtonProps, ToolbarButton } from '../shared_components'; import { ToolbarButton, ToolbarButtonProps } from '../../../../../src/plugins/kibana_react/public';
export type ChangeIndexPatternTriggerProps = ToolbarButtonProps & { export type ChangeIndexPatternTriggerProps = ToolbarButtonProps & {
label: string; label: string;

View file

@ -6,6 +6,5 @@
export * from './empty_placeholder'; export * from './empty_placeholder';
export { ToolbarPopoverProps, ToolbarPopover } from './toolbar_popover'; export { ToolbarPopoverProps, ToolbarPopover } from './toolbar_popover';
export { ToolbarButtonProps, ToolbarButton } from './toolbar_button';
export { LegendSettingsPopover } from './legend_settings_popover'; export { LegendSettingsPopover } from './legend_settings_popover';
export { PalettePicker } from './palette_picker'; export { PalettePicker } from './palette_picker';

View file

@ -6,8 +6,8 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { EuiFlexItem, EuiPopover, EuiIcon, EuiPopoverTitle, IconType } from '@elastic/eui'; import { EuiFlexItem, EuiPopover, EuiIcon, EuiPopoverTitle, IconType } from '@elastic/eui';
import { ToolbarButton, ToolbarButtonProps } from './toolbar_button';
import { EuiIconLegend } from '../assets/legend'; import { EuiIconLegend } from '../assets/legend';
import { ToolbarButton, ToolbarButtonProps } from '../../../../../src/plugins/kibana_react/public';
const typeToIconMap: { [type: string]: string | IconType } = { const typeToIconMap: { [type: string]: string | IconType } = {
legend: EuiIconLegend as IconType, legend: EuiIconLegend as IconType,

View file

@ -16,12 +16,13 @@ import {
} from '@elastic/eui'; } from '@elastic/eui';
import { i18n } from '@kbn/i18n'; import { i18n } from '@kbn/i18n';
import { LayerConfig, AxesSettingsConfig } from './types'; import { LayerConfig, AxesSettingsConfig } from './types';
import { ToolbarPopover, ToolbarButtonProps } from '../shared_components'; import { ToolbarPopover } from '../shared_components';
import { isHorizontalChart } from './state_helpers'; import { isHorizontalChart } from './state_helpers';
import { EuiIconAxisBottom } from '../assets/axis_bottom'; import { EuiIconAxisBottom } from '../assets/axis_bottom';
import { EuiIconAxisLeft } from '../assets/axis_left'; import { EuiIconAxisLeft } from '../assets/axis_left';
import { EuiIconAxisRight } from '../assets/axis_right'; import { EuiIconAxisRight } from '../assets/axis_right';
import { EuiIconAxisTop } from '../assets/axis_top'; import { EuiIconAxisTop } from '../assets/axis_top';
import { ToolbarButtonProps } from '../../../../../src/plugins/kibana_react/public';
type AxesSettingsConfigKeys = keyof AxesSettingsConfig; type AxesSettingsConfigKeys = keyof AxesSettingsConfig;
export interface AxisSettingsPopoverProps { export interface AxisSettingsPopoverProps {