Form.js 2.5 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(!inputsMargin) inputsMargin = 7
  36. let Inputs = Object.keys(inputs).map((key,indx) => {
  37. let state = {}
  38. return (<TextInput key={indx}
  39. style={[{height: 20, borderColor: 'gray', borderWidth: 1 , marginBottom:inputsMargin},StaticRules.text]}
  40. onChangeText={(text) => {
  41. state[indx] = text;
  42. this.setState(state)}}
  43. value={this.state.key}
  44. placeholder={inputs[key].placeholder}
  45. textContentType={inputs[key].textContentType}
  46. secureTextEntry={ inputs[key].textContentType === "password" ? true :false}
  47. />)
  48. })
  49. let fn = this.props.submit || this.submit
  50. return (
  51. <View style={[{backgroundColor:backgroundColor},StaticRules.container]}>
  52. {this.props.Formtitle ? (
  53. <Text>{this.props.Formtitle}</Text>
  54. ):(null)}
  55. {Inputs}
  56. <Button
  57. onPress = {(e) => fn(e)}
  58. title={this.props.ButtonText }
  59. color={this.props.ButtonColor}
  60. accessibilityLabel="Learn more about this purple button"
  61. />
  62. </View>)
  63. }
  64. }
  65. //Predefined Rules that the User cant change
  66. let StaticRules = StyleSheet.create({
  67. container:{
  68. padding:10
  69. },
  70. text:{
  71. fontSize:7
  72. }
  73. })
  74. FormComp.Inputs = {
  75. //inputsNum: new Types.Integer().require().default(2),
  76. /*inputs:[
  77. {
  78. textContentType :"username",
  79. placeholder:"username"
  80. },
  81. {
  82. textContentType :"password",
  83. placeholder:"password"
  84. },
  85. ],*/
  86. inputs:new Types.Object().require().default(Inp),
  87. ButtonText:new Types.Text().require().default('Submit'),
  88. ButtonColor: new Types.Text().require().color().default('rgba(3, 218, 198, 1)'),
  89. backgroundColor: new Types.Text().require().color().default('rgba(0, 140, 186, 1)'),
  90. Formtitle: new Types.Text().require()
  91. }
  92. /*
  93. inputs, // number of inputs this must be Object
  94. inputsMargin,
  95. ButtonText,
  96. ButtonColor,
  97. backgroundColor,
  98. Formtitle
  99. */