0.2.4 • Published 2 years ago

@pointotech/react-native-combobox v0.2.4

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

Pointotech React Native Combobox

Install

Yarn installation

yarn add @pointotech/react-native-combobox@latest

NPM installation

npm install @pointotech/react-native-combobox@latest

Create JavaScript wrapper for the native component

Put this code in a Combobox.tsx file in your project:

import { ComboboxDataRow } from "@pointotech/react-native-combobox/ComboboxDataRow"
import { ComboboxProps } from "@pointotech/react-native-combobox/ComboboxProps"
import React from "react"
import {
  requireNativeComponent,
  NativeModules,
  HostComponent,
  Platform,
} from "react-native"

const ComboboxImplementation = requireNativeComponent(
  Platform.OS === "android" ? "ComboboxImplementationAndroid" : "Combobox"
) as HostComponent<ComboboxProps>

const prepareSelectedItem = (
  selectedItem: string | ComboboxDataRow | null
): { text: string; isAvailable?: boolean } | null => {
  if (selectedItem === null) {
    return null
  } else {
    if (typeof selectedItem === "string") {
      return {
        text: selectedItem,
      }
    } else if (typeof selectedItem === "object") {
      if (selectedItem.text && typeof selectedItem.text === "string") {
        return {
          text: selectedItem.text,
          isAvailable: selectedItem.hasOwnProperty("isAvailable")
            ? !!selectedItem.isAvailable
            : undefined,
        }
      } else {
        throw new Error(`Selected item must have a "text" property.`)
      }
    } else {
      throw new Error(
        "Selected item must be an object or a string. Value received: " +
          selectedItem
      )
    }
  }
}

export function Combobox(props: ComboboxProps) {
  const { selectedItem, ...propsWithoutSelectedItem } = props
  return (
    <ComboboxImplementation
      selectedItem={prepareSelectedItem(selectedItem)}
      {...propsWithoutSelectedItem}
    />
  )
}

export const ComboboxManager = NativeModules.Combobox

Use

Import the Combobox component from your Combobox.tsx file, and use it in your app's UI:

import { Combobox } from "./src/Combobox"

export const App = () => {
  return (
    <Combobox
      style={{ height: 100 }}
      items={[
        "foo",
        "bar",
        "qux",
        { text: "hello world", isAvailable: true },
        "narf",
      ]}
      selectedItem="hello world"
      placeholder="Pick a nonsense, please! Or don't."
      onSelectedItemChanged={(event) => {
        if (!event) {
          throw new Error("onSelectedItemChanged event is null.")
        }
        if (!(event instanceof Object)) {
          throw new Error("onSelectedItemChanged event is not an object.")
        }
        if (event.hasOwnProperty("nativeEvent")) {
          const nativeEvent = (event as { nativeEvent: unknown }).nativeEvent

          if (nativeEvent.hasOwnProperty("selectedItem")) {
            const selectedItem = (nativeEvent as { selectedItem: unknown })
              .selectedItem

            if (selectedItem && typeof selectedItem === "object") {
              if (selectedItem.hasOwnProperty("text")) {
                console.log(
                  "onSelectedItemChanged text " +
                    (selectedItem as { text: unknown }).text
                )
              } else {
                throw new Error("onSelectedItemChanged text not defined")
              }

              if (selectedItem.hasOwnProperty("isAvailable")) {
                console.log(
                  "onSelectedItemChanged isAvailable " +
                    (selectedItem as { isAvailable: unknown }).isAvailable
                )
              } else {
                console.log("onSelectedItemChanged isAvailable not defined")
              }
            }
          } else {
            throw new Error(
              'onSelectedItemChanged native event has no "selectedItem" property.'
            )
          }
        } else {
          throw new Error(
            'onSelectedItemChanged event has no "nativeEvent" property.'
          )
        }
      }}
    />
  )
}
0.2.3

2 years ago

0.2.4

2 years ago

0.2.1

3 years ago

0.2.2

3 years ago

0.2.0

4 years ago

0.1.7

4 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.0

5 years ago

0.1.1

5 years ago

0.0.99

5 years ago

0.0.98

5 years ago

0.0.97

5 years ago

0.0.96

5 years ago

0.0.95

5 years ago

0.0.93

5 years ago

0.0.94

5 years ago

0.0.92

5 years ago

0.0.91

5 years ago

0.0.90

5 years ago

0.0.89

5 years ago

0.0.88

5 years ago

0.0.87

5 years ago

0.0.86

5 years ago

0.0.85

5 years ago

0.0.84

5 years ago

0.0.82

5 years ago

0.0.83

5 years ago

0.0.80

5 years ago

0.0.81

5 years ago

0.0.79

5 years ago

0.0.78

5 years ago

0.0.76

5 years ago

0.0.77

5 years ago

0.0.74

5 years ago

0.0.75

5 years ago

0.0.73

5 years ago

0.0.72

5 years ago

0.0.70

5 years ago

0.0.71

5 years ago

0.0.65

5 years ago

0.0.66

5 years ago

0.0.67

5 years ago

0.0.68

5 years ago

0.0.69

5 years ago

0.0.63

5 years ago

0.0.64

5 years ago

0.0.62

5 years ago

0.0.60

5 years ago

0.0.61

5 years ago

0.0.59

5 years ago

0.0.58

5 years ago

0.0.57

5 years ago

0.0.56

5 years ago

0.0.54

5 years ago

0.0.55

5 years ago

0.0.51

5 years ago

0.0.52

5 years ago

0.0.53

5 years ago

0.0.50

5 years ago

0.0.49

5 years ago

0.0.48

5 years ago

0.0.47

5 years ago

0.0.46

5 years ago

0.0.45

5 years ago

0.0.41

5 years ago

0.0.42

5 years ago

0.0.43

5 years ago

0.0.44

5 years ago

0.0.40

5 years ago

0.0.39

5 years ago

0.0.38

5 years ago

0.0.37

5 years ago

0.0.34

5 years ago

0.0.35

5 years ago

0.0.36

5 years ago

0.0.32

5 years ago

0.0.30

5 years ago

0.0.31

5 years ago

0.0.29

5 years ago

0.0.28

5 years ago

0.0.27

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.23

5 years ago

0.0.21

5 years ago

0.0.22

5 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.13

5 years ago

0.0.14

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago