Skip to content

Commit 347b268

Browse files
committed
feat: dynamic tippy on truncate poc
1 parent d312cce commit 347b268

File tree

4 files changed

+14
-32
lines changed

4 files changed

+14
-32
lines changed

src/Common/RJSF/constants.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,3 @@ export const PLACEHOLDERS = {
2121
}
2222

2323
export const DEFAULT_FIELD_TITLE = 'Key not available'
24-
25-
export const ADD_BUTTON_WIDTH = {
26-
MAX_WIDTH_VALUE: 250,
27-
MAX_WIDTH_CLASSNAME: 'dc__mxw-250',
28-
}

src/Common/RJSF/templates/ButtonTemplates/AddButton.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import { IconButtonProps } from '@rjsf/utils'
1818

1919
import Tippy from '../../../Tippy'
20-
import { ADD_BUTTON_WIDTH } from '../../constants'
2120
import { ReactComponent as PlusIcon } from '../../../../Assets/Icon/ic-add.svg'
2221

2322
export const AddButton = ({
@@ -37,12 +36,11 @@ export const AddButton = ({
3736
arrow={false}
3837
placement="right"
3938
content={content}
40-
truncateWidth={ADD_BUTTON_WIDTH.MAX_WIDTH_VALUE}
4139
>
4240
<button
4341
{...props}
4442
type="button"
45-
className={`dc__outline-none-imp p-0 dc__transparent flex dc__gap-4 cursor ${ADD_BUTTON_WIDTH.MAX_WIDTH_CLASSNAME}`}
43+
className="dc__outline-none-imp p-0 dc__transparent flex dc__gap-4 cursor dc__mxw-250"
4644
title="Add"
4745
>
4846
<PlusIcon className="icon-dim-16 fcb-5" />

src/Common/RJSF/utils.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,8 @@ export const getInferredTypeFromValueType = (value) => {
138138
}
139139

140140
export const getTippyWrapperWithContent =
141-
(content, placement?: React.ComponentProps<typeof Tippy>['placement'], truncateWidth = 0) =>
142-
(children) => (
143-
<Tippy
144-
className="default-tt"
145-
maxWidth={300}
146-
arrow={false}
147-
placement={placement || 'right'}
148-
content={content}
149-
truncateWidth={truncateWidth}
150-
>
141+
(content, placement?: React.ComponentProps<typeof Tippy>['placement']) => (children) => (
142+
<Tippy className="default-tt" maxWidth={300} arrow={false} placement={placement || 'right'} content={content}>
151143
{children}
152144
</Tippy>
153145
)

src/Common/Tippy.tsx

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
import { useEffect, useRef, useState } from 'react'
1+
import { useCallback, useState, Children, cloneElement } from 'react'
22
import TippyJS, { TippyProps as TippyJSProps } from '@tippyjs/react'
33

4-
// TODO: we should generalize this Tippy to work without sending in this truncateWidth
5-
const Tippy = ({ truncateWidth = 0, children, ...rest }: TippyJSProps & Record<'truncateWidth', number>) => {
6-
// NOTE: if showOnTruncate is off then always showTippy
7-
const [showTippy, setShowTippy] = useState(!!truncateWidth)
8-
const ref = useRef(null)
4+
const Tippy = ({ children, ...rest }: TippyJSProps) => {
5+
const [showTippy, setShowTippy] = useState(false)
96

10-
useEffect(() => {
11-
if (truncateWidth) {
12-
setShowTippy(ref.current?.offsetWidth >= truncateWidth)
7+
const refCallback = useCallback((node: HTMLDivElement) => {
8+
if (node) {
9+
setShowTippy(node.scrollWidth > node.clientWidth)
1310
}
14-
}, [ref.current?.offsetWidth])
11+
}, [])
12+
13+
const child = Children.only(children)
1514

1615
return !showTippy ? (
17-
children
16+
cloneElement(child, { ...child.props, ref: refCallback })
1817
) : (
19-
<TippyJS {...rest}>
20-
<span ref={ref}>{children}</span>
21-
</TippyJS>
18+
<TippyJS {...rest}>{cloneElement(child, { ...child.props, ref: refCallback })}</TippyJS>
2219
)
2320
}
2421

0 commit comments

Comments
 (0)