Skip to content

(fix) Make the whole label as tooltip trigger #475

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
35 changes: 31 additions & 4 deletions src/components/field-label/field-label.component.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import { Tooltip as CarbonTooltip } from '@carbon/react';
import { useTranslation } from 'react-i18next';
import { type FormField } from '../../types';
import Tooltip from '../inputs/tooltip/tooltip.component';

import styles from './field-label.scss';
import { Information } from '@carbon/react/icons';

interface FieldLabelProps {
field: FormField;
Expand All @@ -13,20 +14,46 @@ interface FieldLabelProps {
customLabel?: string;
}

const FieldLabel: React.FC<FieldLabelProps> = ({ field, customLabel }) => {
const Tooltip: React.FC<{ field: FormField; children: React.ReactNode }> = ({ field, children }) => {
const { t } = useTranslation();
return (
<CarbonTooltip align="top-left" label={t(field.questionInfo)} defaultOpen>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think defaultOpen should not be enabled.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I removed this, apologies.

{children}
</CarbonTooltip>
);
};

interface FieldLabelContentProps extends FieldLabelProps {
hasTooltip: boolean;
}

const FieldLabelContent: React.FC<FieldLabelContentProps> = ({ field, customLabel, hasTooltip }) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vasharma05 Splitting these components feels overkill. Here is a simplified version: https://gist.github.com/samuelmale/daef56444bc7e8ea79c044dc842dec37

const { t } = useTranslation();
const labelText = customLabel || t(field.label);
return (
<div className={styles.questionLabel}>
<div className={styles.questionLabel} data-testid={`${field.id}-label`}>
<span>{labelText}</span>
{field.isRequired && (
<span title={t('required', 'Required')} className={styles.required}>
*
</span>
)}
{field.questionInfo && <Tooltip field={field} />}
{hasTooltip && (
<Information size={20} aria-hidden="true" className={styles.tooltipIcon} data-testid="information-icon" />
)}
</div>
);
};

const FieldLabel: React.FC<FieldLabelProps> = ({ field, customLabel }) => {
const hasTooltip = Boolean(field.questionInfo);
return hasTooltip ? (
<Tooltip field={field}>
<FieldLabelContent field={field} customLabel={customLabel} hasTooltip={hasTooltip} />
</Tooltip>
) : (
<FieldLabelContent field={field} customLabel={customLabel} hasTooltip={hasTooltip} />
);
};

export default FieldLabel;
9 changes: 9 additions & 0 deletions src/components/field-label/field-label.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@
display: flex;
align-items: center;
}

.tooltip {
display: flex;
align-items: center;
}

.tooltipIcon {
margin-left: 0.25rem;
}
23 changes: 0 additions & 23 deletions src/components/inputs/tooltip/tooltip.component.tsx

This file was deleted.

8 changes: 0 additions & 8 deletions src/components/inputs/tooltip/tooltip.scss

This file was deleted.

5 changes: 4 additions & 1 deletion src/form-engine.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,12 @@ describe('Form engine component', () => {

screen.findByRole('textbox', { name: /text question/i });

const textFieldTooltip = screen.getByTestId('id_text');
const textFieldTooltip = screen.getByTestId('id_text-label');
expect(textFieldTooltip).toBeInTheDocument();

const informationIcon = screen.getByTestId('information-icon');
expect(informationIcon).toBeInTheDocument();

await user.hover(textFieldTooltip);
await screen.findByText(/sample tooltip info for text/i);
});
Expand Down