Skip to content

Commit 296ecdd

Browse files
authored
[select] Add open state for Select.Icon and fix ref type (#2714)
1 parent 3dbc0e3 commit 296ecdd

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

docs/reference/generated/select-icon.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
"detailedType": "| ReactElement\n| ((\n props: HTMLProps,\n state: Select.Icon.State,\n ) => ReactElement)"
1313
}
1414
},
15-
"dataAttributes": {},
15+
"dataAttributes": {
16+
"data-popup-open": {
17+
"description": "Present when the corresponding popup is open."
18+
}
19+
},
1620
"cssVariables": {}
1721
}

packages/react/src/select/icon/SelectIcon.tsx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
'use client';
22
import * as React from 'react';
3+
import { useStore } from '@base-ui-components/utils/store';
34
import type { BaseUIComponentProps } from '../../utils/types';
45
import { useRenderElement } from '../../utils/useRenderElement';
6+
import { useSelectRootContext } from '../root/SelectRootContext';
7+
import { triggerOpenStateMapping } from '../../utils/popupStateMapping';
8+
import { selectors } from '../store';
59

610
/**
711
* An icon that indicates that the trigger button opens a select menu.
@@ -11,19 +15,25 @@ import { useRenderElement } from '../../utils/useRenderElement';
1115
*/
1216
export const SelectIcon = React.forwardRef(function SelectIcon(
1317
componentProps: SelectIcon.Props,
14-
forwardedRef: React.ForwardedRef<HTMLDivElement>,
18+
forwardedRef: React.ForwardedRef<HTMLSpanElement>,
1519
) {
1620
const { className, render, ...elementProps } = componentProps;
1721

22+
const { store } = useSelectRootContext();
23+
const open = useStore(store, selectors.open);
24+
25+
const state: SelectIcon.State = React.useMemo(
26+
() => ({
27+
open,
28+
}),
29+
[open],
30+
);
31+
1832
const element = useRenderElement('span', componentProps, {
33+
state,
1934
ref: forwardedRef,
20-
props: [
21-
{
22-
'aria-hidden': true,
23-
children: '▼',
24-
},
25-
elementProps,
26-
],
35+
props: [{ 'aria-hidden': true, children: '▼' }, elementProps],
36+
stateAttributesMapping: triggerOpenStateMapping,
2737
});
2838

2939
return element;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { CommonTriggerDataAttributes } from '../../utils/popupStateMapping';
2+
3+
export enum SelectIconDataAttributes {
4+
/**
5+
* Present when the corresponding popup is open.
6+
*/
7+
popupOpen = CommonTriggerDataAttributes.popupOpen,
8+
}

0 commit comments

Comments
 (0)