react-focus-flow
Version:
React components to define isolated focus flows
138 lines (112 loc) • 5.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _FocusFlow = _interopRequireDefault(require("./FocusFlow"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var Focusable =
/*#__PURE__*/
function (_React$Component) {
_inherits(Focusable, _React$Component);
function Focusable(props) {
var _this;
_classCallCheck(this, Focusable);
_this = _possibleConstructorReturn(this, (Focusable.__proto__ || Object.getPrototypeOf(Focusable)).call(this, props));
_this.index = props.index;
return _this;
}
_createClass(Focusable, [{
key: "componentWillMount",
value: function componentWillMount() {
if (!this.context.focusFlow) {
throw new Error("All <Focusable> components must be a descendant of a <FocusFlow> component");
}
this.focusFlow = this.context.focusFlow;
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
// React translates an autoFocus on an element into
// an imperative .focus() on mount - so we can't look at ref.autofocus
// to see if the user wanted autofocus set.
// So, if you want to autofocus, you need to put it on Focusable component i.e.,
// <Focusable autoFocus .../>
if (this.props.autoFocus) {
this.focusFlow.setActive(this);
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.focusFlow.remove(this);
}
}, {
key: "focus",
value: function focus() {
console.log("focus call");
var onFocusTurn = this.props.onFocusTurn;
if (onFocusTurn) {
onFocusTurn();
} else {
this.node.focus();
}
}
}, {
key: "isDisabled",
value: function isDisabled() {
return this.node.disabled;
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var child = _react.default.Children.only(this.props.children);
return _react.default.cloneElement(child, {
ref: function ref(node) {
// don't overwrite ref callback if present
if (typeof child.ref === "function") {
child.ref(node);
}
if (node === null) {
return;
}
_this2.node = node;
_this2.focusFlow.add(_this2);
},
onClick: function onClick(e) {
// don't overwrite onClick callback if present
if (typeof child.props.onClick === "function") {
child.props.onClick(e);
}
if (!e.defaultPrevented) {
_this2.focusFlow.setActive(_this2);
}
}
});
}
}]);
return Focusable;
}(_react.default.Component);
exports.default = Focusable;
Focusable.propTypes = {
index: _propTypes.default.number.isRequired,
autoFocus: _propTypes.default.bool,
children: _propTypes.default.element.isRequired,
onFocusTurn: _propTypes.default.func
};
Focusable.defaultProps = {
autoFocus: false,
onFocusTurn: undefined
};
Focusable.contextTypes = {
focusFlow: _propTypes.default.instanceOf(_FocusFlow.default).isRequired
};