1.18.0 • Published 4 years ago

react-native-material-selectize v1.18.0

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

react-native-material-selectize npm version

A React Native component inspired to Selectize which follows Material Design guidelines.

Examples

Working examples are provided within the project. After downloading it, navigate to any subfolder of examples and run either "npm install" or "yarn".

preview

Features

  • Type definitions with Typescript
  • Material design guidelines compliance
  • Consistent look and feel on iOS and Android
  • State transitions (normal, focused and errored)
  • Highly customizable styles and props of inner TextInput component
  • Pure javascript implementation
  • Minimal setup (no props required)

Installation

The easiest way to install it is via npm. You need to have node.js installed.

npm install react-native-material-selectize --save

Alternatively you can use yarn.

yarn add react-native-material-selectize

Properties

namedescriptiontypedefault
chipStyleStyle of Chip componentStyle-
chipIconStyleStyle of Chip close iconStyle-
containerStyleStyle of the main container componentStyle-
inputContainerStyleStyle of the input container componentStyle-
labelStyleStyle of the label text componentStyle-
listStyleStyle of the dropdown list containerStyle-
listRowStyleStyle of each row within the listStyle-
itemIdUnique identifier for each item objectStringid
itemsArray of objects to populate the list More infoArray[]
selectedItemsArray of objects to render chips More infoArray[]
labelText field label textString-
errorText field error textString-
errorColorText field color for errored stateStringrgb(213, 0, 0)
tintColorText field accent color (focus)Stringrgb(0, 145, 234)
baseColorText field base color (blur)Stringrgba(0, 0, 0, .38)
showItemsDefine when dropdown items should be seen'always', 'never', 'onFocus', 'onTyping''onFocus'
autoReflowDefine whether the typed text reflows More infoBooleantrue
trimOnSubmitTrim text before creating the ChipBooleantrue
renderRowReturn the rendered row of the listFunctionrenderRow default
renderChipReturn the rendered Chip componentFunctionrenderChip default
textInputPropsTextInput props More infoObject-
middleComponentA custom component to render between the text input and the item listComponent-
filterOnKeyThe key to filter on. If not set, the provided itemId is usedString-
onChangeSelectedItemsCalled when selectedItems changesFunction(selectedItems) => {}

items

In general, this array should be consisting of objects having at least one property itemId which value is unique for each object. An example of valid input for itemId = 'id' would be:

[
  { id: 'john', email: 'john@gmail.com' },
  { id: 'doe', email: 'doe@gmail.com' }
]

An example of invalid input for itemId = 'anotherId' would be:

[
  { anotherId: '1' },
  { anotherId: '1' },
  { id: '2' }
]

It is invalid for two reasons: itemId doesn't appear in every object and there are two objects having same itemId.

If you just need to render a single value for each row in the list and for each Chip, 'items' prop can be represented as simple array of strings, but only if each value is unique:

['john', 'doe']

selectedItems

Same format rules of items

renderRow default

(id, onPress, item, style) => (
  <TouchableOpacity
    activeOpacity={0.6}
    key={id}
    onPress={onPress}
    style={[styles.listRow, style]}>
    <Text style={{ color: 'rgba(0, 0, 0, 0.87)' }}>{id}</Text>
  </TouchableOpacity>
);
paramdescription
id'itemId' of the row-indexed object contained in 'items', used as 'key'
onPressonPress handler that turns the selected row into a Chip
itemrow-indexed object contained in 'items'
stylesreference to 'listRowStyle' prop

renderChip default

(id, onClose, item, style, iconStyle) => (
  <Chip
    key={id}
    iconStyle={iconStyle}
    onClose={onClose}
    text={id}
    style={style}
  />
);
paramdescription
idunique key for the Chip, used as 'key'
onCloseonClose handler for removing the Chip
itemrow-indexed object contained in 'items' if the Chip has been created clicking on a row of the list, OR the value of the TextInput otherwise
stylesreference to 'chipStyle' prop
iconStylereference to 'chipIconStyle' prop

textInputProps

Object which contains the props of the inner TextInput component used for filtering the results from the list generated by 'items' prop. Some props are set internally and cannot be changed.

ignored proptypedefault
disableFullscreenUIBooleantrue
underlineColorAndroidStringtransparent
valueStringset internally
selectionColorString'tintColor' prop

Other props have different default to better cope with the component requirements, but they can be overridden.

different default proptypedefault
autoCapitalizeStringnone
autoCorrectBooleanfalse
blurOnSubmitBooleanfalse

The following handler props are wrapped internally and will be invoked if defined. They are called with different parameters than their original counterparts.

different parameters proptypeparameters
onChangeTextFunction(text) => {}
onSubmitEditingFunction(text) => {}
onFocusFunction(text) => {}
onBlurFunction(text) => {}

The following handler props have a special behavior: returning false they prevent the related event dispatch.

prevent event proptype
onChangeTextFunction
onSubmitEditingFunction

Methods

namedescriptionreturns
focus()Acquire focus-
blur()Release focus-
submit()Transform current text into Chip-
getValue()Get current value of the inner TextInputString
getSelectedItems()Get normalised items transformed into Chips{ result: string[]; entities: { item: { key: string: Item } }; }
clearSelectedItems()Remove all the Chips-

Caveats

  • When Selectize is a child of a ScrollView, the ScrollView's keyboardShouldPersistTaps prop must be set to either handled or always for dropdown completion to work properly.

FAQ

How to make list go over all other elements vs pushing them down?

By default the list of rendered items pushes the other elements down. If you want to have an absolute positioned list, it can be achieved in the following way:

  • specify a zIndex for the container bigger than its siblings, usually 1 is just ok:
containerStyle: {
  zIndex: 1;
}
  • specify an absolute positioning for listStyle:
listStyle: {
  position: 'absolute';
}

Related issue: https://github.com/raynor85/react-native-material-selectize/issues/19

Licence

react-native-material-selectize is MIT licensed.

1.18.0

4 years ago

1.17.0

4 years ago

1.16.1

4 years ago

1.16.0

5 years ago

1.15.0

5 years ago

1.14.2

5 years ago

1.14.0

5 years ago

1.13.0

6 years ago

1.12.0

6 years ago

1.11.0

6 years ago

1.10.0

6 years ago

1.9.0

6 years ago

1.8.1

6 years ago

1.8.0

6 years ago

1.7.0

6 years ago

1.6.0

7 years ago

1.5.1

7 years ago

1.5.0

7 years ago

1.4.2

7 years ago

1.4.1

7 years ago

1.4.0

7 years ago

1.3.1

7 years ago

1.2.0

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago