Skip to content

Commit fb216ef

Browse files
committed
feat: add highlighted prop
1 parent 4e6bd33 commit fb216ef

File tree

5 files changed

+27
-0
lines changed

5 files changed

+27
-0
lines changed

packages/menu/src/Menu.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const Menu = (props) => {
1111
children,
1212
defaultSelected,
1313
divider,
14+
highlighted,
1415
menuRef,
1516
multiple,
1617
onChange = () => {},
@@ -27,6 +28,7 @@ const Menu = (props) => {
2728
{...otherProps}
2829
defaultSelected={defaultSelected}
2930
hasFocus={hasFocus}
31+
highlighted={highlighted}
3032
menuRef={menuRef}
3133
multiple={multiple}
3234
onBlur={handleBlur}
@@ -102,6 +104,11 @@ Menu.propTypes = {
102104
* Shows a divider at the bottom of the menu
103105
*/
104106
divider: PropTypes.bool,
107+
/**
108+
* Semi-controlled way of having Options highlighted
109+
* Takes the index of the option you want highlighted
110+
*/
111+
highlighted: PropTypes.number,
105112
/**
106113
* A callback ref that gets passed to the HTML
107114
* element with `role="listbox"`

packages/menu/src/MenuGroup.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const MenuGroup = (props) => {
88
const {
99
children,
1010
defaultSelected,
11+
highlighted,
1112
menuRef,
1213
multiple,
1314
onChange,
@@ -21,6 +22,7 @@ const MenuGroup = (props) => {
2122
<MenuBehavior
2223
{...otherProps}
2324
defaultSelected={defaultSelected}
25+
highlighted={highlighted}
2426
menuRef={menuRef}
2527
multiple={multiple}
2628
onChange={onChange}
@@ -84,6 +86,13 @@ MenuGroup.propTypes = {
8486
*/
8587
// eslint-disable-next-line react/forbid-prop-types
8688
defaultSelected: PropTypes.arrayOf(PropTypes.any),
89+
/**
90+
* Semi-controlled way of having Options highlighted
91+
* Takes the index of the option you want highlighted
92+
* This will take precedent over the Menu prop of the
93+
* same name
94+
*/
95+
highlighted: PropTypes.number,
8796
/**
8897
* A callback ref that gets passed to the HTML
8998
* element with `role="listbox"`

packages/menu/src/behaviors/MenuBehavior.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@ const MenuBehavior = (props) => {
275275
}
276276
}, [props.selected]);
277277

278+
useEffect(() => {
279+
if (props.highlighted >= 0) {
280+
setHighlightIndexHook(props.highlighted + 1);
281+
}
282+
}, [props.highlighted]);
283+
278284
return props.children({
279285
getActiveOption: getActiveOptionMethod,
280286
getHighlightIndex: getHighlightIndexMethod,
@@ -306,6 +312,7 @@ MenuBehavior.propTypes = {
306312
handleFocus: PropTypes.func,
307313
handleKeyDown: PropTypes.func,
308314
handleMouseMove: PropTypes.func,
315+
highlighted: PropTypes.func,
309316
menuRef: PropTypes.func,
310317
multiple: PropTypes.bool,
311318
onBlur: PropTypes.func,

packages/menu/src/presenters/MenuGroupPresenter.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const MenuGroupPresenter = (props) => {
1818
getHighlightIndex,
1919
getOptionsInfo,
2020
getPreviousEvent,
21+
highlighted,
2122
multiple,
2223
onBlur: handleBlur,
2324
onFocus: handleFocus,
@@ -44,6 +45,7 @@ const MenuGroupPresenter = (props) => {
4445
handleFocus,
4546
handleKeyDown,
4647
handleMouseMove,
48+
highlighted,
4749
multiple,
4850
selected,
4951
setActiveOption,
@@ -180,6 +182,7 @@ MenuGroupPresenter.propTypes = {
180182
getHighlightIndex: PropTypes.func,
181183
getOptionsInfo: PropTypes.func,
182184
getPreviousEvent: PropTypes.func,
185+
highlighted: PropTypes.number,
183186
menuRef: PropTypes.func,
184187
multiple: PropTypes.bool,
185188
onBlur: PropTypes.func,

packages/menu/src/presenters/MenuPresenter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const MenuPresenter = (props) => {
7171
delete payload.handleFocus;
7272
delete payload.handleKeyDown;
7373
delete payload.handleMouseMove;
74+
delete payload.highlighted;
7475
delete payload.setActiveOption;
7576
delete payload.setHighlightIndex;
7677
delete payload.setOptionsInfo;

0 commit comments

Comments
 (0)