diff --git a/common/changes/add-button-focus-example_2017-05-05-17-41.json b/common/changes/add-button-focus-example_2017-05-05-17-41.json new file mode 100644 index 0000000000000..829746e1cd212 --- /dev/null +++ b/common/changes/add-button-focus-example_2017-05-05-17-41.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "office-ui-fabric-react", + "comment": "Button: Add button example", + "type": "patch" + } + ], + "email": "hross@hross.net" +} \ No newline at end of file diff --git a/packages/office-ui-fabric-react/src/components/Button/ButtonPage.tsx b/packages/office-ui-fabric-react/src/components/Button/ButtonPage.tsx index d79fdb93803c1..c0a1e30519d36 100644 --- a/packages/office-ui-fabric-react/src/components/Button/ButtonPage.tsx +++ b/packages/office-ui-fabric-react/src/components/Button/ButtonPage.tsx @@ -14,6 +14,7 @@ import { ButtonCommandExample } from './examples/Button.Command.Example'; import { ButtonIconExample } from './examples/Button.Icon.Example'; import { ButtonAnchorExample } from './examples/Button.Anchor.Example'; import { ButtonScreenReaderExample } from './examples/Button.ScreenReader.Example'; +import { ButtonSwapExample } from './examples/Button.Swap.Example'; import { IButtonDemoPageState } from './examples/IButtonDemoPageState'; import './examples/Button.Basic.Example.scss'; import { Link } from '../../Link'; @@ -26,6 +27,7 @@ const ButtonIconExampleCode = require('!raw-loader!office-ui-fabric-react/src/co const ButtonAnchorExampleCode = require('!raw-loader!office-ui-fabric-react/src/components/Button/examples/Button.Anchor.Example.tsx') as string; const ButtonScreenReaderExampleCode = require('!raw-loader!office-ui-fabric-react/src/components/Button/examples/Button.ScreenReader.Example.tsx') as string; const ButtonContextualMenuExampleCode = require('!raw-loader!office-ui-fabric-react/src/components/Button/examples/Button.ContextualMenu.Example.tsx') as string; +const ButtonSwapExampleCode = require('!raw-loader!office-ui-fabric-react/src/components/Button/examples/Button.Swap.Example.tsx') as string; export class ButtonPage extends React.Component { constructor() { @@ -67,6 +69,9 @@ export class ButtonPage extends React.Component + + + } propertiesTables={ diff --git a/packages/office-ui-fabric-react/src/components/Button/examples/Button.Swap.Example.tsx b/packages/office-ui-fabric-react/src/components/Button/examples/Button.Swap.Example.tsx new file mode 100644 index 0000000000000..936e2c9e3f049 --- /dev/null +++ b/packages/office-ui-fabric-react/src/components/Button/examples/Button.Swap.Example.tsx @@ -0,0 +1,79 @@ +import * as React from 'react'; +import * as ReactDOM from 'react-dom'; + +import { + autobind +} from 'office-ui-fabric-react/lib/Utilities'; +import { DefaultButton, PrimaryButton, IButtonProps } from 'office-ui-fabric-react/lib/Button'; +import { Label } from 'office-ui-fabric-react/lib/Label'; + +export interface IButtonSwapExampleState { + isPrimary: boolean; +} + +export class ButtonSwapExample extends React.Component { + private buttonRef: HTMLElement; + private hasFocus: boolean; + + public constructor() { + super(); + + this.hasFocus = false; + this.buttonRef = null; + this.state = { + isPrimary: true + }; + } + + public componentWillUpdate(nextProps: IButtonProps, nextState: IButtonSwapExampleState): void { + // check to see if our button element has focus + this.hasFocus = document.activeElement === this.buttonRef; + } + + public componentDidUpdate(prevProps: IButtonProps, prevState: IButtonSwapExampleState): void { + // if our button previously had focus but we lost it + // because we switched the control type, we need to set focus again + if (this.hasFocus && document.activeElement !== this.buttonRef) { + this.buttonRef.focus(); + } + } + + public render() { + let { isPrimary } = this.state; + let { disabled } = this.props; + let text = 'Swap'; + + // determine which button to render + let button = isPrimary + ? + { text } + + : + { text } + ; + + return ( +
+ + { button } +
+ ); + } + + @autobind + private _setButtonRef(ref: React.ReactInstance): void { + this.buttonRef = ReactDOM.findDOMNode(ref) as HTMLElement; + } + + @autobind + private _onClick(): void { + // change the button type on click + this.setState({ isPrimary: !this.state.isPrimary }); + } +} \ No newline at end of file