Form.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React from 'react';
  2. import Module from '../lib/Module';
  3. import {StyleSheet ,TextInput, View, Button , Text } from 'react-native';
  4. import Types from '../lib/Types';
  5. const Inp = [
  6. {
  7. textContentType :"username",
  8. placeholder:"username"
  9. },
  10. {
  11. textContentType :"password",
  12. placeholder:"password"
  13. }]
  14. export default class FormComp extends Module {
  15. constructor(props) {
  16. super(props);
  17. this.state = {
  18. }
  19. this.submit = this.submit.bind(this)
  20. }
  21. submit(e){
  22. e.preventDefault()
  23. return this.state
  24. }
  25. display() {
  26. let {
  27. inputs, // number of inputs this must be Object
  28. inputsMargin,
  29. ButtonText,
  30. ButtonColor,
  31. backgroundColor,
  32. Formtitle
  33. } = this.props
  34. //Init
  35. if(!inputs) inputs = Inp;
  36. if(!inputsMargin) inputsMargin = 7
  37. let Inputs = Object.keys(inputs).map((key,indx) => {
  38. let state = {}
  39. return (<TextInput key={indx}
  40. style={[{height: 20, borderColor: 'gray', borderWidth: 1 , marginBottom:inputsMargin},StaticRules.text]}
  41. onChangeText={(text) => {
  42. state[indx] = text;
  43. this.setState(state)}}
  44. value={this.state.key}
  45. placeholder={inputs[key].placeholder}
  46. textContentType={inputs[key].textContentType}
  47. secureTextEntry={ inputs[key].textContentType === "password" ? true :false}
  48. />)
  49. })
  50. let fn = this.props.submit || this.submit
  51. return (
  52. <View style={[{backgroundColor:backgroundColor},StaticRules.container]}>
  53. {this.props.Formtitle ? (
  54. <Text>{this.props.Formtitle}</Text>
  55. ):(null)}
  56. {Inputs}
  57. <Button
  58. onPress = {(e) => fn(e)}
  59. title={this.props.ButtonText }
  60. color={this.props.ButtonColor}
  61. accessibilityLabel="Learn more about this purple button"
  62. />
  63. </View>)
  64. }
  65. }
  66. //Predefined Rules that the User cant change
  67. let StaticRules = StyleSheet.create({
  68. container:{
  69. padding:10
  70. },
  71. text:{
  72. fontSize:7
  73. }
  74. })
  75. FormComp.Inputs = {
  76. //inputsNum: new Types.Integer().require().default(2),
  77. /*inputs:[
  78. {
  79. textContentType :"username",
  80. placeholder:"username"
  81. },
  82. {
  83. textContentType :"password",
  84. placeholder:"password"
  85. },
  86. ],*/
  87. inputs:new Types.Object().require(),
  88. ButtonText:new Types.Text().require().default('Submit'),
  89. ButtonColor: new Types.Text().require().color().default('rgba(3, 218, 198, 1)'),
  90. backgroundColor: new Types.Text().require().color().default('rgba(0, 140, 186, 1)'),
  91. Formtitle: new Types.Text().require()
  92. }
  93. /*
  94. inputs, // number of inputs this must be Object
  95. inputsMargin,
  96. ButtonText,
  97. ButtonColor,
  98. backgroundColor,
  99. Formtitle
  100. */