question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add custom elements to TextInput left or right

See original GitHub issue

<TextInput // label={label} value={val} style={this.styles.TextInput} placeholder={placeholder} left={ <TextInput.Icon color="#8C8C8C" icon="lock" size={30} /> } right={ <Button mode="outlined" disabled={hasPhone} compact> "confirm" </Button> } secureTextEntry={isPwd} onChangeText={(text) => this.setText(text, filed) } /> Left can be show Element, but right can’t.

“react-native”: “0.63.2”, “react-native-paper”: “^4.1.0”, “react-native-vector-icons”: “^7.1.0”,

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
gmontecommented, Feb 14, 2021

I resolved the problem using the render prop and styled-components, like that:

import { TextInput } from 'react-native-paper'
import styled from 'styled-components/native'

const InputContainer = styled.View`
  flex-direction: row;
`
const Input = styled.TextInput`
  flex: 1;
`
const AdornmentContainer = styled.View`
  align-items: center;
  justify-content: center;
  padding: 0 10px;
`

const CustomInput = ({ left, right, ...props }) => (
  <TextInput
    { ...props }
    render={ (inputProps) => (
      <InputContainer>
        {left && (
          <AdornmentContainer>
            {left}
          </AdornmentContainer>
        )}
        <Input { ...inputProps } />
        {right && (
          <AdornmentContainer>
            {right}
          </AdornmentContainer>
        )}
      </InputContainer>
    ) }
   />
)

Now I am able to render anything as “left” and “right”.

4reactions
kaimecommented, Dec 5, 2020

Why don’t just allow any component to be passed as left or right? I’d like to place a non-clickable icon on the left of a <TextInput>:

image

But it won’t work because the TextInputAdornment function won’t render any element that’s not an AdornmentType.Icon or an AdornmentType.Affix:

if (type === AdornmentType.Icon) {
  return (
    <IconAdornment ... />
  );
} else if (type === AdornmentType.Affix) {
  return (
    <AffixAdornment ... />
  );
} else {
  return null;
}

If a just use a <TextInput.Icon> without an onPress prop, then I get an <IconButton>, which will receive cursor events and show a ripple when tapped.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A complete guide to TextInput in React Native - LogRocket Blog
Learn how to implement React Native's TextInput component, customize it to collect user inputs, and style your fields with React Native ...
Read more >
How to add icon left side in InputText in React Native
You should make use of TextInput.Icon and the left or right property as explained in the documentation here import React from 'react'; ...
Read more >
TextInput · React Native Paper
A component to allow users to input text. ... left. Type: React.ReactNode. right. Type: React.ReactNode. disabled. Type: boolean. Default value: false.
Read more >
Custom Text Input. React Native Beginner Project Course.#7
Custom Text Input. React Native Beginner Project Course.#7Videos for the backend REST API ...
Read more >
TextInput - React Native
Two methods exposed via the native element are .focus() and .blur() that will focus or blur the TextInput programmatically. Note that some props ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found