Skip to content

Commit a449657

Browse files
committed
TooltipContent is made independent as a component and used in Tbas and FormElement
1 parent 024e544 commit a449657

File tree

3 files changed

+54
-37
lines changed

3 files changed

+54
-37
lines changed

src/scripts/FormElement.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import React, {
1010
import classnames from 'classnames';
1111
import { FieldSetColumnContext } from './FieldSet';
1212
import { createFC } from './common';
13+
import { TooltipContent } from './TooltipContent';
1314

1415
/**
1516
*
@@ -26,6 +27,8 @@ export type FormElementProps = {
2627
elementRef?: Ref<HTMLDivElement>;
2728
style?: CSSProperties;
2829
children?: ReactNode;
30+
tooltip?: ReactNode;
31+
tooltipIcon?: string;
2932
};
3033

3134
/**
@@ -47,6 +50,8 @@ export const FormElement = createFC<
4750
dropdown,
4851
children,
4952
readOnly,
53+
tooltip,
54+
tooltipIcon,
5055
} = props;
5156

5257
const controlElRef = useRef<HTMLDivElement>(null);
@@ -99,6 +104,9 @@ export const FormElement = createFC<
99104
{required ? <abbr className='slds-required'>*</abbr> : undefined}
100105
</label>
101106
) : null}
107+
{tooltip ? (
108+
<TooltipContent icon={tooltipIcon}>{tooltip}</TooltipContent>
109+
) : null}
102110
<div ref={controlElRef} className={formElementControlClassNames}>
103111
{children}
104112
{dropdown}

src/scripts/Tabs.tsx

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, {
22
FC,
3-
FocusEvent,
43
ComponentType,
54
HTMLAttributes,
65
ReactElement,
@@ -12,15 +11,13 @@ import React, {
1211
useRef,
1312
useState,
1413
useEffect,
15-
useCallback,
1614
} from 'react';
1715
import classnames from 'classnames';
1816
import { registerStyle } from './util';
1917
import { DropdownButton, DropdownButtonProps } from './DropdownButton';
2018
import { useControlledValue, useEventCallback } from './hooks';
2119
import { Bivariant } from './typeUtils';
22-
import { Button } from './Button';
23-
import { Popover } from './Popover';
20+
import { TooltipContent } from './TooltipContent';
2421

2522
/**
2623
*
@@ -99,38 +96,6 @@ const TabMenu: FC<TabMenuProps> = (props) => {
9996
);
10097
};
10198

102-
/**
103-
*
104-
*/
105-
const TooltipContent = (props: { children: ReactNode; icon?: string }) => {
106-
const { children, icon = 'info' } = props;
107-
const [isHideTooltip, setIsHideTooltip] = useState(true);
108-
const popoverRef = useRef<HTMLDivElement>(null);
109-
const tooltipToggle = useCallback(() => {
110-
setIsHideTooltip((hidden) => !hidden);
111-
}, []);
112-
const onBlur = useCallback((e: FocusEvent<HTMLElement>) => {
113-
if (!popoverRef.current?.contains(e.relatedTarget)) {
114-
setIsHideTooltip(true);
115-
}
116-
}, []);
117-
return (
118-
<span className='slds-dropdown-trigger react-slds-tooltip-content'>
119-
<Button type='icon' icon={icon} onClick={tooltipToggle} onBlur={onBlur} />
120-
<Popover
121-
ref={popoverRef}
122-
hidden={isHideTooltip}
123-
tabIndex={-1}
124-
onBlur={onBlur}
125-
offsetX={-15}
126-
tooltip
127-
>
128-
{children}
129-
</Popover>
130-
</span>
131-
);
132-
};
133-
13499
/**
135100
*
136101
*/
@@ -325,7 +290,7 @@ function useInitComponentStyle() {
325290
],
326291
['.react-slds-tab-menu', '{ position: absolute; top: 0; right: 0; }'],
327292
[
328-
'.react-slds-tooltip-content',
293+
'.slds-tabs__item.react-slds-tab-with-menu .react-slds-tab-item-content .react-slds-tooltip-content',
329294
'{ position: absolute; top: 0.6rem; right: 2.25rem; }',
330295
],
331296
[

src/scripts/TooltipContent.tsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React, {
2+
ReactNode,
3+
useRef,
4+
useState,
5+
FocusEvent,
6+
useCallback,
7+
} from 'react';
8+
import { Button } from './Button';
9+
import { Popover } from './Popover';
10+
11+
/**
12+
*
13+
*/
14+
export const TooltipContent = (props: {
15+
children: ReactNode;
16+
icon?: string;
17+
}) => {
18+
const { children, icon = 'info' } = props;
19+
const [isHideTooltip, setIsHideTooltip] = useState(true);
20+
const popoverRef = useRef<HTMLDivElement>(null);
21+
const tooltipToggle = useCallback(() => {
22+
setIsHideTooltip((hidden) => !hidden);
23+
}, []);
24+
const onBlur = useCallback((e: FocusEvent<HTMLElement>) => {
25+
if (!popoverRef.current?.contains(e.relatedTarget)) {
26+
setIsHideTooltip(true);
27+
}
28+
}, []);
29+
return (
30+
<span className='slds-dropdown-trigger react-slds-tooltip-content'>
31+
<Button type='icon' icon={icon} onClick={tooltipToggle} onBlur={onBlur} />
32+
<Popover
33+
ref={popoverRef}
34+
hidden={isHideTooltip}
35+
tabIndex={-1}
36+
onBlur={onBlur}
37+
offsetX={-15}
38+
tooltip
39+
>
40+
{children}
41+
</Popover>
42+
</span>
43+
);
44+
};

0 commit comments

Comments
 (0)