tamagui: Select sometimes not working when mounted, unmounted and mounted again.

https://github.com/tamagui/tamagui/assets/47078705/98f63caf-70e9-4ca9-bff7-c1998b7e9277

tamagui@1.76.0

and also it sometimes it doesn’t work with multiple select is on the same screen

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 3
  • Comments: 15 (1 by maintainers)

Most upvoted comments

I have this issue when conditionally rendering a component which has select and adapt inside. I’ll try to make a reproducible sample tomorrow.

I have the same issue, v1.115.5. It seems to happen only on Android.

Does anyone have a solution for this? It would really save me.

@PerryEx64 are you sure the issue is not resolved yet? It’s been a while I worked on the project where I used Tamagui select. At that time, I ended up using another package - react-native-input-select , then I styled it to match my application’s requirement. That is what @coycoylaniba did, only that I used a different package because it kind of matched what I needed. My implementation looked like this:

import { ChevronDown } from '@tamagui/lucide-icons'
import { FieldValues, useController, useFormContext } from 'react-hook-form'
import { TextStyle } from 'react-native'
import Dropdown from 'react-native-input-select'
import { YStack } from 'tamagui'
import { colors } from '../../styles/colors'
import { ErrorText } from '../ErrorText'
import { Label } from '../Label'
import { HelperText } from './HelperText'
import { P1 } from '../BodyText'
import { ReactNode } from 'react'

interface ISelectField {
  name: string
  label?: string
  isRequired?: boolean
  helperText?: string
  placeholder?: string
  items: { name?: string; value: string }[]
  fit?: boolean
  percent?: number
  isSearchable?: boolean
  disabled?: boolean
  emptyListText?: string
  headerComponent?: ReactNode
}

export const SelectField = ({
  label,
  isRequired = false,
  name,
  helperText,
  placeholder,
  items,
  fit = false,
  percent = 60,
  isSearchable,
  disabled,
  emptyListText,
  headerComponent,
}: ISelectField) => {
  const { control } = useFormContext()
  const {
    field: { value, onChange, onBlur },
    fieldState: { error },
  } = useController<FieldValues>({ name, control })

  return (
    <YStack>
      {label && <Label>{label}</Label>}
      <Dropdown
        // label="Country"
        placeholder={placeholder}
        placeholderStyle={$placeholderStyle}
        options={items}
        selectedValue={value}
        onValueChange={(value) => onChange(value)}
        primaryColor={colors.primary1}
        optionLabel={(items[0] && items[0]?.name) ? 'name' : 'value'}
        optionValue="value"
        dropdownStyle={{
          borderWidth: 1, // To remove border, set borderWidth to 0
          backgroundColor: colors.sWhite,
          paddingHorizontal: 13,
          borderRadius: 12,
          borderColor: error ? colors.error : colors.grey6,
          minHeight: 10,
          paddingVertical: 18,
        }}
        dropdownIcon={
          <YStack>
            <ChevronDown color={colors.grey2} />
          </YStack>
        }
        dropdownContainerStyle={{
          marginBottom: 0,
        }}
        selectedItemStyle={{
          color: colors.sBlack,
        }}
        dropdownIconStyle={{ right: 8, top: 14 }}
        isSearchable={isSearchable}
        searchControls={{
          textInputStyle: {
            color: 'red',
            fontWeight: '500',
            minHeight: 10,
            paddingVertical: 10,
            paddingHorizontal: 5,
            width: '70%',
            textAlign: 'center',
            backgroundColor: 'pink',
          },
          textInputContainerStyle: {
            flex: 1,
            justifyContent: 'center',
            alignItems: 'center',
          },
          textInputProps: {
            placeholder: placeholder,
            placeholderTextColor: 'blue',
          },
        }}
        modalControls={{
          modalOptionsContainerStyle: {
            padding: 10,
          },
        }}
        listHeaderComponent={headerComponent}
        listComponentStyles={{
          listEmptyComponentStyle: {
            color: colors.error,
          },
          itemSeparatorStyle: {
            borderWidth: 0,
            opacity: 0,
          },
        }}
        checkboxControls={{
          checkboxStyle: {
            display: 'none',
          },
        }}
        listControls={{
          emptyListMessage: emptyListText ?? 'No record found',
        }}
        disabled={disabled}
      />
      {error && <ErrorText>{error.message}</ErrorText>}
      {helperText && <HelperText>{helperText}</HelperText>}
    </YStack>
  )
}

const $placeholderStyle: TextStyle = {
  color: colors.grey4,
  // paddingVertical: 6,
}

Mind you, I was using react-hook-form to manage my form, so you might have to remove that if that’s not what you use.

@jerrywonderr here:

import { useState } from "react";
import Modal from "react-native-modal";
import { ChevronDown } from "@tamagui/lucide-icons";
import { GetProps, ScrollView, Text, View, XStack } from "tamagui";

interface SelectProps<T> extends GetProps<typeof XStack> {
  label?: string;
  _label?: GetProps<typeof Text>;
  _wrapper?: GetProps<typeof View>;
  options: T[];
  value?: T;
  onValueChange?: (value: T) => void;
}

const Select = <T,>(props: SelectProps<T>) => {
  const {
    label,
    _label = {},
    _wrapper = {},
    options,
    value,
    onValueChange,
    disabled,
    ...rest
  } = props;

  const [open, setOpen] = useState<boolean>(false);
  const [val, setVal] = useState<T>("" as T);

  const handleValueChange = (v: T) => {
    setVal(v);
    onValueChange && onValueChange(v);
    setOpen(false);
  };

  const handleClose = () => {
    setOpen(false);
  };

  return (
    <View {..._wrapper}>
      {!!label && (
        <Text fontSize="$4" mx="$2" mb="$1" {..._label}>
          {label}
        </Text>
      )}

      <XStack
        ai="center"
        h="$4"
        bg="$gray2"
        minWidth="$5"
        bw={1}
        boc="$gray7"
        br="$4"
        px={14}
        o={disabled ? 0.5 : 1}
        pressStyle={{ o: 0.75 }}
        onPress={() => setOpen(true)}
        disabled={disabled}
        {...rest}
      >
        <Text f={1} fontSize="$4">
          {value ?? val}
        </Text>
        <ChevronDown size="$1" color="$gray10" />
      </XStack>

      <Modal
        isVisible={open}
        onBackdropPress={handleClose}
        onBackButtonPress={handleClose}
        style={{ paddingVertical: 32 }}
      >
        <View w="100%" bg="white" br="$4">
          {!!label && (
            <View p="$4">
              <Text fontSize="$5">{label}</Text>
            </View>
          )}

          <ScrollView
            contentContainerStyle={{ flexGrow: 1, padding: 16, paddingTop: 0 }}
          >
            {options.map((option, i) => {
              return (
                <View
                  key={`option-${i}`}
                  py="$2"
                  pressStyle={{ o: 0.75 }}
                  onPress={() => handleValueChange(option)}
                >
                  <Text>{option}</Text>
                </View>
              );
            })}
          </ScrollView>
        </View>
      </Modal>
    </View>
  );
};

export default Select;

it looks like this: image

it’s ugly lol but it gets the job done

Thx for this component code, this helped me a lot.

Makes sense actually. This should work for me also. Thanks. @coycoylaniba

@jerrywonderr here:

import { useState } from "react";
import Modal from "react-native-modal";
import { ChevronDown } from "@tamagui/lucide-icons";
import { GetProps, ScrollView, Text, View, XStack } from "tamagui";

interface SelectProps<T> extends GetProps<typeof XStack> {
  label?: string;
  _label?: GetProps<typeof Text>;
  _wrapper?: GetProps<typeof View>;
  options: T[];
  value?: T;
  onValueChange?: (value: T) => void;
}

const Select = <T,>(props: SelectProps<T>) => {
  const {
    label,
    _label = {},
    _wrapper = {},
    options,
    value,
    onValueChange,
    disabled,
    ...rest
  } = props;

  const [open, setOpen] = useState<boolean>(false);
  const [val, setVal] = useState<T>("" as T);

  const handleValueChange = (v: T) => {
    setVal(v);
    onValueChange && onValueChange(v);
    setOpen(false);
  };

  const handleClose = () => {
    setOpen(false);
  };

  return (
    <View {..._wrapper}>
      {!!label && (
        <Text fontSize="$4" mx="$2" mb="$1" {..._label}>
          {label}
        </Text>
      )}

      <XStack
        ai="center"
        h="$4"
        bg="$gray2"
        minWidth="$5"
        bw={1}
        boc="$gray7"
        br="$4"
        px={14}
        o={disabled ? 0.5 : 1}
        pressStyle={{ o: 0.75 }}
        onPress={() => setOpen(true)}
        disabled={disabled}
        {...rest}
      >
        <Text f={1} fontSize="$4">
          {value ?? val}
        </Text>
        <ChevronDown size="$1" color="$gray10" />
      </XStack>

      <Modal
        isVisible={open}
        onBackdropPress={handleClose}
        onBackButtonPress={handleClose}
        style={{ paddingVertical: 32 }}
      >
        <View w="100%" bg="white" br="$4">
          {!!label && (
            <View p="$4">
              <Text fontSize="$5">{label}</Text>
            </View>
          )}

          <ScrollView
            contentContainerStyle={{ flexGrow: 1, padding: 16, paddingTop: 0 }}
          >
            {options.map((option, i) => {
              return (
                <View
                  key={`option-${i}`}
                  py="$2"
                  pressStyle={{ o: 0.75 }}
                  onPress={() => handleValueChange(option)}
                >
                  <Text>{option}</Text>
                </View>
              );
            })}
          </ScrollView>
        </View>
      </Modal>
    </View>
  );
};

export default Select;

it looks like this: image

it’s ugly lol but it gets the job done

I’m facing this same issue currently. I’m testing my app on vivo2015 via expo go.

Any workaround for this yet? How did you go about this? @coycoylaniba