Skip to content

Commit

Permalink
feat(FieldContainer): add tooltip next to label with tooltipLabel prop
Browse files Browse the repository at this point in the history
  • Loading branch information
maxime-gendron committed Jan 4, 2022
1 parent f36373e commit c7d924a
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 30 deletions.
72 changes: 64 additions & 8 deletions packages/react/src/components/datepicker/datepicker.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

exports[`Datepicker has startDate 1`] = `
.c2 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #000000;
display: block;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
font-size: 0.875rem;
font-weight: var(--font-normal);
-webkit-letter-spacing: 0.02rem;
Expand Down Expand Up @@ -288,8 +295,15 @@ label + .c3 {

exports[`Datepicker matches snapshot (desktop) 1`] = `
.c2 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #000000;
display: block;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
font-size: 0.75rem;
font-weight: var(--font-normal);
-webkit-letter-spacing: 0.02rem;
Expand Down Expand Up @@ -574,8 +588,15 @@ label + .c3 {

exports[`Datepicker matches snapshot (disabled) 1`] = `
.c2 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #000000;
display: block;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
font-size: 0.75rem;
font-weight: var(--font-normal);
-webkit-letter-spacing: 0.02rem;
Expand Down Expand Up @@ -890,8 +911,15 @@ exports[`Datepicker matches snapshot (invalid) 1`] = `
}
.c2 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #000000;
display: block;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
font-size: 0.75rem;
font-weight: var(--font-normal);
-webkit-letter-spacing: 0.02rem;
Expand Down Expand Up @@ -1198,8 +1226,15 @@ label + .c6 {

exports[`Datepicker matches snapshot (mobile) 1`] = `
.c2 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #000000;
display: block;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
font-size: 0.875rem;
font-weight: var(--font-normal);
-webkit-letter-spacing: 0.02rem;
Expand Down Expand Up @@ -1484,8 +1519,15 @@ label + .c3 {

exports[`Datepicker matches snapshot (open, desktop) 1`] = `
.c2 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #000000;
display: block;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
font-size: 0.75rem;
font-weight: var(--font-normal);
-webkit-letter-spacing: 0.02rem;
Expand Down Expand Up @@ -2609,8 +2651,15 @@ exports[`Datepicker matches snapshot (open, hasTodayButton) 1`] = `
}
.c2 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #000000;
display: block;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
font-size: 0.75rem;
font-weight: var(--font-normal);
-webkit-letter-spacing: 0.02rem;
Expand Down Expand Up @@ -3667,8 +3716,15 @@ label + .c3 {

exports[`Datepicker matches snapshot (open, mobile) 1`] = `
.c2 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #000000;
display: block;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
font-size: 0.875rem;
font-weight: var(--font-normal);
-webkit-letter-spacing: 0.02rem;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { renderWithProviders } from '../../test-utils/renderer';
import { mountWithTheme, renderWithProviders } from '../../test-utils/renderer';
import { FieldContainer } from './field-container';
import { Tooltip } from '../tooltip/tooltip';
import { getByTestId } from '../../test-utils/enzyme-selectors';

const defaultProps = {
fieldId: 'test-id',
Expand All @@ -8,6 +10,16 @@ const defaultProps = {
};

describe('Field Container', () => {
test('label has tooltip when tooltipLabel prop is defined', () => {
const wrapper = mountWithTheme(
<FieldContainer tooltipLabel="test" valid {...defaultProps}>
Children
</FieldContainer>,
);

expect(getByTestId(wrapper, 'field-label').find(Tooltip).exists()).toBe(true);
});

test('matches snapshot', () => {
const tree = renderWithProviders(
<FieldContainer valid {...defaultProps}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

exports[`Field Container matches snapshot 1`] = `
.c2 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #000000;
display: block;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
font-size: 0.75rem;
font-weight: var(--font-normal);
-webkit-letter-spacing: 0.02rem;
Expand Down Expand Up @@ -84,8 +91,15 @@ exports[`Field Container should have invalid styles given valid prop is set to f
}
.c2 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #000000;
display: block;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
font-size: 0.75rem;
font-weight: var(--font-normal);
-webkit-letter-spacing: 0.02rem;
Expand Down Expand Up @@ -158,8 +172,15 @@ input + .c1 {

exports[`Field Container should not have margins given noMargin prop is set to true 1`] = `
.c2 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #000000;
display: block;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
font-size: 0.75rem;
font-weight: var(--font-normal);
-webkit-letter-spacing: 0.02rem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface FieldContainerProps {
valid: boolean;
validationErrorMessage: string;
hint?: string;
tooltipLabel?: string;
}

export function FieldContainer({
Expand All @@ -60,6 +61,7 @@ export function FieldContainer({
validationErrorMessage,
hint,
noMargin,
tooltipLabel,
...props
}: FieldContainerProps): ReactElement {
const { isMobile } = useDeviceContext();
Expand All @@ -73,8 +75,8 @@ export function FieldContainer({
valid={valid}
{...props /* eslint-disable-line react/jsx-props-no-spreading */}
>
{label && <Label data-testid="text-input-label" forId={fieldId}>{label}</Label>}
{hint && <StyledHint data-testid="text-input-hint" isMobile={isMobile}>{hint}</StyledHint>}
{label && <Label data-testid="field-label" forId={fieldId} tooltipLabel={tooltipLabel}>{label}</Label>}
{hint && <StyledHint isMobile={isMobile}>{hint}</StyledHint>}
{!valid && (
<InvalidField
data-testid="text-input-error-msg"
Expand Down
9 changes: 8 additions & 1 deletion packages/react/src/components/label/label.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

exports[`Label Matches the snapshot 1`] = `
.c1 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #000000;
display: block;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
font-size: 0.75rem;
font-weight: var(--font-normal);
-webkit-letter-spacing: 0.02rem;
Expand Down
14 changes: 12 additions & 2 deletions packages/react/src/components/label/label.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { ReactElement, ReactNode } from 'react';
import styled from 'styled-components';
import { useDeviceContext } from '../device-context-provider/device-context-provider';
import { Tooltip } from '../tooltip/tooltip';

const StyledLabel = styled.label<{isMobile: boolean}>`
align-items: center;
color: ${(props) => props.theme.greys.black};
display: block;
display: flex;
font-size: ${({ isMobile }) => (isMobile ? '0.875rem' : '0.75rem')};
font-weight: var(--font-normal);
letter-spacing: 0.02rem;
Expand All @@ -17,18 +19,26 @@ const StyledLabel = styled.label<{isMobile: boolean}>`
}
`;

const StyledTooltip = styled(Tooltip)`
margin-left: calc(var(--spacing-1x) * 1.5);
`;

interface LabelProps {
className?: string;
children: ReactNode;
forId: string;
tooltipLabel?: string;
}

function Label({ className, children, forId }: LabelProps): ReactElement {
function Label({
className, children, forId, tooltipLabel,
}: LabelProps): ReactElement {
const { isMobile } = useDeviceContext();

return (
<StyledLabel className={className} htmlFor={forId} isMobile={isMobile}>
{children}
{tooltipLabel && <StyledTooltip label={tooltipLabel} />}
</StyledLabel>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,15 @@ exports[`SearchInput should match the snapshot 1`] = `
}
.c3 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #000000;
display: block;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
font-size: 0.75rem;
font-weight: var(--font-normal);
-webkit-letter-spacing: 0.02rem;
Expand Down
27 changes: 24 additions & 3 deletions packages/react/src/components/select/select.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

exports[`Select disabled select has a different style 1`] = `
.c3 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #000000;
display: block;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
font-size: 0.75rem;
font-weight: var(--font-normal);
-webkit-letter-spacing: 0.02rem;
Expand Down Expand Up @@ -405,8 +412,15 @@ Array [
}
.c3 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #000000;
display: block;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
font-size: 0.75rem;
font-weight: var(--font-normal);
-webkit-letter-spacing: 0.02rem;
Expand Down Expand Up @@ -892,8 +906,15 @@ input[type="radio"]:disabled + .c1 {
exports[`Select matches the snapshot 1`] = `
Array [
.c3 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #000000;
display: block;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
font-size: 0.75rem;
font-weight: var(--font-normal);
-webkit-letter-spacing: 0.02rem;
Expand Down
Loading

0 comments on commit c7d924a

Please sign in to comment.