@thecb/components 10.9.0-beta.13 → 10.9.0-beta.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thecb/components",
3
- "version": "10.9.0-beta.13",
3
+ "version": "10.9.0-beta.14",
4
4
  "description": "Common lib for CityBase react components",
5
5
  "main": "dist/index.cjs.js",
6
6
  "typings": "dist/index.d.ts",
@@ -8,6 +8,7 @@ import {
8
8
  import Checkbox from "../../atoms/checkbox";
9
9
  import Paragraph from "../../atoms/paragraph";
10
10
  import { noop } from "../../../util/general";
11
+ import { useConditionallyAddValidator } from "../../../hooks";
11
12
 
12
13
  const EmailForm = ({
13
14
  variant = "default",
@@ -22,30 +23,19 @@ const EmailForm = ({
22
23
  walletCheckboxMarked,
23
24
  isRequired = false
24
25
  }) => {
25
- console.log("🚀 ~ isRequired:", isRequired)
26
26
  if (clearOnDismount) {
27
27
  useEffect(() => () => actions.form.clear(), []);
28
28
  }
29
+ useConditionallyAddValidator(
30
+ isRequired,
31
+ required,
32
+ actions.fields.email.addValidator,
33
+ actions.fields.email.removeValidator
34
+ );
29
35
  const emailFieldErrorMessages = {
30
36
  [required.error]: "Email address is required",
31
37
  [isProbablyEmail.error]: "Invalid email address"
32
38
  };
33
- useEffect(() => {
34
- debugger;
35
- if (isRequired) {
36
- console.log('adding email validator')
37
- actions.fields.email.addValidator(required());
38
- }
39
- // else {
40
- // console.log('removing email validator')
41
- // actions.fields.email.removeValidator(required());
42
- // }
43
- return () => {
44
- console.log('removing email validator')
45
- // If the form is not rendered it should not be required.
46
- actions.fields.email.removeValidator(required());
47
- };
48
- }, [isRequired]);
49
39
 
50
40
  return (
51
41
  <FormContainer variant={variant} role="form" aria-label="Email address">
@@ -4,11 +4,12 @@ import { createFormat } from "formatted-input";
4
4
  import {
5
5
  FormInput,
6
6
  FormContainer,
7
- FormInputColumn,
7
+ FormInputColumn
8
8
  } from "../../atoms/form-layouts";
9
9
  import { noop } from "../../../util/general";
10
10
  import Checkbox from "../../atoms/checkbox";
11
11
  import { phoneFormats, formatDelimiter } from "../../../util/formats";
12
+ import { useConditionallyAddValidator } from "../../../hooks";
12
13
 
13
14
  const PhoneForm = ({
14
15
  variant = "default",
@@ -25,27 +26,16 @@ const PhoneForm = ({
25
26
  if (clearOnDismount) {
26
27
  useEffect(() => () => actions.form.clear(), []);
27
28
  }
29
+ useConditionallyAddValidator(
30
+ isRequired,
31
+ required,
32
+ actions.fields.phone.addValidator,
33
+ actions.fields.phone.removeValidator
34
+ );
28
35
  const phoneErrorMessage = {
29
36
  [required.error]: "Phone number is required",
30
37
  [hasLength.error]: "Phone number must be 10 digits"
31
38
  };
32
- useEffect(() => {
33
- if (isRequired) {
34
- console.log("adding phone validator");
35
- actions.fields.phone.addValidator(required());
36
- }
37
- // else {
38
- // debugger;
39
- // console.log('removing phone validator 1')
40
- // actions.fields.phone.removeValidator(required());
41
- // }
42
- return () => {
43
- console.log("removing phone validator 2");
44
- // If the form is not rendered it should not be required.
45
- actions.fields.phone.removeValidator(required());
46
- };
47
- }, [isRequired]);
48
-
49
39
  return (
50
40
  <FormContainer variant={variant} role="form" aria-label="Phone number">
51
41
  <FormInputColumn>
@@ -1,3 +1,4 @@
1
1
  export { default as useOutsideClick } from "./use-outside-click";
2
2
  export { default as useScrollTo } from "./use-scroll-to";
3
3
  export { default as useToastNotification } from "./use-toast-notification";
4
+ export { default as useConditionallyAddValidator } from "./use-conditionally-add-validator";
@@ -1,31 +1,18 @@
1
1
  import { useEffect } from "react";
2
2
 
3
- // const validatorAlreadyExists = (currentValidators) => {
4
- // if ( currentValidators
5
- // .map((v) => currentValidators.type)
6
- // .includes('validator/NUMBER_GREATER_THAN')) {
7
-
8
- // }
9
- // }
10
-
11
-
12
3
  function useConditionallyAddValidator(
13
4
  condition,
14
- currentValidators,
5
+ validatorFn,
15
6
  addValidator,
16
- removeValidator,
17
- field
7
+ removeValidator
18
8
  ) {
19
9
  useEffect(() => {
20
- if (isRequired) {
21
- addValidator();
22
- } else {
23
- removeValidator();
10
+ if (condition) {
11
+ addValidator(validatorFn());
24
12
  }
25
13
  return () => {
26
- console.log("removing phone validator 2");
27
- // If the form is not rendered it should not be required.
28
- removeValidator(required());
14
+ // Remove validator when component unmounts
15
+ removeValidator(validatorFn());
29
16
  };
30
17
  }, [condition, addValidator, removeValidator]);
31
18
  }