1- import { LabelHTMLAttributes , PropsWithChildren } from "react" ;
1+ import React , { LabelHTMLAttributes , PropsWithChildren } from "react" ;
22import { checkboxLabelStyles , checkboxInputStyles , CheckboxVariant , CheckboxSize } from "./sharedCheckboxStyles" ;
33import styled from "styled-components" ;
44import { InputHTMLAttributes } from "react" ;
5+ import { useTooltipTriggerState } from 'react-stately' ;
6+ import { useTooltipTrigger } from 'react-aria' ;
7+ import { CustomTooltip } from '../Tooltip' ;
58
69const 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+
1523type 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} ;
0 commit comments