Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import Form from 'react-bootstrap/Form';
  3. import { Formik } from 'formik';
  4. import Button from 'react-bootstrap/Button';
  5.  
  6.  
  7. class Alerts extends Component {
  8. constructor(props) {
  9. super(props);
  10. this.loadAlertData = this.loadAlertData.bind(this);
  11. this.state = {
  12. alertRecipient: {},
  13. alertId: '',
  14. alertVehicles: []
  15. }
  16. }
  17.  
  18. componentDidMount(){
  19. this.loadAlertData();
  20. }
  21.  
  22. loadAlertData(){
  23. // this will be an API call eventually, obviously.
  24. // if this is initialised in the constructor then everything works!
  25. this.setState( {
  26. alertRecipient: {
  27. name: 'Rob',
  28. simNumber: '0123456789',
  29. },
  30. alertId: 1,
  31. alertVehicles: [
  32. {id: 1, vrn: 'vehicle A', selected: true },
  33. {id: 2, vrn: 'vehicle B', selected: false },
  34. {id: 3, vrn: 'vehicle C', selected: true }
  35. ]
  36. })
  37. }
  38.  
  39. render() {
  40. const { alertRecipient, alertId, alertVehicles } = this.state;
  41. return (
  42. <>
  43. <Formik
  44. initialValues={{ alertRecipient, alertId, alertVehicles }}
  45. onSubmit={ values => {
  46. window.alert(JSON.stringify(values))
  47. }
  48. }
  49. render={({values, handleChange, handleSubmit}) => (
  50. <Form onSubmit={handleSubmit}>
  51. <Form.Label>Name</Form.Label>
  52. <Form.Control
  53. type="text"
  54. name="alertRecipient.name"
  55. value={values.alertRecipient.name}
  56. onChange={handleChange}
  57. />
  58. <Form.Label>Phone number</Form.Label>
  59. <Form.Control
  60. type="text"
  61. name="alertRecipient.simNumber"
  62. value={values.alertRecipient.simNumber}
  63. onChange={handleChange}
  64. >
  65. </Form.Control>
  66. <Form.Label>Vehicles</Form.Label>
  67. {
  68. //get an error if we just use alertVehicles.length here??
  69. values.alertVehicles.length === 0 ? null : alertVehicles.map((veh, index) => (
  70.  
  71. <Form.Check type="checkbox"
  72. key={veh.id}
  73. label={veh.vrn}
  74. name={`alertVehicles[${index}].selected`}
  75. checked={values.alertVehicles[index].selected}
  76. onChange={ handleChange }
  77. />
  78. ))
  79. }
  80. <Button type="submit">Save</Button>
  81. </Form>
  82. )
  83. }
  84. />
  85. </>
  86. )
  87. }
  88.  
  89. }
  90.  
  91. export default Alerts;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement