From d662c2f06a7c2c8a4fa9b5910dd0bdb677a42aa8 Mon Sep 17 00:00:00 2001 From: Chris Rohlfs Date: Fri, 25 Aug 2017 13:24:18 +1200 Subject: [PATCH 1/2] [react-aria-menubutton] add typings --- types/react-aria-menubutton/index.d.ts | 151 ++++++++++++++++++ .../react-aria-menubutton-tests.tsx | 126 +++++++++++++++ types/react-aria-menubutton/tsconfig.json | 19 +++ types/react-aria-menubutton/tslint.json | 1 + 4 files changed, 297 insertions(+) create mode 100644 types/react-aria-menubutton/index.d.ts create mode 100644 types/react-aria-menubutton/react-aria-menubutton-tests.tsx create mode 100644 types/react-aria-menubutton/tsconfig.json create mode 100644 types/react-aria-menubutton/tslint.json diff --git a/types/react-aria-menubutton/index.d.ts b/types/react-aria-menubutton/index.d.ts new file mode 100644 index 0000000000..7a6ed925b2 --- /dev/null +++ b/types/react-aria-menubutton/index.d.ts @@ -0,0 +1,151 @@ +// Type definitions for react-aria-menubutton 5.0 +// Project: https://github.com/davidtheclark/react-aria-menubutton +// Definitions by: Muhammad Fawwaz Orabi +// Chris Rohlfs +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +import * as React from "react"; + +export interface WrapperState { + isOpen: boolean; +} + +export type WrapperProps = React.HTMLAttributes & { + /** + * A callback to run when the user makes a selection + * (i.e. clicks or presses Enter or Space on a `MenuItem`). + * It will be passed the value of the selected `MenuItem` and + * the React `SyntheticEvent`. + */ + onSelection(value: any, event: React.SyntheticEvent): any; + + /** + * A callback to run when the menu is opened or closed. + */ + onMenuToggle?(obj: WrapperState): any; + + /** + * By default, it does automatically close. + * If false, the menu will not automatically close when a + * selection is made. Default: `true`. + */ + closeOnSelection?: boolean; + + isOpen?: boolean; + + tag?: T["tagName"]; +}; + +/** + * A simple component to group a `Button`/`Menu`/`MenuItem` set, + * coordinating their interactions. It should wrap your entire menu button + * widget. + * All `Button`, `Menu`, and `MenuItem` components must be nested within a + * `Wrapper` component. + * Each wrapper can contain only one `Button`, only one `Menu`, and + * multiple `MenuItem`s. + */ +export class Wrapper extends React.Component> {} + +export type ButtonProps = React.HTMLAttributes & { + /** + * If true, the element is disabled + * (aria-disabled='true', not in tab order, clicking has no effect). + */ + disabled?: boolean; + + /** + * The HTML tag for this element. Default: 'span'. + */ + tag?: T["tagName"]; +}; + +/** + * A React component to wrap the content of your + * menu-button-pattern's button. + * The `Button` component itself acts as a UI button (with tab-index, role, etc.), + * so you probably do not want to pass an HTML ` + +
    + {menuItems} +
+
+ + ); + } +} + +ReactDOM.render(, document.body); + +const words = [ + "pectinate", + "borborygmus", + "anisodactylous", + "barbar", + "pilcrow", + "destroy" +]; + +interface DemoOneState { + selected: string; + noMenu: boolean; +} + +class DemoOne extends React.Component<{}, DemoOneState> { + constructor(props: any) { + super(props); + this.state = { selected: "", noMenu: false }; + } + + handleSelection(value: string) { + if (value === "destroy") { + this.setState({ noMenu: true }); + } else { + this.setState({ selected: value }); + } + } + + render() { + const { selected, noMenu } = this.state; + + if (noMenu) { + return ( +
+ [You decided to "destroy this menu," so the menu has been destroyed, + according to your wishes. Refresh the page to see it again.] +
+ ); + } + + const menuItemElements = words.map((word, i) => { + let itemClass = "AriaMenuButton-menuItem"; + if (selected === word) { + itemClass += " is-selected"; + } + const display = word === "destroy" ? "destroy this menu" : word; + return ( +
  • + + {display} + +
  • + ); + }); + + return ( +
    + + + +
      + {menuItemElements} +
    +
    +
    + + Your last selection was: {selected} + +
    + ); + } +} + +ReactDOM.render(, document.getElementById("demo-one")); + +closeMenu(""); +closeMenu("", { focusMenu: true }); + +openMenu(""); +openMenu("", { focusMenu: true }); diff --git a/types/react-aria-menubutton/tsconfig.json b/types/react-aria-menubutton/tsconfig.json new file mode 100644 index 0000000000..0e2508eac6 --- /dev/null +++ b/types/react-aria-menubutton/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6", "dom"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "jsx": "react", + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "react-aria-menubutton-tests.tsx" + ] +} diff --git a/types/react-aria-menubutton/tslint.json b/types/react-aria-menubutton/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/react-aria-menubutton/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 9b0bb63c3ec1baff0013acb560a15e5c6b84a6ae Mon Sep 17 00:00:00 2001 From: Chris Rohlfs Date: Sun, 27 Aug 2017 17:38:56 +1200 Subject: [PATCH 2/2] Change types to interfaces --- types/react-aria-menubutton/index.d.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/types/react-aria-menubutton/index.d.ts b/types/react-aria-menubutton/index.d.ts index 7a6ed925b2..cba4c81540 100644 --- a/types/react-aria-menubutton/index.d.ts +++ b/types/react-aria-menubutton/index.d.ts @@ -11,7 +11,8 @@ export interface WrapperState { isOpen: boolean; } -export type WrapperProps = React.HTMLAttributes & { +export interface WrapperProps + extends React.HTMLAttributes { /** * A callback to run when the user makes a selection * (i.e. clicks or presses Enter or Space on a `MenuItem`). @@ -35,7 +36,7 @@ export type WrapperProps = React.HTMLAttributes & { isOpen?: boolean; tag?: T["tagName"]; -}; +} /** * A simple component to group a `Button`/`Menu`/`MenuItem` set, @@ -48,7 +49,8 @@ export type WrapperProps = React.HTMLAttributes & { */ export class Wrapper extends React.Component> {} -export type ButtonProps = React.HTMLAttributes & { +export interface ButtonProps + extends React.HTMLAttributes { /** * If true, the element is disabled * (aria-disabled='true', not in tab order, clicking has no effect). @@ -59,7 +61,7 @@ export type ButtonProps = React.HTMLAttributes & { * The HTML tag for this element. Default: 'span'. */ tag?: T["tagName"]; -}; +} /** * A React component to wrap the content of your @@ -71,19 +73,21 @@ export type ButtonProps = React.HTMLAttributes & { */ export class Button extends React.Component> {} -export type MenuProps = React.HTMLAttributes & { +export interface MenuProps + extends React.HTMLAttributes { /** * The HTML tag for this element. Default: 'span'. */ tag?: T["tagName"]; -}; +} /** * A React component to wrap the content of your menu-button-pattern's menu. */ export class Menu extends React.Component> {} -export type MenuItemProps = React.HTMLAttributes & { +export interface MenuItemProps + extends React.HTMLAttributes { /** * If value has a value, it will be passed to the onSelection handler * when the `MenuItem` is selected @@ -100,7 +104,7 @@ export type MenuItemProps = React.HTMLAttributes & { * The HTML tag for this element. Default: 'span'. */ tag?: T["tagName"]; -}; +} /** * A React component to wrap the content of one of your