|
| 1 | +import React, { JSX } from 'react'; |
| 2 | +import { StyleSheet, Text, View } from 'react-native'; |
| 3 | +import DropDownPicker, { ItemType } from 'react-native-dropdown-picker'; |
| 4 | +import JavascriptClassExample from './example-src-files/javascript-class-example'; |
| 5 | +import JavascriptFunctionExample from './example-src-files/javascript-function-example'; |
| 6 | +import TypescriptClassExample from './example-src-files/typescript-class-example'; |
| 7 | +import TypescriptFunctionExample from './example-src-files/typescript-function-example'; |
| 8 | + |
| 9 | +enum ExampleComponent { |
| 10 | + JavaScriptClassSingleValue, |
| 11 | + JavaScriptClassMultiValue, |
| 12 | + JavaScriptFunctionSingleValue, |
| 13 | + JavaScriptFunctionMultiValue, |
| 14 | + TypeScriptClassSingleValue, |
| 15 | + TypeScriptClassMultiValue, |
| 16 | + TypeScriptFunctionSingleValue, |
| 17 | + TypeScriptFunctionMultiValue, |
| 18 | +} |
| 19 | + |
| 20 | +const styles = StyleSheet.create({ |
| 21 | + container: { |
| 22 | + flex: 1, |
| 23 | + // backgroundColor: "#fff", |
| 24 | + // alignItems: "center", |
| 25 | + // justifyContent: "center", |
| 26 | + flexDirection: 'column', |
| 27 | + margin: 3, |
| 28 | + marginTop: 20, |
| 29 | + padding: 3, |
| 30 | + }, |
| 31 | +}); |
| 32 | + |
| 33 | +const EXAMPLE_COMPONENT_ITEMS: Array<ItemType<ExampleComponent>> = [ |
| 34 | + { |
| 35 | + label: 'JavaScript; class component; single-item', |
| 36 | + value: ExampleComponent.JavaScriptClassSingleValue, |
| 37 | + }, |
| 38 | + { |
| 39 | + label: 'JavaScript; class component; multiple-item', |
| 40 | + value: ExampleComponent.JavaScriptClassMultiValue, |
| 41 | + }, |
| 42 | + { |
| 43 | + label: 'JavaScript; function component; single-item', |
| 44 | + value: ExampleComponent.JavaScriptFunctionSingleValue, |
| 45 | + }, |
| 46 | + { |
| 47 | + label: 'JavaScript; function component; multiple-item', |
| 48 | + value: ExampleComponent.JavaScriptFunctionMultiValue, |
| 49 | + }, |
| 50 | + { |
| 51 | + label: 'TypeScript; class component; single-item', |
| 52 | + value: ExampleComponent.TypeScriptClassSingleValue, |
| 53 | + }, |
| 54 | + { |
| 55 | + label: 'TypeScript; class component; multiple-item', |
| 56 | + value: ExampleComponent.TypeScriptClassMultiValue, |
| 57 | + }, |
| 58 | + { |
| 59 | + label: 'TypeScript; function component; single-item', |
| 60 | + value: ExampleComponent.TypeScriptFunctionSingleValue, |
| 61 | + }, |
| 62 | + { |
| 63 | + label: 'TypeScript; function component; multiple-item', |
| 64 | + value: ExampleComponent.TypeScriptFunctionMultiValue, |
| 65 | + }, |
| 66 | +]; |
| 67 | + |
| 68 | +type Props = Record<string, never>; |
| 69 | +type State = { |
| 70 | + currentExample: ExampleComponent; |
| 71 | + examplePickerOpen: boolean; |
| 72 | + exampleComponents: Array<ItemType<ExampleComponent>>; |
| 73 | +}; |
| 74 | + |
| 75 | +export default class App extends React.Component<Props, State> { |
| 76 | + constructor(props: Readonly<Props>) { |
| 77 | + super(props); |
| 78 | + this.state = { |
| 79 | + currentExample: ExampleComponent.JavaScriptClassSingleValue, |
| 80 | + exampleComponents: EXAMPLE_COMPONENT_ITEMS, |
| 81 | + examplePickerOpen: false, |
| 82 | + }; |
| 83 | + |
| 84 | + this.setOpen = this.setOpen.bind(this); |
| 85 | + this.setCurrentExample = this.setCurrentExample.bind(this); |
| 86 | + } |
| 87 | + |
| 88 | + private static getExample(egComponent: ExampleComponent): JSX.Element { |
| 89 | + switch (egComponent) { |
| 90 | + case ExampleComponent.JavaScriptClassSingleValue: |
| 91 | + return <JavascriptClassExample multiple={false} />; |
| 92 | + case ExampleComponent.JavaScriptClassMultiValue: |
| 93 | + return <JavascriptClassExample multiple />; |
| 94 | + case ExampleComponent.JavaScriptFunctionSingleValue: |
| 95 | + return <JavascriptFunctionExample multiple={false} />; |
| 96 | + case ExampleComponent.JavaScriptFunctionMultiValue: |
| 97 | + return <JavascriptFunctionExample multiple />; |
| 98 | + case ExampleComponent.TypeScriptClassSingleValue: |
| 99 | + return <TypescriptClassExample multiple={false} />; |
| 100 | + case ExampleComponent.TypeScriptClassMultiValue: |
| 101 | + return <TypescriptClassExample multiple />; |
| 102 | + case ExampleComponent.TypeScriptFunctionSingleValue: |
| 103 | + return <TypescriptFunctionExample multiple={false} />; |
| 104 | + case ExampleComponent.TypeScriptFunctionMultiValue: |
| 105 | + return <TypescriptFunctionExample multiple />; |
| 106 | + default: |
| 107 | + throw new Error( |
| 108 | + "couldn't match example component in getExample() in App.tsx. egComponent was: ", |
| 109 | + egComponent, |
| 110 | + ); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + setOpen(examplePickerOpen: boolean): void { |
| 115 | + this.setState({ examplePickerOpen }); |
| 116 | + } |
| 117 | + |
| 118 | + setCurrentExample( |
| 119 | + callback: (prevState: ExampleComponent | null) => ExampleComponent | null, |
| 120 | + ): void { |
| 121 | + this.setState((state: Readonly<State>) => ({ |
| 122 | + currentExample: callback(state.currentExample), |
| 123 | + })); |
| 124 | + } |
| 125 | + |
| 126 | + // todo: fix picker items being under text |
| 127 | + |
| 128 | + render(): JSX.Element { |
| 129 | + const { currentExample, examplePickerOpen, exampleComponents } = this.state; |
| 130 | + |
| 131 | + return ( |
| 132 | + <View style={styles.container}> |
| 133 | + <View style={{ flex: 1 }}> |
| 134 | + <View style={{ flex: 1 }}> |
| 135 | + <Text>Choose example:</Text> |
| 136 | + </View> |
| 137 | + |
| 138 | + <View style={{ flex: 1 }}> |
| 139 | + <DropDownPicker |
| 140 | + setValue={this.setCurrentExample} |
| 141 | + value={currentExample} |
| 142 | + items={exampleComponents} |
| 143 | + open={examplePickerOpen} |
| 144 | + setOpen={this.setOpen} |
| 145 | + /> |
| 146 | + </View> |
| 147 | + </View> |
| 148 | + |
| 149 | + <View style={{ flex: 3 }}> |
| 150 | + <View style={{ flex: 1 }}> |
| 151 | + <Text>Example:</Text> |
| 152 | + </View> |
| 153 | + |
| 154 | + {App.getExample(currentExample)} |
| 155 | + </View> |
| 156 | + </View> |
| 157 | + ); |
| 158 | + } |
| 159 | +} |
0 commit comments