Skip to content

Commit bcb9d0a

Browse files
Fix SearchWithin menu measurements (#2170)
* Fix SearchWithin menu measurements * fix lint * Depend on min-width which is in another PR * fix lint * fix last of lint * use a ref on the group element * simplify logic * Fixing ref usage * fix case where resize doesn't trigger * use @daniel's simpler syntax Co-authored-by: Daniel Lu <[email protected]>
1 parent cadea8d commit bcb9d0a

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

packages/@react-spectrum/searchwithin/src/SearchWithin.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,21 @@ function SearchWithin(props: SpectrumSearchWithinProps, ref: FocusableRef<HTMLEl
3434
'aria-labelledby': ariaLabelledby
3535
} = props;
3636

37-
let inputRef = useRef<HTMLInputElement | HTMLTextAreaElement>();
38-
let domRef = useFocusableRef(ref, inputRef);
37+
let domRef = useFocusableRef(ref);
38+
let groupRef = useRef<HTMLDivElement>();
3939

4040
// Measure the width of the field to inform the width of the menu.
4141
let [menuWidth, setMenuWidth] = useState(null);
4242
let {scale} = useProvider();
4343

4444
let onResize = useCallback(() => {
45-
if (domRef.current) {
46-
let width = domRef.current.offsetWidth;
45+
let shouldUseGroup = !!label;
46+
let width = shouldUseGroup ? groupRef.current?.offsetWidth : domRef.current?.offsetWidth;
47+
48+
if (!isNaN(width)) {
4749
setMenuWidth(width);
4850
}
49-
}, [domRef]);
51+
}, [groupRef, domRef, setMenuWidth, label]);
5052

5153
useResizeObserver({
5254
ref: domRef,
@@ -75,7 +77,8 @@ function SearchWithin(props: SpectrumSearchWithinProps, ref: FocusableRef<HTMLEl
7577
<div
7678
role="group"
7779
aria-labelledby={labelProps.id || ariaLabel}
78-
className={classNames(styles, 'spectrum-SearchWithin', styleProps.className)}>
80+
className={classNames(styles, 'spectrum-SearchWithin', styleProps.className)}
81+
ref={groupRef}>
7982
<SlotProvider slots={slots}>
8083
{children}
8184
</SlotProvider>

packages/@react-spectrum/searchwithin/stories/SearchWithin.stories.tsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212
import {action} from '@storybook/addon-actions';
13+
import {ActionButton} from '@react-spectrum/button';
14+
import {Flex} from '@react-spectrum/layout';
1315
import {Item, Picker} from '@react-spectrum/picker';
14-
import React from 'react';
16+
import React, {useState} from 'react';
1517
import {SearchField} from '@react-spectrum/searchfield';
1618
import {SearchFieldProps} from '@react-types/searchfield';
1719
import {SearchWithin} from '../';
@@ -45,11 +47,32 @@ function renderReverse(props: Omit<SpectrumSearchWithinProps, 'children'> = {},
4547
<Item key="audiences">Audiences</Item>
4648
<Item key="tags">Tags</Item>
4749
</Picker>
48-
<SearchField placeholder="Search" {...searchFieldProps} {...searchFieldProps} onChange={action('change')} onSubmit={action('submit')} />
50+
<SearchField placeholder="Search" {...searchFieldProps} onChange={action('change')} onSubmit={action('submit')} />
4951
</SearchWithin>
5052
);
5153
}
5254

55+
function ResizeSearchWithinApp(props) {
56+
const [state, setState] = useState(true);
57+
58+
return (
59+
<Flex direction="column" gap="size-200" alignItems="start">
60+
<div style={{width: state ? '300px' : '400px'}}>
61+
<SearchWithin label="Search" {...props} width="100%">
62+
<SearchField placeholder="Search" onChange={action('change')} onSubmit={action('submit')} />
63+
<Picker defaultSelectedKey="all" onSelectionChange={action('selectionChange')}>
64+
<Item key="all">All</Item>
65+
<Item key="campaigns">Campaigns</Item>
66+
<Item key="audiences">Audiences</Item>
67+
<Item key="tags">Tags</Item>
68+
</Picker>
69+
</SearchWithin>
70+
</div>
71+
<ActionButton onPress={() => setState(!state)}>Toggle size</ActionButton>
72+
</Flex>
73+
);
74+
}
75+
5376
export const Default = () => render({});
5477

5578
export const ValueControlled = () => render({}, {value: 'Controlled'});
@@ -96,3 +119,7 @@ export const AutoFocusPicker = () => render({}, {}, {autoFocus: true});
96119
AutoFocusPicker.storyName = 'autoFocus: true on Picker';
97120

98121
export const ReverseChildrenOrder = () => renderReverse({});
122+
123+
export const ResizeSearchWithin = () => <ResizeSearchWithinApp />;
124+
125+
export const ResizeSearchWithinNoLabel = () => <ResizeSearchWithinApp label={null} />;

0 commit comments

Comments
 (0)