From b7d91cc8a100f99ff1047364a87579d176a4878e Mon Sep 17 00:00:00 2001 From: Alexandre Brillant Date: Thu, 21 Mar 2024 20:10:49 -0400 Subject: [PATCH] feat(Tooltip): fix dynamic width --- .../components/tooltip/tooltip.test.tsx.snap | 4 ++ .../react/src/components/tooltip/tooltip.tsx | 6 +++ .../storybook/stories/tooltip.stories.tsx | 53 ++++++++++++++++++- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/packages/react/src/components/tooltip/tooltip.test.tsx.snap b/packages/react/src/components/tooltip/tooltip.test.tsx.snap index 0c1cc7509f..8cea951009 100644 --- a/packages/react/src/components/tooltip/tooltip.test.tsx.snap +++ b/packages/react/src/components/tooltip/tooltip.test.tsx.snap @@ -220,6 +220,7 @@ exports[`Tooltip Has default desktop styles (defaultOpen) 1`] = ` data-testid="tooltip-content-container" id="uuid1" isMobile={false} + key="1" role="tooltip" style={ { @@ -492,6 +493,7 @@ exports[`Tooltip Has default desktop styles 1`] = ` data-testid="tooltip-content-container" id="uuid1" isMobile={false} + key="1" role="tooltip" style={ { @@ -768,6 +770,7 @@ exports[`Tooltip Has mobile styles (defaultOpen) 1`] = ` data-testid="tooltip-content-container" id="uuid1" isMobile={true} + key="1" role="tooltip" style={ { @@ -1040,6 +1043,7 @@ exports[`Tooltip Has mobile styles 1`] = ` data-testid="tooltip-content-container" id="uuid1" isMobile={true} + key="1" role="tooltip" style={ { diff --git a/packages/react/src/components/tooltip/tooltip.tsx b/packages/react/src/components/tooltip/tooltip.tsx index 7105f2abd3..43b0b4c62d 100644 --- a/packages/react/src/components/tooltip/tooltip.tsx +++ b/packages/react/src/components/tooltip/tooltip.tsx @@ -232,9 +232,14 @@ export const Tooltip: FunctionComponent> = ({ const [tooltipTimeout, setTooltipTimeout] = useState(); const [controlledTooltipOpen, setControlledTooltipOpen] = useState(); const [isClicked, setIsClicked] = useState(false); + const [tooltipKey, setTooltipKey] = useState(0); const currentLabel = isClicked ? (confirmationLabel ?? label) : label; const tooltipVariant = (mode === 'confirm' && isClicked) ? 'success' : 'normal'; + useEffect(() => { + setTooltipKey((prevKey) => prevKey + 1); + }, [currentLabel]); + const getTooltipTriggerType = useCallback((): TriggerType | null => { if (disabled) { return null; @@ -365,6 +370,7 @@ export const Tooltip: FunctionComponent> = ({ isMobile={isMobile} variant={tooltipVariant} visible={popperTooltip.visible} + key={tooltipKey} {...popperTooltip.getTooltipProps() /* eslint-disable-line react/jsx-props-no-spreading */} > { WithDropdownNavigation.decorators = [RouterDecorator]; -const CodeContainer = styled.div` +const CodeContainer = styled.div<{ justifyContent?: string }>` display: flex; + justify-content: ${({ justifyContent }) => (justifyContent || 'flex-start')}; `; export const WithConfirmation: Story = () => { const code = 'JBSW Y3DP EHPK 3PXP'; @@ -161,3 +163,50 @@ export const WithConfirmation: Story = () => { ); }; + +export const WithDynamicText: Story = () => { + const [label, setLabel] = useState('Hide password'); + const [iconName, setIconName] = useState<'eye' | 'eyeOff'>('eye'); + const [inputType, setInputType] = useState('password'); + const [password, setPassword] = useState('somePassword'); + const handleOnClick = useCallback(() => { + if (label === 'Hide password') { + setLabel('Show password in plain text'); + setIconName('eyeOff'); + setInputType('password'); + } else { + setLabel('Hide password'); + setIconName('eye'); + setInputType('text'); + } + }, [label, setLabel]); + const handleOnPasswordChange = useCallback((newPassword) => { + setPassword(newPassword); + }, [setPassword]); + + return ( + + + + + + + + + ); +};