mitre-form-component 0.0.34 → 0.0.36

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/dist/index.js CHANGED
@@ -15,9 +15,10 @@ function useError() {
15
15
  }
16
16
 
17
17
  // src/components/Form/index.tsx
18
- import { useForm } from "react-hook-form";
18
+ import { useForm, Controller } from "react-hook-form";
19
19
  import { yupResolver } from "@hookform/resolvers/yup";
20
20
  import * as yup from "yup";
21
+ import { PhoneNumberUtil } from "google-libphonenumber";
21
22
 
22
23
  // src/components/styles/utils.ts
23
24
  function flex(direction = "row", alignItems, justifyContent) {
@@ -344,14 +345,12 @@ function date(e) {
344
345
 
345
346
  // src/components/Input/styles.ts
346
347
  import styled2, { css } from "styled-components";
347
- import { PhoneInput } from "react-international-phone";
348
- import "react-international-phone/style.css";
349
348
  var FormLabel = styled2.label`
350
349
  font-family: "Montserrat", sans-serif;
351
350
  font-style: normal;
352
351
  font-weight: 500;
353
352
  font-size: 1rem;
354
- color: ${(props) => props.isInvalid ? "var(--red)" : props.$textColor || "var(--black)"};
353
+ color: ${(props) => props.$textColor || "var(--black)"};
355
354
  display: block;
356
355
  margin-bottom: 0.5rem;
357
356
  text-align: left;
@@ -406,7 +405,7 @@ var FormErrorMessage = styled2.small`
406
405
  display: block;
407
406
  `;
408
407
  var FormControl = styled2.div.withConfig({
409
- shouldForwardProp: (prop) => !["isInvalid", "$bordercolor"].includes(prop)
408
+ shouldForwardProp: (prop) => !["isInvalid"].includes(prop)
410
409
  })`
411
410
  ${FormLabel} {
412
411
  ${(props) => props.isInvalid && css`
@@ -431,74 +430,6 @@ var FormControl = styled2.div.withConfig({
431
430
  }
432
431
  }
433
432
  `;
434
- var StyledPhoneInput = styled2(PhoneInput)`
435
- width: 100%;
436
- height: 3.125rem;
437
-
438
- &:focus-within {
439
- border: 2px solid ${(props) => props.$bordercolor || "var(--yellow-500)"};
440
- border-radius: 0.5rem;
441
- }
442
-
443
- .react-international-phone-input-container {
444
- width: 100%;
445
- height: 100%;
446
- border: 1px solid transparent;
447
- display: flex;
448
- align-items: center;
449
- overflow: hidden;
450
- font-family: "Montserrat", sans-serif;
451
- font-size: 1rem;
452
- font-weight: 500;
453
- color: var(--black);
454
- }
455
-
456
- .react-international-phone-country-selector-button {
457
- height: 100%;
458
- background: var(--gray-500);
459
- display: flex;
460
- align-items: center;
461
- justify-content: center;
462
- font-family: "Montserrat", sans-serif;
463
- font-size: 1rem;
464
- font-weight: 500;
465
- cursor: pointer;
466
- }
467
-
468
- input.react-international-phone-input {
469
- flex: 1;
470
- height: 100%;
471
- padding: 0 0.5rem;
472
- margin: 0;
473
- background: var(--gray-500);
474
- font-family: "Montserrat", sans-serif;
475
- font-style: normal;
476
- font-weight: 500;
477
- font-size: 1rem;
478
- line-height: 1.5rem;
479
- color: var(--black);
480
- outline: none;
481
-
482
- &::placeholder {
483
- font-size: 1rem;
484
- line-height: 1.5rem;
485
- color: #b6b6b6;
486
- font-weight: 800;
487
- }
488
-
489
- &:-webkit-autofill {
490
- background: var(--gray-500) !important;
491
- -webkit-text-fill-color: var(--black) !important;
492
- transition: background-color 5000s ease-in-out 0s;
493
- }
494
-
495
- &:-webkit-autofill::first-line {
496
- font-family: "Montserrat", sans-serif;
497
- font-size: 1rem;
498
- font-weight: 500;
499
- }
500
- }
501
- `;
502
433
 
503
434
  // src/components/Input/index.tsx
504
435
  import { jsx, jsxs } from "react/jsx-runtime";
@@ -525,34 +456,6 @@ var InputBase = ({ id, label, error, showErrorMessage = true, borderColor, textC
525
456
  autoComplete: rest.autoComplete || "on",
526
457
  ...rest
527
458
  }
528
- ) : mask === "phone" ? /* @__PURE__ */ jsx(
529
- StyledPhoneInput,
530
- {
531
- defaultCountry: "br",
532
- disableCountryGuess: true,
533
- disableDialCodeAndPrefix: true,
534
- $bordercolor: borderColor,
535
- placeholder: rest.placeholder,
536
- "aria-invalid": !!error && showErrorMessage ? "true" : "false",
537
- onChange: (value) => {
538
- if (typeof rest.onChange === "function") {
539
- const event = {
540
- target: {
541
- name: rest.name,
542
- value
543
- }
544
- };
545
- rest.onChange(event);
546
- }
547
- },
548
- inputProps: {
549
- id,
550
- name: "phone",
551
- required: true,
552
- autoFocus: true,
553
- autoComplete: "tel"
554
- }
555
- }
556
459
  ) : /* @__PURE__ */ jsx(
557
460
  Input,
558
461
  {
@@ -858,19 +761,209 @@ var Alert = ({
858
761
  );
859
762
  };
860
763
 
764
+ // src/components/PhoneInput/index.tsx
765
+ import {
766
+ forwardRef as forwardRef2
767
+ } from "react";
768
+
769
+ // src/components/PhoneInput/styles.ts
770
+ import styled5, { css as css4 } from "styled-components";
771
+ import { PhoneInput } from "react-international-phone";
772
+ import "react-international-phone/style.css";
773
+ var FormLabel2 = styled5.label`
774
+ font-family: "Montserrat", sans-serif;
775
+ font-style: normal;
776
+ font-weight: 500;
777
+ font-size: 1rem;
778
+ color: ${(props) => props.$textColor || "var(--black)"};
779
+ display: block;
780
+ margin-bottom: 0.5rem;
781
+ text-align: left;
782
+ `;
783
+ var StyledPhoneInput = styled5(PhoneInput)`
784
+ width: 100%;
785
+ height: 3.125rem;
786
+ background: var(--gray-500);
787
+ font-family: "Montserrat", sans-serif;
788
+
789
+ &:focus-within {
790
+ border-radius: 0.125rem;
791
+ border: 2px solid ${(props) => props.$bordercolor || "var(--yellow-500)"};
792
+ outline: none;
793
+ }
794
+
795
+ /* Style for the inner phone input container */
796
+ .react-international-phone-input-container {
797
+ width: 100%;
798
+ height: 100%;
799
+ display: flex;
800
+ align-items: center;
801
+ overflow: hidden;
802
+ font-family: "Montserrat", sans-serif;
803
+ font-size: 1rem;
804
+ font-weight: 500;
805
+ color: var(--black);
806
+ background: var(--white);
807
+ }
808
+
809
+ /* Style for the country selector button */
810
+ .react-international-phone-country-selector-button {
811
+ height: 100%;
812
+ background: var(--white);
813
+ display: flex;
814
+ align-items: center;
815
+ justify-content: center;
816
+ font-family: "Montserrat", sans-serif;
817
+ font-size: 1rem;
818
+ font-weight: 500;
819
+ cursor: pointer;
820
+ outline: none;
821
+ border: none;
822
+ border-right: 2px solid var(--gray-300);
823
+ }
824
+
825
+ /* Style for the input itself */
826
+ input.react-international-phone-input {
827
+ flex: 1;
828
+ height: 100%;
829
+ padding: 0 0.5rem;
830
+ margin: 0;
831
+ background: var(--white);
832
+ font-family: "Montserrat", sans-serif;
833
+ font-style: normal;
834
+ font-weight: 500;
835
+ font-size: 1rem;
836
+ line-height: 1.5rem;
837
+ color: var(--black);
838
+ outline: none;
839
+ border: none;
840
+ border-radius: 0.125rem;
841
+
842
+ &::placeholder {
843
+ font-size: 1rem;
844
+ line-height: 1.5rem;
845
+ color: #b6b6b6;
846
+ font-weight: 800;
847
+ }
848
+
849
+ &:-webkit-autofill {
850
+ background: var(--gray-500) !important;
851
+ -webkit-text-fill-color: var(--black) !important;
852
+ transition: background-color 5000s ease-in-out 0s;
853
+ }
854
+
855
+ &:-webkit-autofill::first-line {
856
+ font-family: "Montserrat", sans-serif;
857
+ font-size: 1rem;
858
+ font-weight: 500;
859
+ }
860
+ }
861
+ `;
862
+ var FormErrorMessage2 = styled5.small`
863
+ font-size: 0.75rem;
864
+ line-height: 1.125rem;
865
+ color: var(--red);
866
+ margin-top: 0.25rem;
867
+ display: block;
868
+ `;
869
+ var FormControl2 = styled5.div.withConfig({
870
+ shouldForwardProp: (prop) => !["isInvalid"].includes(prop)
871
+ })`
872
+ ${FormLabel2} {
873
+ ${(props) => props.isInvalid && css4`
874
+ color: var(--red);
875
+ `};
876
+ }
877
+
878
+ ${StyledPhoneInput} {
879
+ ${(props) => props.isInvalid && css4`
880
+ border: 1px solid var(--red);
881
+
882
+ &:not(:focus)::placeholder {
883
+ color: var(--red);
884
+ font-weight: 600;
885
+ }
886
+ `};
887
+
888
+ .react-international-phone-input-container {
889
+ ${(props) => props.isInvalid && css4`
890
+ border: 1px solid var(--red);
891
+
892
+ &:not(:focus)::placeholder {
893
+ color: var(--red);
894
+ font-weight: 600;
895
+ }
896
+ `};
897
+ }
898
+ }
899
+ `;
900
+
901
+ // src/components/PhoneInput/index.tsx
902
+ import { jsx as jsx4, jsxs as jsxs4 } from "react/jsx-runtime";
903
+ var PhoneInputBase = ({ id, label, error, showErrorMessage = true, borderColor, textColor, value, ...rest }, ref) => {
904
+ return /* @__PURE__ */ jsxs4(FormControl2, { isInvalid: !!error, children: [
905
+ !!label && /* @__PURE__ */ jsx4(FormLabel2, { htmlFor: id, $textColor: textColor, children: label }),
906
+ /* @__PURE__ */ jsx4(
907
+ StyledPhoneInput,
908
+ {
909
+ ref,
910
+ defaultCountry: "br",
911
+ disableCountryGuess: true,
912
+ disableDialCodeAndPrefix: true,
913
+ showDisabledDialCodeAndPrefix: false,
914
+ $bordercolor: borderColor,
915
+ placeholder: rest.placeholder,
916
+ "aria-invalid": !!error && showErrorMessage ? "true" : "false",
917
+ value: String(value),
918
+ onChange: (phone, meta) => {
919
+ if (typeof rest.onChange === "function") {
920
+ rest.onChange({
921
+ phone,
922
+ dialCode: meta.country.dialCode,
923
+ inputValue: meta.inputValue
924
+ });
925
+ }
926
+ },
927
+ inputProps: {
928
+ id,
929
+ name: rest.name || "phone",
930
+ required: rest.required,
931
+ autoFocus: rest.autoFocus,
932
+ autoComplete: rest.autoComplete || "tel"
933
+ }
934
+ }
935
+ ),
936
+ !!error && showErrorMessage && /* @__PURE__ */ jsx4(FormErrorMessage2, { "data-testid": "error-message", children: typeof error === "string" ? error : error?.message })
937
+ ] });
938
+ };
939
+ var PhoneInput2 = forwardRef2(PhoneInputBase);
940
+
861
941
  // src/components/Form/index.tsx
862
- import { Fragment, jsx as jsx4, jsxs as jsxs4 } from "react/jsx-runtime";
942
+ import { Fragment, jsx as jsx5, jsxs as jsxs5 } from "react/jsx-runtime";
943
+ var phoneUtil = PhoneNumberUtil.getInstance();
944
+ var isPhoneValid = (phone) => {
945
+ try {
946
+ return phoneUtil.isValidNumber(phoneUtil.parseAndKeepRawInput(phone));
947
+ } catch (error) {
948
+ console.log("erro ao validar telefone = " + error);
949
+ return false;
950
+ }
951
+ };
863
952
  var schema = yup.object().shape({
864
953
  name: yup.string().required("Nome \xE9 obrigat\xF3rio"),
865
954
  email: yup.string().required("Email \xE9 obrigat\xF3rio").email("Email inv\xE1lido"),
866
- phone: yup.string().required("Telefone \xE9 obrigat\xF3rio").test(
867
- "min-digits",
868
- "N\xFAmero de telefone inv\xE1lido!",
869
- (value) => {
870
- const digitsOnly = value?.replace(/\D/g, "") || "";
871
- return digitsOnly.length >= 8;
872
- }
873
- )
955
+ phone: yup.object().shape({
956
+ phone: yup.string().required("Telefone \xE9 obrigat\xF3rio!").test(
957
+ "is-valid-phone",
958
+ "N\xFAmero de telefone inv\xE1lido!",
959
+ function(value) {
960
+ const digitsOnly = value?.replace(/\D/g, "") || "";
961
+ return digitsOnly.length >= 8 && isPhoneValid(value);
962
+ }
963
+ ),
964
+ inputValue: yup.string().required("Telefone \xE9 obrigat\xF3rio!"),
965
+ dialCode: yup.string().required("C\xF3digo de pa\xEDs \xE9 obrigat\xF3rio")
966
+ })
874
967
  });
875
968
  var MitreFormComponent = React3.forwardRef(({
876
969
  productId,
@@ -883,18 +976,38 @@ var MitreFormComponent = React3.forwardRef(({
883
976
  showHeader = true,
884
977
  colorPrimary = "#F6C76B",
885
978
  textColor = "#2F2F2F",
886
- backgroundColor = "#cecece",
979
+ backgroundColor = "#CECECE",
887
980
  innerPadding = "1rem"
888
981
  }, ref) => {
889
982
  const [loading, setIsLoading] = useState3(false);
890
983
  const { error, handleError, clearError } = useError();
891
984
  const [successMessage, setSuccessMessage] = useState3("");
892
- const { control, register, handleSubmit, formState: { errors }, reset, watch } = useForm({
893
- resolver: yupResolver(schema)
985
+ const [formKey, setFormKey] = useState3(0);
986
+ const { control, register, handleSubmit, formState: { errors }, reset, clearErrors } = useForm({
987
+ resolver: yupResolver(schema),
988
+ mode: "onSubmit"
894
989
  });
895
- const phoneValue = watch("phone");
990
+ const resetForm = () => {
991
+ reset({
992
+ name: "",
993
+ email: "",
994
+ phone: { phone: "", inputValue: "", dialCode: "" }
995
+ }, {
996
+ keepErrors: false,
997
+ keepDirty: false,
998
+ keepTouched: false,
999
+ keepIsSubmitted: false,
1000
+ keepSubmitCount: false,
1001
+ keepValues: false,
1002
+ keepDefaultValues: false
1003
+ });
1004
+ clearErrors();
1005
+ setFormKey((k) => k + 1);
1006
+ };
896
1007
  const sendMessage = async (data) => {
897
1008
  const { name, email, phone } = data;
1009
+ const phoneValue = phone.inputValue;
1010
+ const phoneDigitsOnly = phoneValue?.replace(/\D/g, "") || "";
898
1011
  const message = "Gostaria de mais informa\xE7\xF5es sobre o produto";
899
1012
  try {
900
1013
  setIsLoading(true);
@@ -910,7 +1023,7 @@ var MitreFormComponent = React3.forwardRef(({
910
1023
  body: JSON.stringify({
911
1024
  name,
912
1025
  email,
913
- phone,
1026
+ phone: phoneDigitsOnly,
914
1027
  message,
915
1028
  productId,
916
1029
  utm_source,
@@ -920,24 +1033,21 @@ var MitreFormComponent = React3.forwardRef(({
920
1033
  })
921
1034
  });
922
1035
  if (!response.ok) {
1036
+ console.log("response = " + JSON.stringify(response));
923
1037
  throw new Error("Falha ao enviar a mensagem!");
924
1038
  }
925
1039
  setSuccessMessage("Mensagem enviada com sucesso!");
926
- reset({
927
- name: "",
928
- email: "",
929
- phone: ""
930
- });
1040
+ resetForm();
931
1041
  } catch (err) {
932
1042
  handleError(err);
933
1043
  } finally {
934
1044
  setIsLoading(false);
935
1045
  }
936
1046
  };
937
- return /* @__PURE__ */ jsxs4(Fragment, { children: [
938
- /* @__PURE__ */ jsx4(global_default, {}),
939
- /* @__PURE__ */ jsx4(GlobalStyles, {}),
940
- error && /* @__PURE__ */ jsx4(
1047
+ return /* @__PURE__ */ jsxs5(Fragment, { children: [
1048
+ /* @__PURE__ */ jsx5(global_default, {}),
1049
+ /* @__PURE__ */ jsx5(GlobalStyles, {}),
1050
+ error && /* @__PURE__ */ jsx5(
941
1051
  Alert,
942
1052
  {
943
1053
  type: "error",
@@ -947,7 +1057,7 @@ var MitreFormComponent = React3.forwardRef(({
947
1057
  children: error.message
948
1058
  }
949
1059
  ),
950
- successMessage && /* @__PURE__ */ jsx4(
1060
+ successMessage && /* @__PURE__ */ jsx5(
951
1061
  Alert,
952
1062
  {
953
1063
  type: "success",
@@ -957,13 +1067,13 @@ var MitreFormComponent = React3.forwardRef(({
957
1067
  children: successMessage
958
1068
  }
959
1069
  ),
960
- /* @__PURE__ */ jsxs4(FormContainer, { $backgroundColor: backgroundColor, $innerPadding: innerPadding, ref, children: [
961
- showHeader && /* @__PURE__ */ jsxs4(HeaderContainer, { children: [
962
- /* @__PURE__ */ jsx4(Title, { $textColor: textColor, children: "Atendimento por mensagem" }),
963
- /* @__PURE__ */ jsx4(Text, { $textColor: textColor, children: "Informe seus dados e retornaremos a mensagem." })
1070
+ /* @__PURE__ */ jsxs5(FormContainer, { $backgroundColor: backgroundColor, $innerPadding: innerPadding, ref, children: [
1071
+ showHeader && /* @__PURE__ */ jsxs5(HeaderContainer, { children: [
1072
+ /* @__PURE__ */ jsx5(Title, { $textColor: textColor, children: "Atendimento por mensagem" }),
1073
+ /* @__PURE__ */ jsx5(Text, { $textColor: textColor, children: "Informe seus dados e retornaremos a mensagem." })
964
1074
  ] }),
965
- /* @__PURE__ */ jsxs4(Form, { $textColor: textColor, onSubmit: handleSubmit(sendMessage), noValidate: true, children: [
966
- /* @__PURE__ */ jsx4(
1075
+ /* @__PURE__ */ jsxs5(Form, { $textColor: textColor, onSubmit: handleSubmit(sendMessage), noValidate: true, children: [
1076
+ /* @__PURE__ */ jsx5(
967
1077
  Input2,
968
1078
  {
969
1079
  id: "name",
@@ -977,7 +1087,7 @@ var MitreFormComponent = React3.forwardRef(({
977
1087
  required: true
978
1088
  }
979
1089
  ),
980
- /* @__PURE__ */ jsx4(
1090
+ /* @__PURE__ */ jsx5(
981
1091
  Input2,
982
1092
  {
983
1093
  id: "email",
@@ -992,24 +1102,40 @@ var MitreFormComponent = React3.forwardRef(({
992
1102
  required: true
993
1103
  }
994
1104
  ),
995
- /* @__PURE__ */ jsx4(
996
- Input2,
1105
+ /* @__PURE__ */ jsx5(
1106
+ Controller,
997
1107
  {
998
- id: "phone",
999
- label: "Telefone *",
1000
- placeholder: "(11) 00000-0000",
1001
- mask: "phone",
1002
- borderColor: colorPrimary,
1003
- textColor,
1004
- required: true
1005
- }
1108
+ name: "phone",
1109
+ control,
1110
+ defaultValue: { phone: "", inputValue: "", dialCode: "" },
1111
+ shouldUnregister: true,
1112
+ render: ({ field }) => {
1113
+ const errorMsg = errors.phone?.inputValue?.message || errors.phone?.phone?.message || errors.phone?.message;
1114
+ return /* @__PURE__ */ jsx5(
1115
+ PhoneInput2,
1116
+ {
1117
+ id: "phone",
1118
+ label: "Telefone *",
1119
+ placeholder: "(11) 00000-0000",
1120
+ borderColor: colorPrimary,
1121
+ textColor,
1122
+ error: errorMsg,
1123
+ required: true,
1124
+ value: field.value.phone,
1125
+ onChange: field.onChange,
1126
+ name: field.name
1127
+ }
1128
+ );
1129
+ }
1130
+ },
1131
+ formKey
1006
1132
  ),
1007
- /* @__PURE__ */ jsx4("h6", { children: "* Campos de preenchimento obrigat\xF3rio." }),
1008
- /* @__PURE__ */ jsx4(ButtonContainer, { children: /* @__PURE__ */ jsx4(Button2, { bgColor: colorPrimary, color: textColor, type: "submit", isSubmitting: loading, children: "Enviar mensagem" }) }),
1009
- /* @__PURE__ */ jsxs4("p", { children: [
1133
+ /* @__PURE__ */ jsx5("h6", { children: "* Campos de preenchimento obrigat\xF3rio." }),
1134
+ /* @__PURE__ */ jsx5(ButtonContainer, { children: /* @__PURE__ */ jsx5(Button2, { bgColor: colorPrimary, color: textColor, type: "submit", isSubmitting: loading, children: "Enviar mensagem" }) }),
1135
+ /* @__PURE__ */ jsxs5("p", { children: [
1010
1136
  "A Mitre Realty respeita a sua privacidade e utiliza os seus dados pessoais para contat\xE1-lo por e-mail ou telefone aqui registrados. Para saber mais, acesse a nossa",
1011
1137
  " ",
1012
- /* @__PURE__ */ jsx4(
1138
+ /* @__PURE__ */ jsx5(
1013
1139
  "a",
1014
1140
  {
1015
1141
  href: "https://www.mitrerealty.com.br/politica-de-privacidade",