react-native-tvfocus
Version:
React Native tvOS and Android TV library to improve focus management with multiple screens.
39 lines (38 loc) • 1.44 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const React = require("react");
const focus_manager_1 = require("./focus-manager");
let key = 0;
class Focusable extends React.Component {
constructor() {
super(...arguments);
this.id = key++;
this.focus = null;
this.onFocus = event => {
var _a, _b;
// @ts-expect-error
this.focus.lastfocused = this.id;
(_b = (_a = this.props).onFocus) === null || _b === void 0 ? void 0 : _b.call(_a, event);
};
}
componentDidMount() {
this.focus = this.context.focus;
this.focus.focusables.set(this.id, this);
}
componentDidUpdate() {
var _a, _b;
if (this.focus !== this.context.focus) {
(_a = this.focus) === null || _a === void 0 ? void 0 : _a.focusables.delete(this.id);
(_b = this.focus) === null || _b === void 0 ? void 0 : _b.handleRemovedFocusable(this);
this.focus = this.context.focus;
this.focus.focusables.set(this.id, this);
}
}
componentWillUnmount() {
var _a, _b;
(_a = this.focus) === null || _a === void 0 ? void 0 : _a.focusables.delete(this.id);
(_b = this.focus) === null || _b === void 0 ? void 0 : _b.handleRemovedFocusable(this);
}
}
exports.default = Focusable;
Focusable.contextType = focus_manager_1.FocusManagerContext;