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 Composer to TypeScript #31912

Merged
merged 22 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f1c060b
migrate index.ios.tsx to TypeScript
JKobrynski Nov 22, 2023
32f4140
migrate index.android.js to TypeScript
JKobrynski Nov 22, 2023
ef6cbd6
start migrating index.js
JKobrynski Nov 24, 2023
bc23a01
fix forwarded ref
JKobrynski Nov 24, 2023
3b27bf8
Fixes in refs and events
fabioh8010 Nov 24, 2023
0a02f93
remove unsupported multiline prop
JKobrynski Nov 27, 2023
dc94cd7
remove mutliline prop
JKobrynski Nov 27, 2023
2aac63a
Merge branch 'main' into migrateComposerToTypeScript
JKobrynski Nov 27, 2023
000fc54
cleanup the code
JKobrynski Nov 27, 2023
205469a
apply suggested improvements
JKobrynski Nov 28, 2023
d70b49d
apply improvements, unify ComposerProps type
JKobrynski Nov 28, 2023
da409ec
Merge branch 'main' into migrateComposerToTypeScript
JKobrynski Nov 30, 2023
c8ec585
Merge branch 'main' into migrateComposerToTypeScript
JKobrynski Nov 30, 2023
235f9a3
Merge branch 'main' into migrateComposerToTypeScript
JKobrynski Dec 1, 2023
76ac410
Merge branch 'main' into migrateComposerToTypeScript
JKobrynski Dec 4, 2023
40de00c
remove ref from if statement
JKobrynski Dec 5, 2023
76c7334
add optional chaining to onFocus call
JKobrynski Dec 5, 2023
5370049
Merge branch 'main' into migrateComposerToTypeScript
JKobrynski Dec 7, 2023
49e358c
Merge branch 'main' into migrateComposerToTypeScript
JKobrynski Dec 8, 2023
0a91ea7
move selection destructuring to the top
JKobrynski Dec 11, 2023
4114784
Merge branch 'main' into migrateComposerToTypeScript
JKobrynski Dec 14, 2023
1c26c3e
change variable names
JKobrynski Dec 18, 2023
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,100 +1,91 @@
import PropTypes from 'prop-types';
import React, {useCallback, useEffect, useMemo, useRef} from 'react';
import {StyleSheet} from 'react-native';
import _ from 'underscore';
import React, {ForwardedRef, useCallback, useEffect, useMemo, useRef} from 'react';
import {StyleProp, StyleSheet, TextInput, TextStyle} from 'react-native';
import RNTextInput from '@components/RNTextInput';
import * as ComposerUtils from '@libs/ComposerUtils';
import themeColors from '@styles/themes/default';
import {TextSelection} from './types';

const propTypes = {
type ComposerProps = {
/** Maximum number of lines in the text input */
JKobrynski marked this conversation as resolved.
Show resolved Hide resolved
maxLines: PropTypes.number,
maxLines: number;

/** If the input should clear, it actually gets intercepted instead of .clear() */
shouldClear: PropTypes.bool,

/** A ref to forward to the text input */
forwardedRef: PropTypes.func,
shouldClear: boolean;

/** When the input has cleared whoever owns this input should know about it */
onClear: PropTypes.func,
onClear: () => void;

/** Set focus to this component the first time it renders.
* Override this in case you need to set focus on one field out of many, or when you want to disable autoFocus */
autoFocus: PropTypes.bool,
autoFocus: boolean;

/** Prevent edits and interactions like focus for this input. */
isDisabled: PropTypes.bool,
isDisabled: boolean;

/** Selection Object */
selection: PropTypes.shape({
start: PropTypes.number,
end: PropTypes.number,
}),
selection: TextSelection;

/** Whether the full composer can be opened */
isFullComposerAvailable: PropTypes.bool,
isFullComposerAvailable: boolean;

/** Allow the full composer to be opened */
setIsFullComposerAvailable: PropTypes.func,
setIsFullComposerAvailable: () => void;

/** Whether the composer is full size */
isComposerFullSize: PropTypes.bool,
isComposerFullSize: boolean;

/** General styles to apply to the text input */
// eslint-disable-next-line react/forbid-prop-types
style: PropTypes.any,
};

const defaultProps = {
shouldClear: false,
onClear: () => {},
autoFocus: false,
isDisabled: false,
forwardedRef: null,
selection: {
start: 0,
end: 0,
},
maxLines: undefined,
isFullComposerAvailable: false,
setIsFullComposerAvailable: () => {},
isComposerFullSize: false,
style: null,
style: StyleProp<TextStyle>;
};

function Composer({shouldClear, onClear, isDisabled, maxLines, forwardedRef, isComposerFullSize, setIsFullComposerAvailable, ...props}) {
const textInput = useRef(null);
function Composer(
{
shouldClear = false,
onClear = () => {},
isDisabled = false,
maxLines,
isComposerFullSize = false,
setIsFullComposerAvailable = () => {},
style,
autoFocus = false,
selection = {
start: 0,
end: 0,
},
isFullComposerAvailable = false,
...props
}: ComposerProps,
ref: ForwardedRef<TextInput>,
) {
const textInput = useRef<TextInput>();

JKobrynski marked this conversation as resolved.
Show resolved Hide resolved
/**
* Set the TextInput Ref
* @param {Element} el
*/
const setTextInputRef = useCallback((el) => {
const setTextInputRef = useCallback((el: TextInput) => {
textInput.current = el;
if (!_.isFunction(forwardedRef) || textInput.current === null) {
if (typeof ref !== 'function' || textInput.current === null) {
return;
}

// This callback prop is used by the parent component using the constructor to
// get a ref to the inner textInput element e.g. if we do
// <constructor ref={el => this.textInput = el} /> this will not
// return a ref to the component, but rather the HTML element by default
forwardedRef(textInput.current);
ref(textInput.current);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
if (!shouldClear) {
return;
}
textInput.current.clear();
textInput.current?.clear();
onClear();
}, [shouldClear, onClear]);

/**
* Set maximum number of lines
* @return {Number}
*/
const maxNumberOfLines = useMemo(() => {
if (isComposerFullSize) {
Expand All @@ -103,9 +94,7 @@ function Composer({shouldClear, onClear, isDisabled, maxLines, forwardedRef, isC
return maxLines;
}, [isComposerFullSize, maxLines]);

const composerStyles = useMemo(() => {
StyleSheet.flatten(props.style);
}, [props.style]);
const composerStyles = useMemo(() => StyleSheet.flatten(style), [style]);

JKobrynski marked this conversation as resolved.
Show resolved Hide resolved
return (
<RNTextInput
Expand All @@ -121,24 +110,16 @@ function Composer({shouldClear, onClear, isDisabled, maxLines, forwardedRef, isC
maxNumberOfLines={maxNumberOfLines}
textAlignVertical="center"
style={[composerStyles]}
readOnly={isDisabled}
autoFocus={autoFocus}
selection={selection}
isFullComposerAvailable={isFullComposerAvailable}
/* eslint-disable-next-line react/jsx-props-no-spreading */
{...props}
readOnly={isDisabled}
/>
JKobrynski marked this conversation as resolved.
Show resolved Hide resolved
);
}

Composer.propTypes = propTypes;
Composer.defaultProps = defaultProps;

const ComposerWithRef = React.forwardRef((props, ref) => (
<Composer
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
forwardedRef={ref}
/>
));

ComposerWithRef.displayName = 'ComposerWithRef';
Composer.displayName = 'Composer';

export default ComposerWithRef;
export default React.forwardRef(Composer);
145 changes: 0 additions & 145 deletions src/components/Composer/index.ios.js

This file was deleted.

Loading
Loading