react-native-rich-editor icon indicating copy to clipboard operation
react-native-rich-editor copied to clipboard

Can't dismiss keyboard programmatically

Open stesvis opened this issue 1 year ago • 6 comments

Once you focus on the RichEditor and the keyboard shows, I am unable to close it.

You should be able to automatically close the keyboard by tapping outside of the editor (same behavior of the TextInput) or by calling Keyboard.dismiss().

None of those approaches work. In Android you can use the hardware button to close it, but in iOS there is no way.

How do you dismiss the keyboard?? Basically it seems impossible to "blur" the control.

stesvis avatar Mar 10 '23 00:03 stesvis

Same issue here when screen open the keyboard comes and you can never dismiss it. If somebody has a solution please post here

sujathasperi2022 avatar Apr 04 '23 03:04 sujathasperi2022

 /**
   * Dismisses the active keyboard and removes focus.
   */
  dismissKeyboard: () => void;

stulip avatar Apr 04 '23 04:04 stulip

solution?

Jamal-ReachFirst avatar Jul 15 '23 21:07 Jamal-ReachFirst

Solution?

cassiocsantana avatar Nov 10 '23 15:11 cassiocsantana

+1

Jamal-ReachFirst avatar Dec 11 '23 18:12 Jamal-ReachFirst

original answer with touchable:

// I use this for iOS (on android you can close keyboard yourself)
const richText = useRef(null);

<TouchableWithoutFeedback
  disable={Platform.OS === 'android'}
  onPress={() => {
    richText.current?.blurContentEditor(); // or richText.current?.dismissKeyboard();
  }}
>
  <RichEditor
    ref={(r) => {
      richText.current = r;
    }}
  />
</TouchableWithoutFeedback>

Currently I'm not using this solution with touchablewithoutfeedback because I wanted it to work on android also but it doesn't work on all android phones: https://github.com/wxik/react-native-rich-editor/issues/124#issuecomment-756316041

now I use this:

const richText = useRef(null);
  const measureView = useRef<View>(null);

  const [scrollY, setScrollY] = useState(0); // needed your rich-text located inside scrollview
  const [measure, setMeasure] = useState(null);
  useEffect(() => {
    measureView.current?.measure((x, y, width, height, pageX, pageY) => {
      const location = {
        x,
        y,
        width,
        height,
        pageX,
        pageY,
      };
      setMeasure(location);
    });
  }, [measureView.current]);

  return (
    ... scrollview with onScroll={(e) => setScrollY(e.nativeEvent.contentOffset.y)}...
    <View
      onStartShouldSetResponder={(e) => {
        if (
          e.nativeEvent.pageX <= measure.pageX + measure.width &&
          e.nativeEvent.pageX >= measure.pageX &&
          e.nativeEvent.pageY  + scrollY <= measure.pageY + measure.height &&   // remove scrollY in both lines if you don't have vertical scrollview
          e.nativeEvent.pageY  + scrollY >= measure.pageY
        ) {
          // after testing, seems like manually calling focusContentEditor() isn't needed
          // richText.current?.focusContentEditor();
          return false;
        } else {
          richText.current?.blurContentEditor(); // or richText.current?.dismissKeyboard();
          return true;
        }
      }}
    >
      <View collapsable={false} ref={measureView}>
        <RichEditor
          ref={(r) => {
            richText.current = r;
          }}
        />
      </View>
    </View>
   ...
  );

hssdiv avatar Feb 20 '24 12:02 hssdiv