Skip to content

Commit c27b761

Browse files
committed
Add tooltip to checkboxes
1 parent 3db6f5a commit c27b761

4 files changed

Lines changed: 46 additions & 18 deletions

File tree

src/components/Checkbox/Checkbox.stories.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,9 @@ export const Disabled = () => <>
4646
{renderDisabledCheckboxes('light', 1.6)}
4747
{renderDisabledCheckboxes('light', 2)}
4848
</>
49+
50+
export const WithTooltip = () => <CheckboxGroup>
51+
<Checkbox tooltipText="Tooltip text for checkbox goes here">Checkbox label</Checkbox>
52+
<Checkbox disabled tooltipText="This option is unavailable">Disabled label with tooltip</Checkbox>
53+
<Checkbox disabled defaultChecked tooltipText="This option is unavailable">Disabled checked label with tooltip</Checkbox>
54+
</CheckboxGroup>;
Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { LabelHTMLAttributes, PropsWithChildren } from "react";
1+
import React, { LabelHTMLAttributes, PropsWithChildren } from "react";
22
import { checkboxLabelStyles, checkboxInputStyles, CheckboxVariant, CheckboxSize } from "./sharedCheckboxStyles";
33
import styled from "styled-components";
44
import { InputHTMLAttributes } from "react";
5+
import {useTooltipTriggerState} from 'react-stately';
6+
import {useTooltipTrigger} from 'react-aria';
7+
import { CustomTooltip } from '../Tooltip';
58

69
const StyledLabel = styled.label<{ bold: boolean; variant: CheckboxVariant; isDisabled?: boolean; }>`
710
${checkboxLabelStyles}
@@ -12,19 +15,38 @@ const StyledInput = styled.input<{ variant: CheckboxVariant; checkboxSize: Check
1215
${checkboxInputStyles}
1316
`;
1417

18+
const LabelWithTooltipWrapper = styled.div`
19+
display: inline-block;
20+
position: relative;
21+
`;
22+
1523
type CheckboxProps = PropsWithChildren<
1624
Omit<InputHTMLAttributes<HTMLInputElement>, 'type'> & {
1725
variant?: CheckboxVariant;
1826
size?: CheckboxSize;
1927
bold?: boolean;
2028
labelProps?: LabelHTMLAttributes<HTMLLabelElement>;
29+
tooltipText?: string;
2130
}>;
2231

23-
export const Checkbox = ({ children, disabled, variant = 'primary', bold = false, size = 1.6, labelProps, ...props }: CheckboxProps) => {
24-
return (
25-
<StyledLabel bold={bold} variant={variant} isDisabled={disabled} {...labelProps}>
26-
<StyledInput {...props} type="checkbox" variant={variant} checkboxSize={size} isDisabled={disabled} disabled={disabled} />
27-
{children}
28-
</StyledLabel>
29-
);
32+
export const Checkbox = ({ children, disabled, variant = 'primary', bold = false, size = 1.6, labelProps, tooltipText, ...props }: CheckboxProps) => {
33+
const state = useTooltipTriggerState({delay: 0});
34+
const ref = React.useRef(null);
35+
36+
const { triggerProps, tooltipProps } = useTooltipTrigger({delay: 0}, state, ref);
37+
38+
return tooltipText
39+
? <LabelWithTooltipWrapper>
40+
<StyledLabel ref={ref} bold={bold} variant={variant} isDisabled={disabled} {...triggerProps} {...labelProps}>
41+
<StyledInput {...props} type="checkbox" onFocus={() => state.open()} variant={variant} checkboxSize={size} isDisabled={disabled} disabled={disabled} />
42+
{children}
43+
{state.isOpen && (
44+
<CustomTooltip state={state} {...tooltipProps} placement='right'>{tooltipText}</CustomTooltip>
45+
)}
46+
</StyledLabel>
47+
</LabelWithTooltipWrapper>
48+
: <StyledLabel bold={bold} variant={variant} isDisabled={disabled} {...labelProps}>
49+
<StyledInput {...props} type="checkbox" variant={variant} checkboxSize={size} isDisabled={disabled} disabled={disabled} />
50+
{children}
51+
</StyledLabel>;
3052
};

src/components/Checkbox/__snapshots__/Checkbox.spec.tsx.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
exports[`Checkbox allows setting props on label 1`] = `
44
<label
55
aria-label="custom label"
6-
className="sc-bczRLJ XfrOB"
6+
className="sc-hKMtZM gNqItn"
77
>
88
<input
9-
className="sc-gsnTZi dskNep"
9+
className="sc-eCYdqJ gpoUA"
1010
type="checkbox"
1111
/>
1212
Click Me
@@ -15,10 +15,10 @@ exports[`Checkbox allows setting props on label 1`] = `
1515

1616
exports[`Checkbox handles disabled state 1`] = `
1717
<label
18-
className="sc-bczRLJ jDFcYw"
18+
className="sc-hKMtZM uiIZG"
1919
>
2020
<input
21-
className="sc-gsnTZi dJvfbo"
21+
className="sc-eCYdqJ fBIvaF"
2222
disabled={true}
2323
type="checkbox"
2424
/>
@@ -28,10 +28,10 @@ exports[`Checkbox handles disabled state 1`] = `
2828

2929
exports[`Checkbox handles options 1`] = `
3030
<label
31-
className="sc-bczRLJ XfrOB"
31+
className="sc-hKMtZM gNqItn"
3232
>
3333
<input
34-
className="sc-gsnTZi dskNep"
34+
className="sc-eCYdqJ gpoUA"
3535
type="checkbox"
3636
/>
3737
Click Me
@@ -40,10 +40,10 @@ exports[`Checkbox handles options 1`] = `
4040

4141
exports[`Checkbox matches snapshot 1`] = `
4242
<label
43-
className="sc-bczRLJ fIuhsq"
43+
className="sc-hKMtZM dHABFU"
4444
>
4545
<input
46-
className="sc-gsnTZi ehAXiz"
46+
className="sc-eCYdqJ glqLW"
4747
type="checkbox"
4848
/>
4949
Click Me

src/components/Tree/__snapshots__/Tree.spec.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ exports[`Tree matches snapshot 1`] = `
4141
style="display: contents;"
4242
>
4343
<label
44-
class="sc-jSMfEi fnAjTL"
44+
class="sc-ftvSup kBXYav"
4545
>
4646
<input
47-
class="sc-gKXOVf bROlip"
47+
class="sc-papXJ eMMRLt"
4848
slot="check"
4949
type="checkbox"
5050
value="one"

0 commit comments

Comments
 (0)