Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocomplete text input component #4587

Open
Sami-21 opened this issue Dec 28, 2024 · 3 comments
Open

Autocomplete text input component #4587

Sami-21 opened this issue Dec 28, 2024 · 3 comments

Comments

@Sami-21
Copy link

Sami-21 commented Dec 28, 2024

Is your feature request related to a problem? Please describe.
I have been using native paper for different projects and I came across a problem several times where I needed an autocomplete component, I tried 2 packages but I would love to have a component from the native paper so that my UI remains consistent

Describe the solution you'd like
I implemented an autocomplete component with the use of TextInput + Menu component from native paper

interface Props<T extends Record<string, any>> {
  theme?: AppTheme;
  value: {
    origValue: string | undefined;
  };
  label: string;
  data: T[];
  displayKey: keyof T;
  containerStyle?: StyleProp<ViewStyle>;
  onChange: (text: string) => void;
  leadingIcon?: IconSource;
  trailingIcon?: IconSource;
  textInputstyle?: StyleProp<TextStyle>;
  menuStyle?: StyleProp<ViewStyle>;
  textInputRight?: ReactNode;
  textInputLeft?: ReactNode;
}

const Autocomplete = <T extends Record<string, any>>({
  theme = lightTheme,
  value: propValue,
  label,
  data,
  displayKey,
  containerStyle = {},
  onChange: originOnChange,
  textInputstyle = {},
  leadingIcon = "",
  trailingIcon = "",
  menuStyle = {},
  textInputRight = null,
  textInputLeft = null,
}: Props<T>) => {
  const [value, setValue] = useState<string | undefined>(propValue.origValue);
  const [menuVisible, setMenuVisible] = useState<boolean>(false);

  return (
    <View
      style={
        containerStyle,
      }
    >
      <TextInput
        theme={theme}
        mode="outlined"
        onFocus={() => {
          if (value || value?.length === 0) {
            setMenuVisible(true);
          }
        }}
        outlineStyle={
          menuVisible
            ? {
                borderBottomRightRadius: 0,
                borderBottomLeftRadius: 0,
              }
            : {}
        }
        onBlur={() => setMenuVisible(false)}
        label={label}
        {...(textInputRight ? { right: textInputRight } : {})}
        {...(textInputLeft ? { left: textInputLeft } : {})}
        style={textInputstyle}
        onChangeText={(text) => {
          originOnChange(text);
          setMenuVisible(true);
          setValue(text);
        }}
        value={value}
      />
      {menuVisible && (
        <View
          style={{
            flex: 1,
            width: "100%",
            backgroundColor: "white",
            borderWidth: 1,
            borderColor: theme.colors.primary,
            borderBottomRightRadius: 4,
            borderBottomLeftRadius: 4,
            flexDirection: "column",
            position: "absolute",
            top: "100%",
            left: 0,
            zIndex: 10,
          }}
        >
          {data.length === 0 && (
            <Text style={{ padding: 10 }}>
              <Text style={{ textAlign: "center" }}>
                No result found
              </Text>
            </Text>
          )}
          {data.map((item, index) => (
            <Menu.Item
              key={index}
              style={ menuStyle}
              leadingIcon={leadingIcon}
              trailingIcon={trailingIcon}
              onPress={() => {
                const selectedValue = String(item[displayKey]);
                console.log(selectedValue);

                setValue(selectedValue);
                setMenuVisible(false);
                originOnChange(selectedValue);
              }}
              title={String(item[displayKey])}
            />
          ))}
        </View>
      )}
    </View>
  );
};

Describe alternatives you've considered
I have tried two alternatives:
react-native-autocomplete-dropdown and react-native-autocomplete-input
I dropped those packages out of fear since they were backed only by one contributor.

Additional context
N/A

@RichardLindhout
Copy link
Contributor

I once tried it and came really far but i came to conclusion we need a bottom sheet modal on mobile instead of a dropdown
https://github.com/web-ridge/react-native-paper-autocomplete

@RichardLindhout
Copy link
Contributor

It's really hard though to get it working on web + native with great performance and inside scroll containers

@RichardLindhout
Copy link
Contributor

The package needs to get a lot of work and bugfixes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants