ArrayEditor.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import React, { useState } from 'react';
  2. import {View, Text, TextInput, StyleSheet} from 'react-native';
  3. import { Icon } from 'react-native-elements';
  4. import TypeHandler from './TypeHandler'
  5. export default function ArrayEditor(props) {
  6. let [array, setArray] = useState(props.value || []);
  7. let [template, setTemplate] = useState(props.template || {});
  8. let Handler = TypeHandler(template);
  9. return (
  10. <View style={styles.container}>
  11. <View style={styles.row}>
  12. <Text style={styles.capFirst}>{props.title}</Text>
  13. <View>
  14. <Icon name={'plus-one'}
  15. // type = {}
  16. // color = {}
  17. onPress = {() => {
  18. array.push(null);
  19. setArray(array);
  20. props.onChange && props.onChange(array);
  21. }}/>
  22. </View>
  23. </View>
  24. {array.map((item, index) => {
  25. let editorProps = {
  26. title: undefined,
  27. onChange: (value) => {
  28. array[index] = value;
  29. setArray(array);
  30. props.onChange && props.onChange(array);
  31. },
  32. value: array[index]
  33. };
  34. return <View style={styles.item}>
  35. <View style={styles.row}>
  36. <Text style={styles.itemIndex}># {index}</Text>
  37. <Icon name={'delete-forever'}
  38. // type = {}
  39. // color = {}
  40. onPress = {() => {
  41. array.splice(index,1);
  42. setArray(array);
  43. props.onChange && props.onChange(array);
  44. }}/>
  45. </View>
  46. <Handler {...editorProps} key={array.length * 100 + index} />
  47. </View>
  48. })}
  49. </View>
  50. );
  51. }
  52. const styles = StyleSheet.create({
  53. container:{
  54. flexDirection: 'column'
  55. },
  56. row: {
  57. flexDirection: 'row',
  58. alignItems: 'baseline',
  59. justifyContent: 'space-between',
  60. },
  61. capFirst: {
  62. fontSize: 14,
  63. fontFamily: 'roboto-light',
  64. textTransform: 'capitalize'
  65. },
  66. defaultInput: {
  67. marginTop:6,
  68. borderWidth:1,
  69. backgroundColor: 'white',
  70. boxShadow: '0px 3px 6px #00000029',
  71. borderColor: '#FFFFFF66',
  72. borderRadius: 14
  73. },
  74. item: {
  75. paddingLeft: 8,
  76. borderLeftWidth: 1,
  77. borderColor: '#cecece',
  78. marginBottom: 4,
  79. marginTop: 4
  80. },
  81. itemIndex: {
  82. fontSize: 12,
  83. opacity: 0.4
  84. }
  85. })