diff --git a/x-pack/plugins/lens/public/shared_components/columns_number_setting.test.tsx b/x-pack/plugins/lens/public/shared_components/columns_number_setting.test.tsx new file mode 100644 index 0000000000000..50f2dc2fb93dc --- /dev/null +++ b/x-pack/plugins/lens/public/shared_components/columns_number_setting.test.tsx @@ -0,0 +1,19 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import React from 'react'; +import { mountWithIntl as mount } from '@kbn/test-jest-helpers'; +import { ColumnsNumberSetting } from './columns_number_setting'; + +describe('Columns Number Setting', () => { + it('should have default the columns input to 1 when no value is given', () => { + const component = mount(); + expect( + component.find('[data-test-subj="lens-legend-location-columns-input"]').at(0).prop('value') + ).toEqual(1); + }); +}); diff --git a/x-pack/plugins/lens/public/shared_components/columns_number_setting.tsx b/x-pack/plugins/lens/public/shared_components/columns_number_setting.tsx new file mode 100644 index 0000000000000..6a1d1ec6d0306 --- /dev/null +++ b/x-pack/plugins/lens/public/shared_components/columns_number_setting.tsx @@ -0,0 +1,84 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import React from 'react'; +import { i18n } from '@kbn/i18n'; +import { EuiFieldNumber, EuiFormRow } from '@elastic/eui'; +import { useDebouncedValue } from './debounced_value'; +import { TooltipWrapper } from './tooltip_wrapper'; + +export const DEFAULT_FLOATING_COLUMNS = 1; + +interface ColumnsNumberSettingProps { + /** + * Sets the number of columns for legend inside chart + */ + floatingColumns?: number; + /** + * Callback on horizontal alignment option change + */ + onFloatingColumnsChange?: (value: number) => void; + /** + * Flag to disable the location settings + */ + isDisabled: boolean; + /** + * Indicates if legend is located outside + */ + isLegendOutside: boolean; +} + +export const ColumnsNumberSetting = ({ + floatingColumns, + onFloatingColumnsChange = () => {}, + isDisabled, + isLegendOutside, +}: ColumnsNumberSettingProps) => { + const { inputValue, handleInputChange } = useDebouncedValue({ + value: floatingColumns ?? DEFAULT_FLOATING_COLUMNS, + onChange: onFloatingColumnsChange, + }); + + return ( + + + { + handleInputChange(Number(e.target.value)); + }} + step={1} + /> + + + ); +}; diff --git a/x-pack/plugins/lens/public/shared_components/legend_location_settings.test.tsx b/x-pack/plugins/lens/public/shared_components/legend_location_settings.test.tsx index 49a53c1abf66f..f4b5ced490663 100644 --- a/x-pack/plugins/lens/public/shared_components/legend_location_settings.test.tsx +++ b/x-pack/plugins/lens/public/shared_components/legend_location_settings.test.tsx @@ -7,7 +7,7 @@ import React from 'react'; import { Position } from '@elastic/charts'; -import { shallowWithIntl as shallow, mountWithIntl as mount } from '@kbn/test-jest-helpers'; +import { shallowWithIntl as shallow } from '@kbn/test-jest-helpers'; import { LegendLocationSettings, LegendLocationSettingsProps } from './legend_location_settings'; describe('Legend Location Settings', () => { @@ -104,17 +104,6 @@ describe('Legend Location Settings', () => { expect(newProps.onAlignmentChange).toHaveBeenCalled(); }); - it('should have default the columns input to 1 when no value is given', () => { - const newProps = { - ...props, - location: 'inside', - } as LegendLocationSettingsProps; - const component = mount(); - expect( - component.find('[data-test-subj="lens-legend-location-columns-input"]').at(0).prop('value') - ).toEqual(1); - }); - it('should disable the components when is Disabled is true', () => { const newProps = { ...props, diff --git a/x-pack/plugins/lens/public/shared_components/legend_location_settings.tsx b/x-pack/plugins/lens/public/shared_components/legend_location_settings.tsx index 6791d5586d327..f3ac54ab00a05 100644 --- a/x-pack/plugins/lens/public/shared_components/legend_location_settings.tsx +++ b/x-pack/plugins/lens/public/shared_components/legend_location_settings.tsx @@ -7,9 +7,8 @@ import React from 'react'; import { i18n } from '@kbn/i18n'; -import { EuiFormRow, EuiButtonGroup, EuiFieldNumber } from '@elastic/eui'; +import { EuiFormRow, EuiButtonGroup } from '@elastic/eui'; import { VerticalAlignment, HorizontalAlignment, Position } from '@elastic/charts'; -import { useDebouncedValue } from './debounced_value'; import { TooltipWrapper } from './tooltip_wrapper'; export interface LegendLocationSettingsProps { @@ -41,22 +40,12 @@ export interface LegendLocationSettingsProps { * Callback on horizontal alignment option change */ onAlignmentChange?: (id: string) => void; - /** - * Sets the number of columns for legend inside chart - */ - floatingColumns?: number; - /** - * Callback on horizontal alignment option change - */ - onFloatingColumnsChange?: (value: number) => void; /** * Flag to disable the location settings */ isDisabled?: boolean; } -const DEFAULT_FLOATING_COLUMNS = 1; - const toggleButtonsIcons = [ { id: Position.Top, @@ -149,32 +138,6 @@ const locationAlignmentButtonsIcons: Array<{ }, ]; -const FloatingColumnsInput = ({ - value, - setValue, - isDisabled, -}: { - value: number; - setValue: (value: number) => void; - isDisabled: boolean; -}) => { - const { inputValue, handleInputChange } = useDebouncedValue({ value, onChange: setValue }); - return ( - { - handleInputChange(Number(e.target.value)); - }} - step={1} - /> - ); -}; - export const LegendLocationSettings: React.FunctionComponent = ({ location, onLocationChange = () => {}, @@ -183,8 +146,6 @@ export const LegendLocationSettings: React.FunctionComponent {}, - floatingColumns, - onFloatingColumnsChange = () => {}, isDisabled = false, }) => { const alignment = `${verticalAlignment || VerticalAlignment.Top}_${ @@ -294,37 +255,6 @@ export const LegendLocationSettings: React.FunctionComponent - {location && ( - - - - - - )} ); }; diff --git a/x-pack/plugins/lens/public/shared_components/legend_settings_popover.tsx b/x-pack/plugins/lens/public/shared_components/legend_settings_popover.tsx index 15e6697d6c5fa..481c38815d43d 100644 --- a/x-pack/plugins/lens/public/shared_components/legend_settings_popover.tsx +++ b/x-pack/plugins/lens/public/shared_components/legend_settings_popover.tsx @@ -17,6 +17,7 @@ import { import { Position, VerticalAlignment, HorizontalAlignment } from '@elastic/charts'; import { ToolbarPopover } from '../shared_components'; import { LegendLocationSettings } from './legend_location_settings'; +import { ColumnsNumberSetting } from './columns_number_setting'; import { LegendSizeSettings } from './legend_size_settings'; import { ToolbarButtonProps } from '../../../../../src/plugins/kibana_react/public'; import { TooltipWrapper } from './tooltip_wrapper'; @@ -223,8 +224,6 @@ export const LegendSettingsPopover: React.FunctionComponent + {location && ( + + )} void; @@ -25,31 +25,41 @@ interface LegendSizeSettingsProps { isDisabled: boolean; } -const MIN_SIZE_VALUE = 1; -const DEFAULT_SIZE_VALUE = 100; - -const WIDTH_LABEL = i18n.translate('xpack.lens.shared.legendSizeSetting.labelWidth', { - defaultMessage: 'width', -}); - -const HEIGHT_LABEL = i18n.translate('xpack.lens.shared.legendSizeSetting.labelHeight', { - defaultMessage: 'height', -}); - -const VERTICAL_TOOLTIP_LABEL = i18n.translate( - 'xpack.lens.shared.legendSizeSetting.tooltipLabelTVertical', +const legendSizeOptions: Array<{ value: LegendSizes; inputDisplay: string }> = [ { - defaultMessage: - 'Vertical legends have minimum of 30% and maximum of 70% of the visualization container width.', - } -); - -const HORIZONTAL_TOOLTIP_LABEL = i18n.translate( - 'xpack.lens.shared.legendSizeSetting.tooltipLabelHorizontal', + value: LegendSizes.AUTO, + inputDisplay: i18n.translate('xpack.lens.shared.legendSizeSetting.legendSizeOptions.auto', { + defaultMessage: 'auto', + }), + }, { - defaultMessage: 'Horizontal legends have maximum of 70% of the visualization height.', - } -); + value: LegendSizes.SMALL, + inputDisplay: i18n.translate('xpack.lens.shared.legendSizeSetting.legendSizeOptions.small', { + defaultMessage: 'small', + }), + }, + { + value: LegendSizes.MEDIUM, + inputDisplay: i18n.translate('xpack.lens.shared.legendSizeSetting.legendSizeOptions.medium', { + defaultMessage: 'medium', + }), + }, + { + value: LegendSizes.LARGE, + inputDisplay: i18n.translate('xpack.lens.shared.legendSizeSetting.legendSizeOptions.large', { + defaultMessage: 'large', + }), + }, + { + value: LegendSizes.EXTRA_LARGE, + inputDisplay: i18n.translate( + 'xpack.lens.shared.legendSizeSetting.legendSizeOptions.extraLarge', + { + defaultMessage: 'extra large', + } + ), + }, +]; export const LegendSizeSettings = ({ legendSize, @@ -57,102 +67,48 @@ export const LegendSizeSettings = ({ isVerticalLegend, isDisabled, }: LegendSizeSettingsProps) => { - const sizeProperty = isVerticalLegend ? WIDTH_LABEL : HEIGHT_LABEL; - const autoSizeLabel = i18n.translate('xpack.lens.shared.legendSizeSetting.switch', { - defaultMessage: 'Auto {sizeProperty}', - values: { sizeProperty }, - }); - - const [inputValue, setInputValue] = useState(legendSize ?? DEFAULT_SIZE_VALUE); - const [isAutoSizeEnabled, setIsAutoSizeEnabled] = useState(!legendSize); - - const handleAutoSizeChange = useCallback(() => { - const shouldUseAutoSize = !isAutoSizeEnabled; - setIsAutoSizeEnabled(shouldUseAutoSize); - onLegendSizeChange(shouldUseAutoSize ? undefined : inputValue); - }, [onLegendSizeChange, inputValue, isAutoSizeEnabled]); + useEffect(() => { + if (legendSize && !isVerticalLegend) { + onLegendSizeChange(undefined); + } + }, [isVerticalLegend, legendSize, onLegendSizeChange]); - const handleLegendSizeChange = useCallback( - (e) => { - const value = Math.max(Number(e.target.value), MIN_SIZE_VALUE); - setInputValue(value); - onLegendSizeChange(value); - }, + const onLegendSizeOptionChange = useCallback( + (option) => onLegendSizeChange(Number(option) || undefined), [onLegendSizeChange] ); return ( - <> - - - - - - - - - - - - - + + - - - - - + + + ); };