react-native-paper icon indicating copy to clipboard operation
react-native-paper copied to clipboard

(FlatInput): change label fontFamily

Open fershibli opened this issue 1 year ago • 2 comments

Description

I've been trying to customize my Input mode="flat", but one thing that I wasn't able to figure out (even reading the source code) was How to change the fontFamily of my Input's label.

I looked over a lot of issues on this repository and I was able to remove the underline from the input and have a different visual for when it is selected/filled.

While looking the original source, I found that I could change it through the project's Theme, but I'm using styled-components/native's ThemeProvider, I'm not sure the cleanest way to accomplish that. It would be better for me to change it directly on the component.

Please, help me to change the label fontFamily. I would be very grateful. Thanks!

Screenshot 2023-12-12 at 19 58 41

Coding

import React from 'react';
import { Styled, StyleSheet } from './styled';

type FlatInputProps = {
  label: string;
  value: string;
  onChangeText: (text: string) => void;
};

const FlatInput: React.FC<FlatInputProps> = ({
  label,
  value,
  onChangeText,
}) => {
  const [isSelected, setIsSelected] = React.useState(false);
  return (
    <Styled.TextInput
      mode="flat"
      label={label}
      value={value}
      onChangeText={(content) => onChangeText(content)}
      onFocus={() => setIsSelected(true)}
      onBlur={() => setIsSelected(false)}
      underlineColor="transparent"
      underlineStyle={StyleSheet.TextInputUnderline}
      theme={StyleSheet.TextInputTheme}
      contentStyle={StyleSheet.TextInputContentStyle}
      bgColor={value.length || isSelected ? 'white' : ''}
    />
  );
};

export default FlatInput;

import styled from 'styled-components/native';
import { TextInput } from 'react-native-paper';
import FONTS from '@src/theme/fonts';

export const Styled = {
  TextInput: styled(TextInput)<{ bgColor?: string }>`
    width: 100%;
    border: 2px solid #f1f2fe;
    background-color: ${(p) => p.bgColor || '#f1f2fe'};
    font-weight: 700;
    margin-bottom: 8px;
  `,
};

export const StyleSheet = {
  TextInputUnderline: {
    display: 'none',
  },
  TextInputTheme: {
    colors: {
      primary: '#2B3267',
      placeholder: '#2B3267',
    },
  },
  TextInputContentStyle: {
    fontFamily: FONTS?.GT_WALSHEIM,
  },
};

fershibli avatar Dec 12 '23 23:12 fershibli

I was able to achieve it on iOS by assigning label as a styled Text

<Styled.TextInput
  mode="flat"
  label={<Styled.TextH6>{label}</Styled.TextH6>}
  ... />

But as you can see bellow, it didn't work for Android. Any ideas of how to solve this?

Screenshot 2023-12-13 at 09 40 01 (iOS on the left, Android on the right)

fershibli avatar Dec 13 '23 12:12 fershibli

Hey @fershibli, could you please create an expo snack or sample github repo from your code?

lukewalczak avatar Jan 08 '24 12:01 lukewalczak