From 2c5731aea37cf85e07d079f819edb3910b13d50b Mon Sep 17 00:00:00 2001 From: Geido <60598000+geido@users.noreply.github.com> Date: Thu, 12 Aug 2021 19:42:48 +0200 Subject: [PATCH] Show/hide tooltips (#16192) --- .../DatasourcePanelDragOption.test.tsx} | 20 +++---- .../index.tsx | 43 +++++++++++---- .../components/DatasourcePanel/index.tsx | 54 +++++++++++++++---- 3 files changed, 85 insertions(+), 32 deletions(-) rename superset-frontend/src/explore/components/DatasourcePanel/{DatasourcePanelDragWrapper/DatasourcePanelDragWrapper.test.tsx => DatasourcePanelDragOption/DatasourcePanelDragOption.test.tsx} (75%) rename superset-frontend/src/explore/components/DatasourcePanel/{DatasourcePanelDragWrapper => DatasourcePanelDragOption}/index.tsx (52%) diff --git a/superset-frontend/src/explore/components/DatasourcePanel/DatasourcePanelDragWrapper/DatasourcePanelDragWrapper.test.tsx b/superset-frontend/src/explore/components/DatasourcePanel/DatasourcePanelDragOption/DatasourcePanelDragOption.test.tsx similarity index 75% rename from superset-frontend/src/explore/components/DatasourcePanel/DatasourcePanelDragWrapper/DatasourcePanelDragWrapper.test.tsx rename to superset-frontend/src/explore/components/DatasourcePanel/DatasourcePanelDragOption/DatasourcePanelDragOption.test.tsx index 7698f7328..be8f91ead 100644 --- a/superset-frontend/src/explore/components/DatasourcePanel/DatasourcePanelDragWrapper/DatasourcePanelDragWrapper.test.tsx +++ b/superset-frontend/src/explore/components/DatasourcePanel/DatasourcePanelDragOption/DatasourcePanelDragOption.test.tsx @@ -21,37 +21,33 @@ import { DndProvider } from 'react-dnd'; import { HTML5Backend } from 'react-dnd-html5-backend'; import { render, screen } from 'spec/helpers/testing-library'; import { DndItemType } from 'src/explore/components/DndItemType'; -import DatasourcePanelDragWrapper from '.'; +import DatasourcePanelDragOption from '.'; test('should render', () => { render( - -
- + /> , ); - expect(screen.getByTestId('DatasourcePanelDragWrapper')).toBeInTheDocument(); - expect(screen.getByTestId('children')).toBeInTheDocument(); + expect(screen.getByTestId('DatasourcePanelDragOption')).toBeInTheDocument(); + expect(screen.getByText('test')).toBeInTheDocument(); }); test('should have attribute draggable:true', () => { render( - -
- + /> , ); - expect(screen.getByTestId('DatasourcePanelDragWrapper')).toHaveAttribute( + expect(screen.getByTestId('DatasourcePanelDragOption')).toHaveAttribute( 'draggable', 'true', ); diff --git a/superset-frontend/src/explore/components/DatasourcePanel/DatasourcePanelDragWrapper/index.tsx b/superset-frontend/src/explore/components/DatasourcePanel/DatasourcePanelDragOption/index.tsx similarity index 52% rename from superset-frontend/src/explore/components/DatasourcePanel/DatasourcePanelDragWrapper/index.tsx rename to superset-frontend/src/explore/components/DatasourcePanel/DatasourcePanelDragOption/index.tsx index c4ad3952e..518f7ebab 100644 --- a/superset-frontend/src/explore/components/DatasourcePanel/DatasourcePanelDragWrapper/index.tsx +++ b/superset-frontend/src/explore/components/DatasourcePanel/DatasourcePanelDragOption/index.tsx @@ -16,9 +16,15 @@ * specific language governing permissions and limitations * under the License. */ -import React, { ReactNode } from 'react'; +import React from 'react'; import { useDrag } from 'react-dnd'; -import { styled } from '@superset-ui/core'; +import { Metric, styled } from '@superset-ui/core'; +import { DndItemType } from 'src/explore/components/DndItemType'; +import { + StyledColumnOption, + StyledMetricOption, +} from 'src/explore/components/optionRenderers'; +import { ColumnMeta } from '@superset-ui/chart-controls'; import { DatasourcePanelDndItem } from '../types'; const DatasourceItemContainer = styled.div` @@ -37,23 +43,42 @@ const DatasourceItemContainer = styled.div` } `; -interface DatasourcePanelDragWrapperProps extends DatasourcePanelDndItem { - children?: ReactNode; +interface DatasourcePanelDragOptionProps extends DatasourcePanelDndItem { + labelRef?: React.RefObject; + showTooltip?: boolean; } -export default function DatasourcePanelDragWrapper( - props: DatasourcePanelDragWrapperProps, +type MetricOption = Omit & { + label?: string; +}; + +export default function DatasourcePanelDragOption( + props: DatasourcePanelDragOptionProps, ) { - const [, drag] = useDrag({ + const { labelRef, showTooltip, type, value } = props; + const [{ isDragging }, drag] = useDrag({ item: { value: props.value, type: props.type, }, + collect: monitor => ({ + isDragging: monitor.isDragging(), + }), }); + const optionProps = { + labelRef, + showTooltip: !isDragging && showTooltip, + showType: true, + }; + return ( - - {props.children} + + {type === DndItemType.Column ? ( + + ) : ( + + )} ); } diff --git a/superset-frontend/src/explore/components/DatasourcePanel/index.tsx b/superset-frontend/src/explore/components/DatasourcePanel/index.tsx index 2c5caa6c3..bc436cf66 100644 --- a/superset-frontend/src/explore/components/DatasourcePanel/index.tsx +++ b/superset-frontend/src/explore/components/DatasourcePanel/index.tsx @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -import React, { useEffect, useMemo, useState } from 'react'; +import React, { useEffect, useMemo, useRef, useState } from 'react'; import { styled, t } from '@superset-ui/core'; import Collapse from 'src/components/Collapse'; import { ControlConfig, DatasourceMeta } from '@superset-ui/chart-controls'; @@ -26,7 +26,7 @@ import { FAST_DEBOUNCE } from 'src/constants'; import { FeatureFlag, isFeatureEnabled } from 'src/featureFlags'; import { ExploreActions } from 'src/explore/actions/exploreActions'; import Control from 'src/explore/components/Control'; -import DatasourcePanelDragWrapper from './DatasourcePanelDragWrapper'; +import DatasourcePanelDragOption from './DatasourcePanelDragOption'; import { DndItemType } from '../DndItemType'; import { StyledColumnOption, StyledMetricOption } from '../optionRenderers'; @@ -87,7 +87,7 @@ const DatasourceContainer = styled.div` } `; -const LabelContainer = styled.div` +const LabelWrapper = styled.div` overflow: hidden; text-overflow: ellipsis; @@ -110,6 +110,42 @@ const LabelContainer = styled.div` } `; +const LabelContainer = (props: { + children: React.ReactElement; + className: string; +}) => { + const labelRef = useRef(null); + const [showTooltip, setShowTooltip] = useState(true); + const isLabelTruncated = () => + !!( + labelRef && + labelRef.current && + labelRef.current.scrollWidth > labelRef.current.clientWidth + ); + const handleShowTooltip = () => { + const shouldShowTooltip = isLabelTruncated(); + if (shouldShowTooltip !== showTooltip) { + setShowTooltip(shouldShowTooltip); + } + }; + const handleResetTooltip = () => { + setShowTooltip(true); + }; + const extendedProps = { + labelRef, + showTooltip, + }; + return ( + + {React.cloneElement(props.children, extendedProps)} + + ); +}; + const enableExploreDnd = isFeatureEnabled( FeatureFlag.ENABLE_EXPLORE_DRAG_AND_DROP, ); @@ -245,12 +281,10 @@ export default function DataSourcePanel({ {metricSlice.map(m => ( {enableExploreDnd ? ( - - - + /> ) : ( )} @@ -276,12 +310,10 @@ export default function DataSourcePanel({ {columnSlice.map(col => ( {enableExploreDnd ? ( - - - + /> ) : ( )}