0.0.1 • Published 4 years ago

react-native-autotags v0.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

This package is a quick fix for a project. All rights reserved to Andreas Raduege. Github: https://github.com/Eldjotnar

react-native-autocomplete-tags

A quick and easy solutions for projects that need an input with both autocomplete and tags

Features

  • custom tag and suggestions
  • fully style-able
  • extractors for tags and for suggestions
  • easy to integrate and use
  • controlled text input

Installation

yarn add react-native-autocomplete-tags

or

npm install react-native-autocomplete-tags --save

Dependency

Requires RN >= 0.59

Useage

Also see the demo projects

const suggestions = [
  { name: "Boris Yeltsin", email: "boris.yeltsin@abc.com" },
  { name: "Tom Boboby", email: "tom.boboy@abc.com" }
];

const Demo = () => {
  const [text, setText] = useState("");
  const [tags, setTags] = useState([
    { name: "Fred Hendriks", email: "fred.hendricks@abc.com" }
  ]);

  const onChangeText = text => {
    setText(text);

    const lastTyped = text.charAt(text.length - 1);
    const parseWhen = [",", " ", ";", "\n"];

    if (parseWhen.indexOf(lastTyped) > -1) {
      setTags(tags => [...tags, { name: text, email: text }]);
      setText("");
    }
  };

  const handleSuggestionPress = suggestion => {
    setText("");
    setTags(tags => [...tags, suggestion]);
  };

  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.rowContainer}>
        <Text>To: </Text>
        <AutocompleteTags
          tags={tags}
          labelExtractor={item => item.name}
          text={text}
          onChangeText={onChangeText}
          onChangeTags={tags => setTags(tags)}
          suggestions={suggestions}
          suggestionExtractor={item => item.email}
          onSuggestionPress={handleSuggestionPress}
        />
      </View>
    </SafeAreaView>
  );
};

Props

ProptypeDescriptionrequireddefault
tagsarray of anyThe current tags to be renderedtrue
labelExtractorfunctionDetermines what property of tags is displayedtrue
textstringvalue of TextInputtrue
onChangeTextfunctioncalled when text changes, should also handle tag creationtrue
onChangeTagsfunctioncalled when tags change (i.e. by deleting), should just write tagstrue
minInputWidthnumberminimum width of TextInput before jumping to new linefalse100
suggestionsarray of anyAll possible suggestionsfalse[]
suggestionExtractorfunctiondetermines which property of suggestions is displayedfalsesame as labelExtractor
onSuggestionPressfunctioncalled when suggestion is pressedfalsenull
onTagPressfunctioncalled when tag is pressedfalsenull
renderSuggestionfunctionrenders a custom suggestion itemfalsenull
renderTagfunctionrenders a custom tagfalsenull
filterSuggestionsfunctionfilters suggestions based on text (receives text as parameter)falsenull
inputPropsTextInput propsany additional props for TextInputfalsenull
flatListPropsFlatList propsany additional props for FlatListfalsenull

Style Props

No style props are required.

PropDescription
containerStyleThe outmost container which contains the TextInput and the FlatList of suggestions
tagContainerStyleContainer for the tags and the TextInput
inputContainerStyleContainer around the TextInput
inputStyleApplied to the TextInput directly
tagStyleApplied to each tag
tagTextStyleApplied to the tag label
suggestionStyleApplied to each suggestion
suggestionContainerStyleApplied to the FlatList which renders suggestions
suggestionTextStyleApplied to the suggestion label

Contributing

PRs and issues welcome!