-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1733 from hross/add-button-focus-example
Button: Add button focus swap example
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
common/changes/add-button-focus-example_2017-05-05-17-41.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "office-ui-fabric-react", | ||
"comment": "Button: Add button example", | ||
"type": "patch" | ||
} | ||
], | ||
"email": "hross@hross.net" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/office-ui-fabric-react/src/components/Button/examples/Button.Swap.Example.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IButtonProps, IButtonSwapExampleState> { | ||
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 | ||
? <PrimaryButton | ||
ref={ this._setButtonRef } | ||
disabled={ disabled } | ||
onClick={ this._onClick }> | ||
{ text } | ||
</PrimaryButton> | ||
: <DefaultButton | ||
ref={ this._setButtonRef } | ||
disabled={ disabled } | ||
onClick={ this._onClick }> | ||
{ text } | ||
</DefaultButton>; | ||
|
||
return ( | ||
<div className='ms-BasicButtonsExample'> | ||
<Label>Click to swap button types</Label> | ||
{ button } | ||
</div> | ||
); | ||
} | ||
|
||
@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 }); | ||
} | ||
} |