Guest User

Untitled

a guest
Mar 31st, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. import * as React from 'react';
  2. import { Form, Field, FormikProps, withFormik, getIn } from 'formik';
  3. import { object, string } from 'yup';
  4. import { Datasource } from '../../../Model/Datasource';
  5. import { Col, Button, FormGroup, Label, } from 'reactstrap';
  6.  
  7. interface ErrorMessageProps {
  8. name: string;
  9. }
  10.  
  11. const ErrorMessage = ({name}: ErrorMessageProps) => (
  12. <Field
  13. name={name}
  14. render={({form}) => {
  15. const error = getIn(form.errors, name);
  16. const touch = getIn(form.touched, name);
  17. return touch && error ? error : null;
  18. }}
  19. />
  20. );
  21.  
  22. const InnerForm = (props: FormikProps<Datasource>) => {
  23. return(
  24. <Form>
  25. <FormGroup row={true}>
  26. <Label for="name" sm={2}>Name</Label>
  27. <Col sm={10}>
  28. <Field name={`name`}/>
  29. <ErrorMessage name={`name`}/>
  30. </Col>
  31. </FormGroup>
  32.  
  33. <FormGroup row={true}>
  34. <Label for="url" sm={2}>URL</Label>
  35. <Col sm={10}>
  36. <Field name={`url`}/>
  37. <ErrorMessage name={`url`}/>
  38. </Col>
  39. </FormGroup>
  40.  
  41. <FormGroup row={true}>
  42. <Label for="username" sm={2}>Username</Label>
  43. <Col sm={10}>
  44. <Field name={`username`}/>
  45. <ErrorMessage name={`username`}/>
  46. </Col>
  47. </FormGroup>
  48.  
  49. <FormGroup row={true}>
  50. <Label for="password" sm={2}>Password</Label>
  51. <Col sm={10}>
  52. <Field name={`password`}/>
  53. <ErrorMessage name={`password`}/>
  54. </Col>
  55. </FormGroup>
  56.  
  57. <FormGroup row={true}>
  58. <Label for="description" sm={2}>Description</Label>
  59. <Col sm={10}>
  60. <Field name={`description`}/>
  61. <ErrorMessage name={`description`}/>
  62. </Col>
  63. </FormGroup>
  64.  
  65. <div>
  66. <Button type="submit">Submit</Button>
  67. </div>
  68. </Form>);
  69. };
  70.  
  71. const NewDatasourceForm = withFormik({
  72. mapPropsToValues: (initialData): Datasource => ({
  73. id: '',
  74. name: '',
  75. url: '' ,
  76. username: '' ,
  77. password: '' ,
  78. description: ''
  79. }),
  80. validationSchema: object().shape({
  81. name: string().required('name required'),
  82. url: string().required('url required'),
  83. username: string().required('username required'),
  84. password: string().required('password required')
  85. }),
  86. handleSubmit: (values) => {
  87. alert(JSON.stringify(values));
  88. },
  89. })(InnerForm);
  90.  
  91. export default NewDatasourceForm;
Add Comment
Please, Sign In to add comment