about summary refs log tree commit diff
path: root/src/view/com/composer/text-input/TextInput.tsx
blob: be6150e112a7911674b5df91ba7fc146eeed0f52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React from 'react'
import {
  NativeSyntheticEvent,
  StyleProp,
  TextInputSelectionChangeEventData,
  TextStyle,
} from 'react-native'
import PasteInput, {
  PastedFile,
  PasteInputRef,
} from '@mattermost/react-native-paste-input'
import {usePalette} from 'lib/hooks/usePalette'

export type TextInputRef = PasteInputRef

interface TextInputProps {
  testID: string
  innerRef: React.Ref<TextInputRef>
  placeholder: string
  style: StyleProp<TextStyle>
  onChangeText: (str: string) => void
  onSelectionChange?:
    | ((e: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => void)
    | undefined
  onPaste: (err: string | undefined, uris: string[]) => void
}

export function TextInput({
  testID,
  innerRef,
  placeholder,
  style,
  onChangeText,
  onSelectionChange,
  onPaste,
  children,
}: React.PropsWithChildren<TextInputProps>) {
  const pal = usePalette('default')
  const onPasteInner = (err: string | undefined, files: PastedFile[]) => {
    if (err) {
      onPaste(err, [])
    } else {
      onPaste(
        undefined,
        files.map(f => f.uri),
      )
    }
  }
  return (
    <PasteInput
      testID={testID}
      ref={innerRef}
      multiline
      scrollEnabled
      onChangeText={(str: string) => onChangeText(str)}
      onSelectionChange={onSelectionChange}
      onPaste={onPasteInner}
      placeholder={placeholder}
      placeholderTextColor={pal.colors.textLight}
      style={style}>
      {children}
    </PasteInput>
  )
}