mitre-form-component 0.0.34 → 0.0.35

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,198 @@ 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
+ border-radius: 0.125rem;
787
+ background: var(--gray-500);
788
+ font-family: "Montserrat", sans-serif;
789
+
790
+ /* Style for the inner phone input container */
791
+ .react-international-phone-input-container {
792
+ width: 100%;
793
+ height: 100%;
794
+ display: flex;
795
+ align-items: center;
796
+ overflow: hidden;
797
+ font-family: "Montserrat", sans-serif;
798
+ font-size: 1rem;
799
+ font-weight: 500;
800
+ color: var(--black);
801
+ background: var(--gray-500);
802
+ border-radius: 0.125rem;
803
+
804
+ /* Apply dynamic border color here */
805
+ border: 1px solid ${(props) => props.$bordercolor || "transparent"};
806
+ }
807
+
808
+ /* Style for the country selector button */
809
+ .react-international-phone-country-selector-button {
810
+ height: 100%;
811
+ background: var(--gray-500);
812
+ display: flex;
813
+ align-items: center;
814
+ justify-content: center;
815
+ font-family: "Montserrat", sans-serif;
816
+ font-size: 1rem;
817
+ font-weight: 500;
818
+ cursor: pointer;
819
+ border: none;
820
+ outline: none;
821
+ }
822
+
823
+ /* Style for the input itself */
824
+ input.react-international-phone-input {
825
+ flex: 1;
826
+ height: 100%;
827
+ padding: 0 0.5rem;
828
+ margin: 0;
829
+ background: var(--gray-500);
830
+ font-family: "Montserrat", sans-serif;
831
+ font-style: normal;
832
+ font-weight: 500;
833
+ font-size: 1rem;
834
+ line-height: 1.5rem;
835
+ color: var(--black);
836
+ outline: none;
837
+ border: none;
838
+ border-radius: 0.125rem;
839
+
840
+ &::placeholder {
841
+ font-size: 1rem;
842
+ line-height: 1.5rem;
843
+ color: #b6b6b6;
844
+ font-weight: 800;
845
+ }
846
+
847
+ &:-webkit-autofill {
848
+ background: var(--gray-500) !important;
849
+ -webkit-text-fill-color: var(--black) !important;
850
+ transition: background-color 5000s ease-in-out 0s;
851
+ }
852
+
853
+ &:-webkit-autofill::first-line {
854
+ font-family: "Montserrat", sans-serif;
855
+ font-size: 1rem;
856
+ font-weight: 500;
857
+ }
858
+ }
859
+ `;
860
+ var FormErrorMessage2 = styled5.small`
861
+ font-size: 0.75rem;
862
+ line-height: 1.125rem;
863
+ color: var(--red);
864
+ margin-top: 0.25rem;
865
+ display: block;
866
+ `;
867
+ var FormControl2 = styled5.div.withConfig({
868
+ shouldForwardProp: (prop) => !["isInvalid"].includes(prop)
869
+ })`
870
+ ${FormLabel2} {
871
+ ${(props) => props.isInvalid && css4`
872
+ color: var(--red);
873
+ `};
874
+ }
875
+
876
+ ${StyledPhoneInput} {
877
+ .react-international-phone-input-container {
878
+ ${(props) => props.isInvalid && css4`
879
+ border: 1px solid var(--red);
880
+
881
+ &:not(:focus)::placeholder {
882
+ color: var(--red);
883
+ font-weight: 600;
884
+ }
885
+ `};
886
+ }
887
+ }
888
+ `;
889
+
890
+ // src/components/PhoneInput/index.tsx
891
+ import { jsx as jsx4, jsxs as jsxs4 } from "react/jsx-runtime";
892
+ var PhoneInputBase = ({ id, label, error, showErrorMessage = true, borderColor, textColor, value, ...rest }, ref) => {
893
+ return /* @__PURE__ */ jsxs4(FormControl2, { isInvalid: !!error, children: [
894
+ !!label && /* @__PURE__ */ jsx4(FormLabel2, { htmlFor: id, $textColor: textColor, children: label }),
895
+ /* @__PURE__ */ jsx4(
896
+ StyledPhoneInput,
897
+ {
898
+ ref,
899
+ defaultCountry: "br",
900
+ disableCountryGuess: true,
901
+ disableDialCodeAndPrefix: true,
902
+ showDisabledDialCodeAndPrefix: false,
903
+ $bordercolor: borderColor,
904
+ placeholder: rest.placeholder,
905
+ "aria-invalid": !!error && showErrorMessage ? "true" : "false",
906
+ value: String(value),
907
+ onChange: (phone, meta) => {
908
+ if (typeof rest.onChange === "function") {
909
+ rest.onChange({
910
+ phone,
911
+ dialCode: meta.country.dialCode,
912
+ inputValue: meta.inputValue
913
+ });
914
+ }
915
+ },
916
+ inputProps: {
917
+ id,
918
+ name: rest.name || "phone",
919
+ required: rest.required,
920
+ autoFocus: rest.autoFocus,
921
+ autoComplete: rest.autoComplete || "tel"
922
+ }
923
+ }
924
+ ),
925
+ !!error && showErrorMessage && /* @__PURE__ */ jsx4(FormErrorMessage2, { "data-testid": "error-message", children: typeof error === "string" ? error : error?.message })
926
+ ] });
927
+ };
928
+ var PhoneInput2 = forwardRef2(PhoneInputBase);
929
+
861
930
  // src/components/Form/index.tsx
862
- import { Fragment, jsx as jsx4, jsxs as jsxs4 } from "react/jsx-runtime";
931
+ import { Fragment, jsx as jsx5, jsxs as jsxs5 } from "react/jsx-runtime";
932
+ var phoneUtil = PhoneNumberUtil.getInstance();
933
+ var isPhoneValid = (phone) => {
934
+ try {
935
+ return phoneUtil.isValidNumber(phoneUtil.parseAndKeepRawInput(phone));
936
+ } catch (error) {
937
+ console.log("erro ao validar telefone = " + error);
938
+ return false;
939
+ }
940
+ };
863
941
  var schema = yup.object().shape({
864
942
  name: yup.string().required("Nome \xE9 obrigat\xF3rio"),
865
943
  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
- )
944
+ phone: yup.object().shape({
945
+ phone: yup.string().required("Telefone \xE9 obrigat\xF3rio!").test(
946
+ "is-valid-phone",
947
+ "N\xFAmero de telefone inv\xE1lido!",
948
+ function(value) {
949
+ const digitsOnly = value?.replace(/\D/g, "") || "";
950
+ return digitsOnly.length >= 8 && isPhoneValid(value);
951
+ }
952
+ ),
953
+ inputValue: yup.string().required("Telefone \xE9 obrigat\xF3rio!"),
954
+ dialCode: yup.string().required("C\xF3digo de pa\xEDs \xE9 obrigat\xF3rio")
955
+ })
874
956
  });
875
957
  var MitreFormComponent = React3.forwardRef(({
876
958
  productId,
@@ -889,12 +971,32 @@ var MitreFormComponent = React3.forwardRef(({
889
971
  const [loading, setIsLoading] = useState3(false);
890
972
  const { error, handleError, clearError } = useError();
891
973
  const [successMessage, setSuccessMessage] = useState3("");
892
- const { control, register, handleSubmit, formState: { errors }, reset, watch } = useForm({
893
- resolver: yupResolver(schema)
974
+ const [formKey, setFormKey] = useState3(0);
975
+ const { control, register, handleSubmit, formState: { errors }, reset, clearErrors } = useForm({
976
+ resolver: yupResolver(schema),
977
+ mode: "onSubmit"
894
978
  });
895
- const phoneValue = watch("phone");
979
+ const resetForm = () => {
980
+ reset({
981
+ name: "",
982
+ email: "",
983
+ phone: { phone: "", inputValue: "", dialCode: "" }
984
+ }, {
985
+ keepErrors: false,
986
+ keepDirty: false,
987
+ keepTouched: false,
988
+ keepIsSubmitted: false,
989
+ keepSubmitCount: false,
990
+ keepValues: false,
991
+ keepDefaultValues: false
992
+ });
993
+ clearErrors();
994
+ setFormKey((k) => k + 1);
995
+ };
896
996
  const sendMessage = async (data) => {
897
997
  const { name, email, phone } = data;
998
+ const phoneValue = phone.inputValue;
999
+ const phoneDigitsOnly = phoneValue?.replace(/\D/g, "") || "";
898
1000
  const message = "Gostaria de mais informa\xE7\xF5es sobre o produto";
899
1001
  try {
900
1002
  setIsLoading(true);
@@ -910,7 +1012,7 @@ var MitreFormComponent = React3.forwardRef(({
910
1012
  body: JSON.stringify({
911
1013
  name,
912
1014
  email,
913
- phone,
1015
+ phone: phoneDigitsOnly,
914
1016
  message,
915
1017
  productId,
916
1018
  utm_source,
@@ -920,24 +1022,21 @@ var MitreFormComponent = React3.forwardRef(({
920
1022
  })
921
1023
  });
922
1024
  if (!response.ok) {
1025
+ console.log("response = " + JSON.stringify(response));
923
1026
  throw new Error("Falha ao enviar a mensagem!");
924
1027
  }
925
1028
  setSuccessMessage("Mensagem enviada com sucesso!");
926
- reset({
927
- name: "",
928
- email: "",
929
- phone: ""
930
- });
1029
+ resetForm();
931
1030
  } catch (err) {
932
1031
  handleError(err);
933
1032
  } finally {
934
1033
  setIsLoading(false);
935
1034
  }
936
1035
  };
937
- return /* @__PURE__ */ jsxs4(Fragment, { children: [
938
- /* @__PURE__ */ jsx4(global_default, {}),
939
- /* @__PURE__ */ jsx4(GlobalStyles, {}),
940
- error && /* @__PURE__ */ jsx4(
1036
+ return /* @__PURE__ */ jsxs5(Fragment, { children: [
1037
+ /* @__PURE__ */ jsx5(global_default, {}),
1038
+ /* @__PURE__ */ jsx5(GlobalStyles, {}),
1039
+ error && /* @__PURE__ */ jsx5(
941
1040
  Alert,
942
1041
  {
943
1042
  type: "error",
@@ -947,7 +1046,7 @@ var MitreFormComponent = React3.forwardRef(({
947
1046
  children: error.message
948
1047
  }
949
1048
  ),
950
- successMessage && /* @__PURE__ */ jsx4(
1049
+ successMessage && /* @__PURE__ */ jsx5(
951
1050
  Alert,
952
1051
  {
953
1052
  type: "success",
@@ -957,13 +1056,13 @@ var MitreFormComponent = React3.forwardRef(({
957
1056
  children: successMessage
958
1057
  }
959
1058
  ),
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." })
1059
+ /* @__PURE__ */ jsxs5(FormContainer, { $backgroundColor: backgroundColor, $innerPadding: innerPadding, ref, children: [
1060
+ showHeader && /* @__PURE__ */ jsxs5(HeaderContainer, { children: [
1061
+ /* @__PURE__ */ jsx5(Title, { $textColor: textColor, children: "Atendimento por mensagem" }),
1062
+ /* @__PURE__ */ jsx5(Text, { $textColor: textColor, children: "Informe seus dados e retornaremos a mensagem." })
964
1063
  ] }),
965
- /* @__PURE__ */ jsxs4(Form, { $textColor: textColor, onSubmit: handleSubmit(sendMessage), noValidate: true, children: [
966
- /* @__PURE__ */ jsx4(
1064
+ /* @__PURE__ */ jsxs5(Form, { $textColor: textColor, onSubmit: handleSubmit(sendMessage), noValidate: true, children: [
1065
+ /* @__PURE__ */ jsx5(
967
1066
  Input2,
968
1067
  {
969
1068
  id: "name",
@@ -977,7 +1076,7 @@ var MitreFormComponent = React3.forwardRef(({
977
1076
  required: true
978
1077
  }
979
1078
  ),
980
- /* @__PURE__ */ jsx4(
1079
+ /* @__PURE__ */ jsx5(
981
1080
  Input2,
982
1081
  {
983
1082
  id: "email",
@@ -992,24 +1091,40 @@ var MitreFormComponent = React3.forwardRef(({
992
1091
  required: true
993
1092
  }
994
1093
  ),
995
- /* @__PURE__ */ jsx4(
996
- Input2,
1094
+ /* @__PURE__ */ jsx5(
1095
+ Controller,
997
1096
  {
998
- id: "phone",
999
- label: "Telefone *",
1000
- placeholder: "(11) 00000-0000",
1001
- mask: "phone",
1002
- borderColor: colorPrimary,
1003
- textColor,
1004
- required: true
1005
- }
1097
+ name: "phone",
1098
+ control,
1099
+ defaultValue: { phone: "", inputValue: "", dialCode: "" },
1100
+ shouldUnregister: true,
1101
+ render: ({ field }) => {
1102
+ const errorMsg = errors.phone?.inputValue?.message || errors.phone?.phone?.message || errors.phone?.message;
1103
+ return /* @__PURE__ */ jsx5(
1104
+ PhoneInput2,
1105
+ {
1106
+ id: "phone",
1107
+ label: "Telefone *",
1108
+ placeholder: "(11) 00000-0000",
1109
+ borderColor: colorPrimary,
1110
+ textColor,
1111
+ error: errorMsg,
1112
+ required: true,
1113
+ value: field.value.phone,
1114
+ onChange: field.onChange,
1115
+ name: field.name
1116
+ }
1117
+ );
1118
+ }
1119
+ },
1120
+ formKey
1006
1121
  ),
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: [
1122
+ /* @__PURE__ */ jsx5("h6", { children: "* Campos de preenchimento obrigat\xF3rio." }),
1123
+ /* @__PURE__ */ jsx5(ButtonContainer, { children: /* @__PURE__ */ jsx5(Button2, { bgColor: colorPrimary, color: textColor, type: "submit", isSubmitting: loading, children: "Enviar mensagem" }) }),
1124
+ /* @__PURE__ */ jsxs5("p", { children: [
1010
1125
  "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
1126
  " ",
1012
- /* @__PURE__ */ jsx4(
1127
+ /* @__PURE__ */ jsx5(
1013
1128
  "a",
1014
1129
  {
1015
1130
  href: "https://www.mitrerealty.com.br/politica-de-privacidade",