Skip to content

Commit

Permalink
feat: basic menu item check state toggling (microsoft#3512)
Browse files Browse the repository at this point in the history
* basic check state toggling

* use enum for compare
  • Loading branch information
Stephane Comeau authored Jul 17, 2020
1 parent a0b8658 commit ea84cbd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ <h4>With standard elements</h4>
<div role="menuitem">Menu item 3</div>
</fast-menu>

<h4>With radio buttons</h4>
<h4>With radio buttons and checkboxes</h4>
<fast-menu>
<fast-menu-item>Menu item 1</fast-menu-item>
<fast-menu-item>Menu item 2</fast-menu-item>
<fast-menu-item>Menu item 3</fast-menu-item>
<fast-divider></fast-divider>
<fast-menu-item role="menuitemradio">Menu item 4</fast-menu-item>
<fast-menu-item role="menuitemradio">Menu item 5</fast-menu-item>
<fast-divider></fast-divider>
<fast-menu-item role="menuitemcheckbox">Menu item 4</fast-menu-item>
<fast-menu-item role="menuitemcheckbox">Menu item 5</fast-menu-item>
</fast-menu>
</fast-design-system-provider>
23 changes: 20 additions & 3 deletions packages/web-components/fast-foundation/src/menu-item/menu-item.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { attr, booleanConverter, FASTElement } from "@microsoft/fast-element";
import { keyCodeEnter, keyCodeSpace } from "@microsoft/fast-web-utilities";
import { StartEnd } from "../patterns/start-end";
import { applyMixins } from "../utilities/apply-mixins";

Expand Down Expand Up @@ -63,7 +64,12 @@ export class MenuItem extends FASTElement {
* @internal
*/
public handleMenuItemKeyDown = (e: KeyboardEvent): boolean => {
this.change();
switch (e.keyCode) {
case keyCodeEnter:
case keyCodeSpace:
this.invoke();
return false;
}

return true;
};
Expand All @@ -72,10 +78,21 @@ export class MenuItem extends FASTElement {
* @internal
*/
public handleMenuItemClick = (e: MouseEvent): void => {
this.change();
this.invoke();
};

private change = (): void => {
private invoke = (): void => {
if (this.disabled) {
return;
}

switch (this.role) {
case MenuItemRole.menuitemcheckbox:
case MenuItemRole.menuitemradio:
this.checked = !this.checked;
break;
}

this.$emit("change");
};
}
Expand Down

0 comments on commit ea84cbd

Please sign in to comment.