Guest User

Untitled

a guest
Mar 31st, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. import * as React from 'react';
  2. import VirtualizedSelect from 'react-virtualized-select';
  3. import { withFormik } from 'formik';
  4. import Yup from 'yup';
  5.  
  6. import 'react-select/dist/react-select.css';
  7. import 'react-virtualized/styles.css';
  8. import 'react-virtualized-select/styles.css';
  9. import { Datasource } from '../../../Model/Datasource';
  10. import { If } from '../../Utils/Helper/Helper';
  11.  
  12. const dataSourceType = [
  13. { label: 'MOM', value: 'MOM' },
  14. { label: 'MES', value: 'MES' },
  15. ];
  16.  
  17. interface Props {
  18. initialData: Datasource;
  19. submitHandler: (datasource: Datasource) => void;
  20. }
  21.  
  22. const NewDatasourceForm = (props) => {
  23. const {
  24. values,
  25. touched,
  26. errors,
  27. isSubmitting,
  28. setFieldValue,
  29. handleSubmit,
  30. handleChange,
  31. handleBlur
  32. } = props;
  33.  
  34. const _handleSelect = (selectDSChoice) => {
  35. try {
  36. setFieldValue('dataSourceType', selectDSChoice.value);
  37. } catch (error) {
  38. // tslint:disable-next-line:no-console
  39. console.error(error);
  40. }
  41.  
  42. };
  43.  
  44. return(
  45. <form className="needs-validation was-validated p-5" onSubmit={handleSubmit}>
  46.  
  47. <div className="form-group">
  48. <label>Data source type</label>
  49. <VirtualizedSelect
  50. name="imaginaryThingId"
  51. value={values.dataSourceType}
  52. options={dataSourceType}
  53. onChange={_handleSelect}
  54. />
  55. </div>
  56.  
  57. <If cond={values.dataSourceType !== ''}>
  58. <div className="form-group">
  59. <label>Name</label>
  60. <input
  61. className={`form-control`}
  62. name="name"
  63. type="text"
  64. value={values.name}
  65. onChange={handleChange}
  66. onBlur={handleBlur}
  67. />
  68. {errors.name && touched.name && <div className="invalid-feedback">{errors.name}</div>}
  69. </div>
  70.  
  71. <div className="form-group">
  72. <label>Url</label>
  73. <input
  74. className={`form-control`}
  75. name="url"
  76. type="text"
  77. value={values.url}
  78. onChange={handleChange}
  79. onBlur={handleBlur}
  80. />
  81. {errors.url && touched.url && <div className="invalid-feedback">{errors.url}</div>}
  82. </div>
  83.  
  84. <div className="form-group">
  85. <label>Username</label>
  86. <input
  87. className={`form-control`}
  88. name="username"
  89. type="text"
  90. value={values.username}
  91. onChange={handleChange}
  92. onBlur={handleBlur}
  93. />
  94. {errors.username && touched.username && <div className="invalid-feedback">{errors.username}</div>}
  95. </div>
  96.  
  97. <div className="form-group">
  98. <label>Password</label>
  99. <input
  100. className={`form-control`}
  101. name="password"
  102. type="text"
  103. value={values.password}
  104. onChange={handleChange}
  105. onBlur={handleBlur}
  106. />
  107. {errors.password && touched.password && <div className="invalid-feedback">{errors.password}</div>}
  108. </div>
  109. </If>
  110.  
  111. <button type="submit" className="btn btn-outline-primary" disabled={isSubmitting}>
  112. {isSubmitting ? 'Please wait' : 'Submit'}
  113. </button>
  114. </form>
  115. );
  116. };
  117.  
  118. export default withFormik({
  119. mapPropsToValues: (props: Props) => ({
  120. id: props.initialData.id,
  121. username: props.initialData.username,
  122. password: props.initialData.password,
  123. url: props.initialData.url,
  124. dataSourceType: props.initialData.dataSourceType,
  125. }),
  126.  
  127. validationSchema: Yup.object().shape({
  128. username: Yup.string()
  129. .required('Username is required!'),
  130. dataSourceType: Yup.string()
  131. .required('dataSourceType is required!'),
  132. }),
  133.  
  134. handleSubmit: (values, { setSubmitting }) => {
  135. // submit them do the parent component
  136. alert(JSON.stringify(values, null, 2));
  137. },
  138. })(NewDatasourceForm);
Add Comment
Please, Sign In to add comment