73

I am using TextInput for a project and wanted to DISABLE any kind of text selection or actions like (cut/copy/paste/share) as shared in the screenshot below.

I am not able find anything in the react-native official documentation

enter image description here

firebolt_ash
  • 1,204
  • 4
  • 13
  • 21

9 Answers9

153

You should add 2 attributes selectTextOnFocus and editable

For example:

<TextInput editable={false} selectTextOnFocus={false} />
Artem Tutov
  • 1,827
  • 2
  • 12
  • 12
  • 1
    editable false - i cant even edit the text input now - I need these when i can edit but cannot copy/cut/paste on this text input – firebolt_ash Mar 20 '17 at 06:41
  • 1
    when I give the editable false how can I use TextInput.I want to make copy/paste option disable no need to give editable={false} – saiRam89 Jul 21 '17 at 09:29
26

contextMenuHidden is to disable the user from pasting text into certain fields and to hide context menu.

Update: This hasn’t been included in a release yet. You can always see what release any commit is in by clicking on the link and looking at the tags. so I wouldn't expect it to be on a stable release until 0.55.

<TextInput contextMenuHidden={true} />

Check the commit here: Add option to hide context menu for TextInput

Balasubramanian
  • 5,274
  • 6
  • 33
  • 62
24

Just give your textinput the attribute editable={false}

funkysoul
  • 3,023
  • 1
  • 17
  • 27
23

Set pointerEvents to none on parent View of TextInput to disable touch events, consider following example:

<View pointerEvents="none">
  <TextInput ... />
</View>
Pir Shukarullah Shah
  • 4,124
  • 2
  • 22
  • 42
Shine
  • 297
  • 3
  • 8
3

You can use a View and use removeClippedSubviews={true} (works with Android) and use contextMenuHidden={true} (works with IOS)

<View removeClippedSubviews={true}> <TextInput contextMenuHidden={true} /> </View>

linhadiretalipe
  • 907
  • 7
  • 8
0

Reference from the following issue https://github.com/facebook/react-native/issues/33697

const App = () => {
  const [selection, setSelection] = useState(null);
  const [text, setText] = useState('');

  return (
    <TextInput 
      selection={selection}
      onSelectionChange={(e) => setSelection(e.nativeEvent.selection)}
      value={text}
      onChangeText={(text) => setText(text)}
      style={{ alignSelf: 'center', width: 200, marginTop: 50, backgroundColor: 'grey' }} 
    />
  );
};
0

Below solution works for me for avoiding copy/paste. I am clearing keyboard on onTouchEnd event

const [text1, setText1] = useState('')
    
const clearClipboard = () =>{
 Clipboard.setString('')
}
    
const onChangeText = (text) =>{
 //For android 12+ clipboard autofill option, dont allow text change more than one char..which means user is typing.
 if(text1.length+1 >= text.length){
  setText1(text)
 }
}
    
<TextInput onChangeText={onChangeText} value={text1} contextMenuHidden={true} onTouchEnd={clearClipboard}></TextInput>
Vinay Revankar
  • 823
  • 9
  • 19
-1

Use caretHidden={true} if you want to disable all operation like Cut Paste Copy. It will also hide your cursor as well

Anil Chahal
  • 2,544
  • 2
  • 22
  • 19
-4

This trick worked for me. Here I am using NativeBase. Keep this <TextInput> inside a <Item> tag. Now the selection property should not work.

code sample attached below.

<Item>
<Input
  value={this.props.call_state.destination}
  onChangeText={text => this.props.setDestination(text)}
  returnKeyType={"done"}
  keyboardType={"numeric"}
/>
</Item>

You should install nativebase first and then import {Item} from native-base in your component

zmag
  • 7,825
  • 12
  • 32
  • 42
Pallab Naskar
  • 59
  • 1
  • 6