Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS migration] Migrate 'ReportActionComposeFocusManager.js' lib to TypeScript #26868

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import _ from 'underscore';
import React from 'react';
import {TextInput} from 'react-native';

const composerRef = React.createRef();
type FocusCallback = () => void;

const composerRef = React.createRef<TextInput>();
// There are two types of composer: general composer (edit composer) and main composer.
// The general composer callback will take priority if it exists.
let focusCallback = null;
let mainComposerFocusCallback = null;
let focusCallback: FocusCallback | null = null;
let mainComposerFocusCallback: FocusCallback | null = null;

/**
* Register a callback to be called when focus is requested.
* Typical uses of this would be call the focus on the ReportActionComposer.
*
* @param {Function} callback callback to register
* @param {Boolean} isMainComposer
* @param callback callback to register
*/
function onComposerFocus(callback, isMainComposer = false) {
function onComposerFocus(callback: FocusCallback, isMainComposer = false) {
if (isMainComposer) {
mainComposerFocusCallback = callback;
} else {
Expand All @@ -24,11 +25,10 @@ function onComposerFocus(callback, isMainComposer = false) {

/**
* Request focus on the ReportActionComposer
*
*/
function focus() {
if (!_.isFunction(focusCallback)) {
if (!_.isFunction(mainComposerFocusCallback)) {
if (typeof focusCallback !== 'function') {
if (typeof mainComposerFocusCallback !== 'function') {
return;
}

Expand All @@ -41,8 +41,6 @@ function focus() {

/**
* Clear the registered focus callback
*
* @param {Boolean} isMainComposer
*/
function clear(isMainComposer = false) {
if (isMainComposer) {
Expand All @@ -54,10 +52,9 @@ function clear(isMainComposer = false) {

/**
* Exposes the current focus state of the report action composer.
* @return {Boolean} isFocused
*/
function isFocused() {
return composerRef.current && composerRef.current.isFocused();
function isFocused(): boolean {
return !!composerRef.current?.isFocused();
}

export default {
Expand Down
Loading