From 0be6da91ef5fa0c9d2809f6c6d7c69dad44bf32e Mon Sep 17 00:00:00 2001 From: lerte smith Date: Thu, 7 Nov 2024 01:53:36 +0800 Subject: [PATCH] Select Option width fit parent --- .../src/components/ListBox/listbox.module.css | 4 +++ .../actify/src/components/Popover/Popover.tsx | 14 +++++++++- .../actify/src/components/Select/Select.tsx | 28 +++++++++++++++++-- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/packages/actify/src/components/ListBox/listbox.module.css b/packages/actify/src/components/ListBox/listbox.module.css index b0034ae..360db0c 100644 --- a/packages/actify/src/components/ListBox/listbox.module.css +++ b/packages/actify/src/components/ListBox/listbox.module.css @@ -1,4 +1,7 @@ .items { + list-style: none; + margin: 0; + padding: 0; display: block; padding-block: 8px; list-style-type: none; @@ -9,6 +12,7 @@ --md-menu-container-color, var(--md-sys-color-surface-container, #f3edf7) ); + width: var(--reference-width); height: inherit; max-height: inherit; overflow: auto; diff --git a/packages/actify/src/components/Popover/Popover.tsx b/packages/actify/src/components/Popover/Popover.tsx index a71cda8..4127405 100644 --- a/packages/actify/src/components/Popover/Popover.tsx +++ b/packages/actify/src/components/Popover/Popover.tsx @@ -11,11 +11,18 @@ import type { OverlayTriggerState } from 'react-stately' import React from 'react' interface PopoverProps extends Omit { + referenceWidth?: number children: React.ReactNode state: OverlayTriggerState } -const Popover = ({ children, state, offset = 8, ...props }: PopoverProps) => { +const Popover = ({ + children, + state, + offset = 8, + referenceWidth, + ...props +}: PopoverProps) => { const popoverRef = React.useRef(null) const { popoverProps, underlayProps, arrowProps, placement } = usePopover( { @@ -26,6 +33,11 @@ const Popover = ({ children, state, offset = 8, ...props }: PopoverProps) => { state ) + popoverProps.style = { + ...popoverProps.style, + '--reference-width': referenceWidth + 'px' + } as React.CSSProperties + return (
diff --git a/packages/actify/src/components/Select/Select.tsx b/packages/actify/src/components/Select/Select.tsx index 5fe53c5..5014998 100644 --- a/packages/actify/src/components/Select/Select.tsx +++ b/packages/actify/src/components/Select/Select.tsx @@ -21,7 +21,10 @@ interface SelectProps extends AriaSelectOptions, SelectStateOptions { const Select = (props: SelectProps) => { const state = useSelectState(props) - const ref = React.useRef(null) + const [referenceWidth, setReferenceWidth] = React.useState< + number | undefined + >(0) + const ref = React.useRef(null) const { triggerProps, valueProps, menuProps } = useSelect(props, state, ref) @@ -35,6 +38,22 @@ const Select = (props: SelectProps) => { Tag = OutlinedField } + React.useLayoutEffect(() => { + const width = ref?.current?.getBoundingClientRect().width + setReferenceWidth(width) + }, []) + + React.useEffect(() => { + const updateWidth = () => { + const width = ref?.current?.getBoundingClientRect().width + setReferenceWidth(width) + } + window.addEventListener('resize', updateWidth) + return () => { + window.removeEventListener('resize', updateWidth) + } + }, []) + return (
(props: SelectProps) => { {state.isOpen && ( - + )}