Skip to content

Commit

Permalink
feat: add highlighted prop
Browse files Browse the repository at this point in the history
  • Loading branch information
wmui51 committed Sep 14, 2023
1 parent 4e6bd33 commit fb216ef
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/menu/src/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const Menu = (props) => {
children,
defaultSelected,
divider,
highlighted,
menuRef,
multiple,
onChange = () => {},
Expand All @@ -27,6 +28,7 @@ const Menu = (props) => {
{...otherProps}
defaultSelected={defaultSelected}
hasFocus={hasFocus}
highlighted={highlighted}
menuRef={menuRef}
multiple={multiple}
onBlur={handleBlur}
Expand Down Expand Up @@ -102,6 +104,11 @@ Menu.propTypes = {
* Shows a divider at the bottom of the menu
*/
divider: PropTypes.bool,
/**
* Semi-controlled way of having Options highlighted
* Takes the index of the option you want highlighted
*/
highlighted: PropTypes.number,
/**
* A callback ref that gets passed to the HTML
* element with `role="listbox"`
Expand Down
9 changes: 9 additions & 0 deletions packages/menu/src/MenuGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const MenuGroup = (props) => {
const {
children,
defaultSelected,
highlighted,
menuRef,
multiple,
onChange,
Expand All @@ -21,6 +22,7 @@ const MenuGroup = (props) => {
<MenuBehavior
{...otherProps}
defaultSelected={defaultSelected}
highlighted={highlighted}
menuRef={menuRef}
multiple={multiple}
onChange={onChange}
Expand Down Expand Up @@ -84,6 +86,13 @@ MenuGroup.propTypes = {
*/
// eslint-disable-next-line react/forbid-prop-types
defaultSelected: PropTypes.arrayOf(PropTypes.any),
/**
* Semi-controlled way of having Options highlighted
* Takes the index of the option you want highlighted
* This will take precedent over the Menu prop of the
* same name
*/
highlighted: PropTypes.number,
/**
* A callback ref that gets passed to the HTML
* element with `role="listbox"`
Expand Down
7 changes: 7 additions & 0 deletions packages/menu/src/behaviors/MenuBehavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ const MenuBehavior = (props) => {
}
}, [props.selected]);

useEffect(() => {
if (props.highlighted >= 0) {
setHighlightIndexHook(props.highlighted + 1);
}
}, [props.highlighted]);

return props.children({
getActiveOption: getActiveOptionMethod,
getHighlightIndex: getHighlightIndexMethod,
Expand Down Expand Up @@ -306,6 +312,7 @@ MenuBehavior.propTypes = {
handleFocus: PropTypes.func,
handleKeyDown: PropTypes.func,
handleMouseMove: PropTypes.func,
highlighted: PropTypes.func,
menuRef: PropTypes.func,
multiple: PropTypes.bool,
onBlur: PropTypes.func,
Expand Down
3 changes: 3 additions & 0 deletions packages/menu/src/presenters/MenuGroupPresenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const MenuGroupPresenter = (props) => {
getHighlightIndex,
getOptionsInfo,
getPreviousEvent,
highlighted,
multiple,
onBlur: handleBlur,
onFocus: handleFocus,
Expand All @@ -44,6 +45,7 @@ const MenuGroupPresenter = (props) => {
handleFocus,
handleKeyDown,
handleMouseMove,
highlighted,
multiple,
selected,
setActiveOption,
Expand Down Expand Up @@ -180,6 +182,7 @@ MenuGroupPresenter.propTypes = {
getHighlightIndex: PropTypes.func,
getOptionsInfo: PropTypes.func,
getPreviousEvent: PropTypes.func,
highlighted: PropTypes.number,
menuRef: PropTypes.func,
multiple: PropTypes.bool,
onBlur: PropTypes.func,
Expand Down
1 change: 1 addition & 0 deletions packages/menu/src/presenters/MenuPresenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const MenuPresenter = (props) => {
delete payload.handleFocus;
delete payload.handleKeyDown;
delete payload.handleMouseMove;
delete payload.highlighted;
delete payload.setActiveOption;
delete payload.setHighlightIndex;
delete payload.setOptionsInfo;
Expand Down

0 comments on commit fb216ef

Please sign in to comment.