Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Menu] Add prop to disable auto focus #11984

Merged
merged 3 commits into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = [
name: 'The size of all the modules of material-ui.',
webpack: true,
path: 'packages/material-ui/build/index.js',
limit: '94.7 KB',
limit: '94.8 KB',
},
{
name: 'The main bundle of the docs',
Expand Down
1 change: 1 addition & 0 deletions packages/material-ui/src/Menu/Menu.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ClassNameMap } from '../styles/withStyles';
export interface MenuProps
extends StandardProps<PopoverProps & Partial<TransitionHandlerProps>, MenuClassKey> {
anchorEl?: HTMLElement;
disableAutoFocusItem?: boolean;
MenuListProps?: Partial<MenuListProps>;
PaperProps?: Partial<PaperProps>;
PopoverClasses?: Partial<ClassNameMap<PopoverClassKey>>;
Expand Down
14 changes: 11 additions & 3 deletions packages/material-ui/src/Menu/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Menu extends React.Component {
menuList = null;

componentDidMount() {
if (this.props.open) {
if (this.props.open && this.props.disableAutoFocusItem !== true) {
this.focus();
}
}
Expand All @@ -59,11 +59,13 @@ class Menu extends React.Component {
};

handleEnter = element => {
const { theme } = this.props;
const { disableAutoFocusItem, theme } = this.props;
const menuList = ReactDOM.findDOMNode(this.menuList);

// Focus so the scroll computation of the Popover works as expected.
this.focus();
if (disableAutoFocusItem !== true) {
this.focus();
}

// Let's ignore that piece of logic if users are already overriding the width
// of the menu.
Expand Down Expand Up @@ -92,6 +94,7 @@ class Menu extends React.Component {
const {
children,
classes,
disableAutoFocusItem,
MenuListProps,
onEnter,
PaperProps = {},
Expand Down Expand Up @@ -146,6 +149,10 @@ Menu.propTypes = {
* See [CSS API](#css-api) below for more details.
*/
classes: PropTypes.object.isRequired,
/**
* If `true`, the selected / first menu item will not be auto focused.
*/
disableAutoFocusItem: PropTypes.bool,
/**
* Properties applied to the `MenuList` element.
*/
Expand Down Expand Up @@ -207,6 +214,7 @@ Menu.propTypes = {
};

Menu.defaultProps = {
disableAutoFocusItem: false,
transitionDuration: 'auto',
};

Expand Down
1 change: 1 addition & 0 deletions pages/api/menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ title: Menu API
| <span class="prop-name">anchorEl</span> | <span class="prop-type">object |   | The DOM element used to set the position of the menu. |
| <span class="prop-name">children</span> | <span class="prop-type">node |   | Menu contents, normally `MenuItem`s. |
| <span class="prop-name">classes</span> | <span class="prop-type">object |   | Override or extend the styles applied to the component. See [CSS API](#css-api) below for more details. |
| <span class="prop-name">disableAutoFocusItem</span> | <span class="prop-type">bool | <span class="prop-default">false</span> | If `true`, the selected / first menu item will not be auto focused. |
| <span class="prop-name">MenuListProps</span> | <span class="prop-type">object |   | Properties applied to the `MenuList` element. |
| <span class="prop-name">onClose</span> | <span class="prop-type">func |   | Callback fired when the component requests to be closed.<br><br>**Signature:**<br>`function(event: object) => void`<br>*event:* The event source of the callback |
| <span class="prop-name">onEnter</span> | <span class="prop-type">func |   | Callback fired before the Menu enters. |
Expand Down