Skip to content

Commit

Permalink
Merge pull request #39519 from software-mansion-labs/@Skalakid/new-ta…
Browse files Browse the repository at this point in the history
…sk-page-live-markdown

Implement Live Markdown for task descriptions
  • Loading branch information
roryabraham authored Apr 12, 2024
2 parents 159bb6d + a0cd3d7 commit dff9542
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
18 changes: 14 additions & 4 deletions src/components/TextInput/BaseTextInput/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import FormHelpMessage from '@components/FormHelpMessage';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback';
import type {AnimatedMarkdownTextInputRef} from '@components/RNMarkdownTextInput';
import RNMarkdownTextInput from '@components/RNMarkdownTextInput';
import type {AnimatedTextInputRef} from '@components/RNTextInput';
import RNTextInput from '@components/RNTextInput';
import Text from '@components/Text';
import * as styleConst from '@components/TextInput/styleConst';
import TextInputLabel from '@components/TextInput/TextInputLabel';
import useLocalize from '@hooks/useLocalize';
import useMarkdownStyle from '@hooks/useMarkdownStyle';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
Expand Down Expand Up @@ -54,13 +58,17 @@ function BaseTextInput(
autoCorrect = true,
prefixCharacter = '',
inputID,
isMarkdownEnabled = false,
...props
}: BaseTextInputProps,
ref: ForwardedRef<BaseTextInputRef>,
) {
const InputComponent = isMarkdownEnabled ? RNMarkdownTextInput : RNTextInput;

const inputProps = {shouldSaveDraft: false, shouldUseDefaultValue: false, ...props};
const theme = useTheme();
const styles = useThemeStyles();
const markdownStyle = useMarkdownStyle();
const StyleUtils = useStyleUtils();
const {translate} = useLocalize();

Expand Down Expand Up @@ -321,13 +329,14 @@ function BaseTextInput(
</Text>
</View>
)}
<RNTextInput
ref={(element) => {
<InputComponent
ref={(element: AnimatedTextInputRef | AnimatedMarkdownTextInputRef | null): void => {
const baseTextInputRef = element as BaseTextInputRef | null;
if (typeof ref === 'function') {
ref(element);
ref(baseTextInputRef);
} else if (ref && 'current' in ref) {
// eslint-disable-next-line no-param-reassign
ref.current = element;
ref.current = baseTextInputRef;
}

input.current = element;
Expand Down Expand Up @@ -370,6 +379,7 @@ function BaseTextInput(
selection={inputProps.selection}
readOnly={isReadOnly}
defaultValue={defaultValue}
markdownStyle={markdownStyle}
/>
{inputProps.isLoading && (
<ActivityIndicator
Expand Down
18 changes: 14 additions & 4 deletions src/components/TextInput/BaseTextInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import FormHelpMessage from '@components/FormHelpMessage';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback';
import type {AnimatedMarkdownTextInputRef} from '@components/RNMarkdownTextInput';
import RNMarkdownTextInput from '@components/RNMarkdownTextInput';
import type {AnimatedTextInputRef} from '@components/RNTextInput';
import RNTextInput from '@components/RNTextInput';
import SwipeInterceptPanResponder from '@components/SwipeInterceptPanResponder';
import Text from '@components/Text';
import * as styleConst from '@components/TextInput/styleConst';
import TextInputLabel from '@components/TextInput/TextInputLabel';
import useLocalize from '@hooks/useLocalize';
import useMarkdownStyle from '@hooks/useMarkdownStyle';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
Expand Down Expand Up @@ -56,12 +60,16 @@ function BaseTextInput(
autoCorrect = true,
prefixCharacter = '',
inputID,
isMarkdownEnabled = false,
...inputProps
}: BaseTextInputProps,
ref: ForwardedRef<BaseTextInputRef>,
) {
const InputComponent = isMarkdownEnabled ? RNMarkdownTextInput : RNTextInput;

const theme = useTheme();
const styles = useThemeStyles();
const markdownStyle = useMarkdownStyle();
const {hasError = false} = inputProps;
const StyleUtils = useStyleUtils();
const {translate} = useLocalize();
Expand Down Expand Up @@ -341,13 +349,14 @@ function BaseTextInput(
</Text>
</View>
)}
<RNTextInput
ref={(element) => {
<InputComponent
ref={(element: AnimatedTextInputRef | AnimatedMarkdownTextInputRef | null): void => {
const baseTextInputRef = element as BaseTextInputRef | null;
if (typeof ref === 'function') {
ref(element);
ref(baseTextInputRef);
} else if (ref && 'current' in ref) {
// eslint-disable-next-line no-param-reassign
ref.current = element;
ref.current = baseTextInputRef;
}

input.current = element as HTMLInputElement | null;
Expand Down Expand Up @@ -396,6 +405,7 @@ function BaseTextInput(
selection={inputProps.selection}
readOnly={isReadOnly}
defaultValue={defaultValue}
markdownStyle={markdownStyle}
/>
{inputProps.isLoading && (
<ActivityIndicator
Expand Down
3 changes: 3 additions & 0 deletions src/components/TextInput/BaseTextInput/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ type CustomBaseTextInputProps = {

/** Type of autocomplete */
autoCompleteType?: string;

/** Should live markdown be enabled. Changes RNTextInput component to RNMarkdownTextInput */
isMarkdownEnabled?: boolean;
};

type BaseTextInputRef = HTMLFormElement | AnimatedTextInputRef;
Expand Down
1 change: 1 addition & 0 deletions src/pages/tasks/NewTaskDescriptionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function NewTaskDescriptionPage({task}: NewTaskDescriptionPageProps) {
autoGrowHeight
shouldSubmitForm
containerStyles={styles.autoGrowHeightMultilineInput}
isMarkdownEnabled
/>
</View>
</FormProvider>
Expand Down
1 change: 1 addition & 0 deletions src/pages/tasks/NewTaskDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ function NewTaskDetailsPage({task}: NewTaskDetailsPageProps) {
defaultValue={parser.htmlToMarkdown(parser.replace(taskDescription))}
value={taskDescription}
onValueChange={setTaskDescription}
isMarkdownEnabled
/>
</View>
</FormProvider>
Expand Down
1 change: 1 addition & 0 deletions src/pages/tasks/TaskDescriptionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ function TaskDescriptionPage({report, currentUserPersonalDetails}: TaskDescripti
autoGrowHeight
shouldSubmitForm
containerStyles={[styles.autoGrowHeightMultilineInput]}
isMarkdownEnabled
/>
</View>
</FormProvider>
Expand Down

0 comments on commit dff9542

Please sign in to comment.