office-ui-fabric-react
Version:
Reusable React components for building experiences for Office 365.
49 lines • 2.46 kB
JavaScript
import * as tslib_1 from "tslib";
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { css, classNamesFunction } from 'office-ui-fabric-react/lib/Utilities';
import { getStyles } from './Button.Basic.Example.styles';
import { DefaultButton, PrimaryButton } from 'office-ui-fabric-react/lib/Button';
var ButtonSwapExample = /** @class */ (function (_super) {
tslib_1.__extends(ButtonSwapExample, _super);
function ButtonSwapExample(props) {
var _this = _super.call(this, props) || this;
_this._setButtonRef = function (ref) {
_this.buttonRef = ReactDOM.findDOMNode(ref);
};
_this._onClick = function () {
// change the button type on click
_this.setState({ isPrimary: !_this.state.isPrimary });
};
_this.hasFocus = false;
_this.buttonRef = null;
_this.state = {
isPrimary: true
};
return _this;
}
ButtonSwapExample.prototype.componentWillUpdate = function (nextProps, nextState) {
// check to see if our button element has focus
this.hasFocus = document.activeElement === this.buttonRef;
};
ButtonSwapExample.prototype.componentDidUpdate = function (prevProps, prevState) {
// 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();
}
};
ButtonSwapExample.prototype.render = function () {
var isPrimary = this.state.isPrimary;
var _a = this.props, disabled = _a.disabled, checked = _a.checked;
var text = 'Swap';
// determine which button to render
var button = isPrimary ? (React.createElement(PrimaryButton, { ref: this._setButtonRef, disabled: disabled, checked: checked, onClick: this._onClick }, text)) : (React.createElement(DefaultButton, { ref: this._setButtonRef, disabled: disabled, checked: checked, onClick: this._onClick }, text));
var getClassNames = classNamesFunction();
var classNames = getClassNames(getStyles, {});
return React.createElement("div", { className: css(classNames.example) }, button);
};
return ButtonSwapExample;
}(React.Component));
export { ButtonSwapExample };
//# sourceMappingURL=Button.Swap.Example.js.map