michael-s-molina commented on a change in pull request #18080:
URL: https://github.com/apache/superset/pull/18080#discussion_r800586965



##########
File path: 
superset-frontend/packages/superset-ui-chart-controls/src/components/labelUtils.tsx
##########
@@ -0,0 +1,81 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import React, { ReactNode } from 'react';
+
+import { t } from '@superset-ui/core';
+import { ColumnMeta, Metric } from '@superset-ui/chart-controls';
+
+export const isLabelTruncated = (labelRef?: React.RefObject<any>): boolean =>
+  !!(
+    labelRef &&
+    labelRef.current &&
+    labelRef.current.scrollWidth > labelRef.current.clientWidth
+  );
+
+export const getColumnLabelText = (column: ColumnMeta): string =>
+  column.verbose_name || column.column_name;
+
+export const getColumnTooltipNode = (
+  column: ColumnMeta,
+  labelRef?: React.RefObject<any>,
+): ReactNode => {
+  // don't show tooltip if it hasn't verbose_name and hasn't truncated
+  if (!column.verbose_name && !isLabelTruncated(labelRef)) {
+    return null;
+  }
+
+  if (column.verbose_name) {
+    return (
+      <>
+        <div>{t('column name: %s', column.column_name)}</div>
+        <div>{t('verbose name: %s', column.verbose_name)}</div>
+      </>
+    );
+  }
+
+  // show column name in tooltip when column truncated
+  return t('column name: %s', column.column_name);
+};
+
+type MetricType = Omit<Metric, 'id'> & { label?: string };
+
+export const getMeticTooltipNode = (

Review comment:
       ```suggestion
   export const getMetricTooltipNode = (
   ```

##########
File path: 
superset-frontend/packages/superset-ui-chart-controls/test/components/labelUtils.test.tsx
##########
@@ -0,0 +1,185 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import React from 'react';
+
+import {
+  getColumnLabelText,
+  getColumnTooltipNode,
+  getMeticTooltipNode,
+} from '../../src/components/labelUtils';
+
+test("should get column name when column doesn't have verbose_name", () => {
+  expect(
+    getColumnLabelText({
+      id: 123,
+      column_name: 'column name',
+      verbose_name: '',
+    }),
+  ).toBe('column name');
+});
+
+test('should get verbose name when column have verbose_name', () => {
+  expect(
+    getColumnLabelText({
+      id: 123,
+      column_name: 'column name',
+      verbose_name: 'verbose name',
+    }),
+  ).toBe('verbose name');
+});
+
+test('should get null as tooltip', () => {
+  const ref = { current: { scrollWidth: 100, clientWidth: 100 } };
+  expect(
+    getColumnTooltipNode(
+      {
+        id: 123,
+        column_name: 'column name',
+        verbose_name: '',
+      },
+      ref,
+    ),
+  ).toBe(null);
+});
+
+test('should get column name as tooltip when it verbose name', () => {

Review comment:
       ```suggestion
   test('should get column name and verbose name as the tooltip when it has a 
verbose name', () => {
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to