2
0

Form.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { useState } from 'react';
  2. import { IValidation } from '../components/customInput/Types';
  3. import { checkEmptyValid, getCheckResult } from '../utils/Validation';
  4. export interface IForm {
  5. key: string;
  6. value?: any;
  7. needCheck?: boolean;
  8. }
  9. interface IValidationInfo {
  10. // check general validation
  11. checkFormValid: Function;
  12. // check detail validation
  13. checkIsValid: Function;
  14. validation: {
  15. [key: string]: IValidationItem;
  16. };
  17. disabled: boolean;
  18. setDisabled: Function;
  19. resetValidation: (form: IForm[]) => void;
  20. }
  21. export interface IValidationItem {
  22. result: boolean;
  23. errText: string;
  24. }
  25. export interface ICheckValidParam {
  26. value: string;
  27. key: string;
  28. // one input can contains multiple rules
  29. rules: IValidation[];
  30. }
  31. export const useFormValidation = (form: IForm[]): IValidationInfo => {
  32. const initValidation = form
  33. .filter(f => f.needCheck)
  34. .reduce(
  35. (acc, cur) => ({
  36. ...acc,
  37. [cur.key]: {
  38. result:
  39. typeof cur.value === 'string' ? cur.value === '' : cur.value === 0,
  40. errText: '',
  41. },
  42. }),
  43. {}
  44. );
  45. // validation detail about form item
  46. const [validation, setValidation] = useState(initValidation);
  47. // overall validation result to control following actions
  48. const [disabled, setDisabled] = useState<boolean>(true);
  49. const checkIsValid = (param: ICheckValidParam): IValidationItem => {
  50. const { value, key, rules } = param;
  51. let validDetail = {
  52. result: false,
  53. errText: '',
  54. };
  55. for (let i = 0; i < rules.length; i++) {
  56. const rule = rules[i];
  57. const checkResult = getCheckResult({
  58. value,
  59. extraParam: rule.extraParam,
  60. rule: rule.rule,
  61. });
  62. if (!checkResult) {
  63. validDetail = {
  64. result: true,
  65. errText: rule.errorText || '',
  66. };
  67. break;
  68. }
  69. }
  70. const validInfo = {
  71. ...validation,
  72. [key]: validDetail,
  73. };
  74. const isOverallValid = Object.values(validInfo).every(
  75. v => !(v as IValidationItem).result
  76. );
  77. setDisabled(!isOverallValid);
  78. setValidation(validInfo);
  79. return validDetail;
  80. };
  81. const checkFormValid = (form: IForm[]): boolean => {
  82. const requireCheckItems = form.filter(f => f.needCheck);
  83. if (requireCheckItems.some(item => !checkEmptyValid(item.value))) {
  84. return false;
  85. }
  86. const validations = Object.values(validation);
  87. return validations.every(v => !(v as IValidationItem).result);
  88. };
  89. const resetValidation = (form: IForm[]) => {
  90. const validation = form
  91. .filter(f => f.needCheck)
  92. .reduce(
  93. (acc, cur) => ({
  94. ...acc,
  95. [cur.key]: { result: cur.value === '', errText: '' },
  96. }),
  97. {}
  98. );
  99. setValidation(validation);
  100. };
  101. return {
  102. checkFormValid,
  103. checkIsValid,
  104. validation,
  105. disabled,
  106. setDisabled,
  107. resetValidation,
  108. };
  109. };