Skip to content

Commit

Permalink
feat: change state to ref
Browse files Browse the repository at this point in the history
  • Loading branch information
BartoszGrajdek committed Mar 4, 2024
1 parent 0ad69db commit 1f469f0
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
TextInputKeyPressEventData,
TextInputFocusEventData,
} from 'react-native';
import React, {useEffect, useRef, useCallback, useMemo, useLayoutEffect, useState} from 'react';
import React, {useEffect, useRef, useCallback, useMemo, useLayoutEffect} from 'react';
import type {CSSProperties, MutableRefObject, ReactEventHandler, FocusEventHandler, MouseEvent, KeyboardEvent, SyntheticEvent} from 'react';
import {StyleSheet} from 'react-native';
import * as ParseUtils from './web/parserUtils';
Expand Down Expand Up @@ -156,7 +156,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
},
ref,
) => {
const [isComposition, setIsComposition] = useState(false);
const compositionRef = useRef<boolean | null>(null);
const divRef = useRef<HTMLDivElement | null>(null);
const currentlyFocusedField = useRef<HTMLDivElement | null>(null);
const contentSelection = useRef<Selection | null>(null);
Expand Down Expand Up @@ -270,8 +270,8 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
return;
}

if (isComposition) {
setIsComposition(false);
if (compositionRef.current) {
compositionRef.current = false;
return;
}

Expand Down Expand Up @@ -300,7 +300,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
onChangeText(normalizedText);
}
},
[multiline, isComposition, onChange, onChangeText, setEventProps, processedMarkdownStyle],
[multiline, onChange, onChangeText, setEventProps, processedMarkdownStyle],
);

const handleKeyPress = useCallback(
Expand Down Expand Up @@ -520,6 +520,10 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
divRef.current.focus();
}, []);

const startComposition = () => {
compositionRef.current = true;
};

return (
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
<div
Expand All @@ -535,7 +539,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
autoCapitalize={autoCapitalize}
className={className}
onKeyDown={handleKeyPress}
onCompositionStart={() => setIsComposition(true)}
onCompositionStart={startComposition}
onKeyUp={updateSelection}
onInput={handleOnChangeText}
onSelect={handleSelectionChange}
Expand Down

0 comments on commit 1f469f0

Please sign in to comment.