mattermost-mobile/app/components/pasteable_text_input/index.js
Mattermost Build 4602fb916f
Emit PASTE_FILES event only from last subscribed input (#4872) (#4875)
(cherry picked from commit ae43ba1187)

Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com>
2020-10-05 13:18:04 -07:00

76 lines
2.1 KiB
JavaScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import PropTypes from 'prop-types';
import {NativeEventEmitter, NativeModules, Platform, TextInput} from 'react-native';
import {PASTE_FILES} from '@constants/post_draft';
import EventEmitter from '@mm-redux/utils/event_emitter';
const {OnPasteEventManager} = NativeModules;
const OnPasteEventEmitter = new NativeEventEmitter(OnPasteEventManager);
export class PasteableTextInput extends React.PureComponent {
static propTypes = {
...TextInput.PropTypes,
forwardRef: PropTypes.any,
}
componentDidMount() {
this.subscription = OnPasteEventEmitter.addListener('onPaste', this.onPaste);
}
componentWillUnmount() {
if (this.subscription) {
this.subscription.remove();
}
}
getLastSubscriptionKey = () => {
const subscriptions = OnPasteEventEmitter._subscriber._subscriptionsForType.onPaste?.filter((sub) => sub); // eslint-disable-line no-underscore-dangle
return subscriptions?.length && subscriptions[subscriptions.length - 1].key;
}
onPaste = (event) => {
const lastSubscriptionKey = this.getLastSubscriptionKey();
if (this.subscription.key !== lastSubscriptionKey) {
return;
}
let data = null;
let error = null;
if (Platform.OS === 'android') {
const {nativeEvent} = event;
data = nativeEvent.data;
error = nativeEvent.error;
} else {
data = event;
}
EventEmitter.emit(PASTE_FILES, error, data);
}
render() {
const {forwardRef, ...props} = this.props;
return (
<TextInput
testID='post_input'
{...props}
onPaste={this.onPaste}
ref={forwardRef}
/>
);
}
}
const WrappedPasteableTextInput = (props, ref) => (
<PasteableTextInput
{...props}
forwardRef={ref}
/>
);
export default React.forwardRef(WrappedPasteableTextInput);