From 4d191b14ad38e5cf871d8554d24b695c1d83096e Mon Sep 17 00:00:00 2001 From: Joshua Archer Date: Thu, 20 Apr 2023 15:56:34 -0700 Subject: [PATCH] fix(/lib/components/dropdown): set width equal to trigger #(575) Description Update the dropdown width to fit the button width Fixes #575 Type of change Please delete options that are not relevant. Bug fix (non-breaking change which fixes an issue) New feature (non-breaking change which adds functionality) Breaking change (fix or feature that would cause existing functionality to not work as expected) This change contains documentation update Breaking changes Please document the breaking changes if suitable. How Has This Been Tested? Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration Test A Test B Test Configuration: Firmware version: Hardware: Toolchain: SDK: Checklist: My code follows the style guidelines of this project I have performed a self-review of my own code I have commented my code, particularly in hard-to-understand areas I have made corresponding changes to the documentation My changes generate no new warnings I have added tests that prove my fix is effective or that my feature works New and existing unit tests pass locally with my changes Any dependent changes have been merged and published in downstream modules I have checked my code and corrected any misspellings --- src/lib/components/Dropdown/Dropdown.tsx | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/lib/components/Dropdown/Dropdown.tsx b/src/lib/components/Dropdown/Dropdown.tsx index be73c8548..71776d790 100644 --- a/src/lib/components/Dropdown/Dropdown.tsx +++ b/src/lib/components/Dropdown/Dropdown.tsx @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/ban-ts-comment */ import type { ComponentProps, FC, PropsWithChildren, ReactElement, ReactNode } from 'react'; -import React, { Children, useCallback, useMemo, useState } from 'react'; +import React, { Children, useCallback, useMemo, useRef, useState } from 'react'; import { HiOutlineChevronDown, HiOutlineChevronLeft, HiOutlineChevronRight, HiOutlineChevronUp } from 'react-icons/hi'; import type { DeepPartial } from '..'; import { mergeDeep } from '../../helpers/mergeDeep'; @@ -75,6 +75,8 @@ const DropdownComponent: FC = ({ }, [placement]); const [closeRequestKey, setCloseRequestKey] = useState(undefined); + const triggerRef = useRef(null); + const buttonWidth = triggerRef.current?.offsetWidth; // Extends DropdownItem's onClick to trigger a close request to the Floating component const attachCloseListener = useCallback( @@ -101,12 +103,24 @@ const DropdownComponent: FC = ({ ); const content = useMemo( - () =>
    {Children.map(children, attachCloseListener)}
, - [attachCloseListener, children, theme.content], + () => ( +
    + {Children.map(children, attachCloseListener)} +
+ ), + [children, theme, triggerRef.current, attachCloseListener, buttonWidth], ); const TriggerWrapper: FC = ({ children }) => - inline ? : ; + inline ? ( + + ) : ( + + ); return (