Skip to content

Commit

Permalink
fix: add functional ref support for 'inputRef' prop
Browse files Browse the repository at this point in the history
  • Loading branch information
dabakovich committed Dec 22, 2020
1 parent 31285d8 commit bee0304
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/components/mentions.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useMemo, useRef, useState } from 'react';
import React, { FC, MutableRefObject, useMemo, useRef, useState } from 'react';
import {
NativeSyntheticEvent,
Text,
Expand Down Expand Up @@ -267,7 +267,14 @@ const Mentions: FC<MentionsProps> = (

const handleTextInputRef = (ref: TextInput) => {
textInput.current = ref as TextInput;
if (propInputRef) propInputRef.current = ref as TextInput;

if (propInputRef) {
if (typeof propInputRef === 'function') {
propInputRef(ref);
} else {
(propInputRef as MutableRefObject<TextInput>).current = ref as TextInput;
}
}
};

return (
Expand Down
4 changes: 2 additions & 2 deletions src/types/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReactNode, Ref } from 'react';
import { StyleProp, TextInput, TextInputProps, TextStyle, ViewStyle } from 'react-native';
import { MutableRefObject, ReactNode } from 'react';

type Suggestion = {
id: string;
Expand Down Expand Up @@ -49,7 +49,7 @@ type MentionsProps = Omit<TextInputProps, 'onChange'> & {
// Should we add a space after selected mentions if the mention is at the end of row
isInsertSpaceAfterMention?: boolean;

inputRef?: MutableRefObject<TextInput | null>;
inputRef?: Ref<TextInput>;

mentionTextStyle?: StyleProp<TextStyle>;

Expand Down

0 comments on commit bee0304

Please sign in to comment.