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.cjs CHANGED
@@ -35,7 +35,7 @@ __export(index_exports, {
35
35
  module.exports = __toCommonJS(index_exports);
36
36
 
37
37
  // src/components/Form/index.tsx
38
- var import_react5 = __toESM(require("react"), 1);
38
+ var import_react6 = __toESM(require("react"), 1);
39
39
 
40
40
  // src/components/hooks/useError.ts
41
41
  var import_react = require("react");
@@ -54,6 +54,7 @@ function useError() {
54
54
  var import_react_hook_form = require("react-hook-form");
55
55
  var import_yup = require("@hookform/resolvers/yup");
56
56
  var yup = __toESM(require("yup"), 1);
57
+ var import_google_libphonenumber = require("google-libphonenumber");
57
58
 
58
59
  // src/components/styles/utils.ts
59
60
  function flex(direction = "row", alignItems, justifyContent) {
@@ -377,14 +378,12 @@ function date(e) {
377
378
 
378
379
  // src/components/Input/styles.ts
379
380
  var import_styled_components3 = __toESM(require("styled-components"), 1);
380
- var import_react_international_phone = require("react-international-phone");
381
- var import_style = require("react-international-phone/style.css");
382
381
  var FormLabel = import_styled_components3.default.label`
383
382
  font-family: "Montserrat", sans-serif;
384
383
  font-style: normal;
385
384
  font-weight: 500;
386
385
  font-size: 1rem;
387
- color: ${(props) => props.isInvalid ? "var(--red)" : props.$textColor || "var(--black)"};
386
+ color: ${(props) => props.$textColor || "var(--black)"};
388
387
  display: block;
389
388
  margin-bottom: 0.5rem;
390
389
  text-align: left;
@@ -439,7 +438,7 @@ var FormErrorMessage = import_styled_components3.default.small`
439
438
  display: block;
440
439
  `;
441
440
  var FormControl = import_styled_components3.default.div.withConfig({
442
- shouldForwardProp: (prop) => !["isInvalid", "$bordercolor"].includes(prop)
441
+ shouldForwardProp: (prop) => !["isInvalid"].includes(prop)
443
442
  })`
444
443
  ${FormLabel} {
445
444
  ${(props) => props.isInvalid && import_styled_components3.css`
@@ -464,74 +463,6 @@ var FormControl = import_styled_components3.default.div.withConfig({
464
463
  }
465
464
  }
466
465
  `;
467
- var StyledPhoneInput = (0, import_styled_components3.default)(import_react_international_phone.PhoneInput)`
468
- width: 100%;
469
- height: 3.125rem;
470
-
471
- &:focus-within {
472
- border: 2px solid ${(props) => props.$bordercolor || "var(--yellow-500)"};
473
- border-radius: 0.5rem;
474
- }
475
-
476
- .react-international-phone-input-container {
477
- width: 100%;
478
- height: 100%;
479
- border: 1px solid transparent;
480
- display: flex;
481
- align-items: center;
482
- overflow: hidden;
483
- font-family: "Montserrat", sans-serif;
484
- font-size: 1rem;
485
- font-weight: 500;
486
- color: var(--black);
487
- }
488
-
489
- .react-international-phone-country-selector-button {
490
- height: 100%;
491
- background: var(--gray-500);
492
- display: flex;
493
- align-items: center;
494
- justify-content: center;
495
- font-family: "Montserrat", sans-serif;
496
- font-size: 1rem;
497
- font-weight: 500;
498
- cursor: pointer;
499
- }
500
-
501
- input.react-international-phone-input {
502
- flex: 1;
503
- height: 100%;
504
- padding: 0 0.5rem;
505
- margin: 0;
506
- background: var(--gray-500);
507
- font-family: "Montserrat", sans-serif;
508
- font-style: normal;
509
- font-weight: 500;
510
- font-size: 1rem;
511
- line-height: 1.5rem;
512
- color: var(--black);
513
- outline: none;
514
-
515
- &::placeholder {
516
- font-size: 1rem;
517
- line-height: 1.5rem;
518
- color: #b6b6b6;
519
- font-weight: 800;
520
- }
521
-
522
- &:-webkit-autofill {
523
- background: var(--gray-500) !important;
524
- -webkit-text-fill-color: var(--black) !important;
525
- transition: background-color 5000s ease-in-out 0s;
526
- }
527
-
528
- &:-webkit-autofill::first-line {
529
- font-family: "Montserrat", sans-serif;
530
- font-size: 1rem;
531
- font-weight: 500;
532
- }
533
- }
534
- `;
535
466
 
536
467
  // src/components/Input/index.tsx
537
468
  var import_jsx_runtime = require("react/jsx-runtime");
@@ -558,34 +489,6 @@ var InputBase = ({ id, label, error, showErrorMessage = true, borderColor, textC
558
489
  autoComplete: rest.autoComplete || "on",
559
490
  ...rest
560
491
  }
561
- ) : mask === "phone" ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
562
- StyledPhoneInput,
563
- {
564
- defaultCountry: "br",
565
- disableCountryGuess: true,
566
- disableDialCodeAndPrefix: true,
567
- $bordercolor: borderColor,
568
- placeholder: rest.placeholder,
569
- "aria-invalid": !!error && showErrorMessage ? "true" : "false",
570
- onChange: (value) => {
571
- if (typeof rest.onChange === "function") {
572
- const event = {
573
- target: {
574
- name: rest.name,
575
- value
576
- }
577
- };
578
- rest.onChange(event);
579
- }
580
- },
581
- inputProps: {
582
- id,
583
- name: "phone",
584
- required: true,
585
- autoFocus: true,
586
- autoComplete: "tel"
587
- }
588
- }
589
492
  ) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
590
493
  Input,
591
494
  {
@@ -891,21 +794,198 @@ var Alert = ({
891
794
  );
892
795
  };
893
796
 
894
- // src/components/Form/index.tsx
797
+ // src/components/PhoneInput/index.tsx
798
+ var import_react5 = require("react");
799
+
800
+ // src/components/PhoneInput/styles.ts
801
+ var import_styled_components6 = __toESM(require("styled-components"), 1);
802
+ var import_react_international_phone = require("react-international-phone");
803
+ var import_style = require("react-international-phone/style.css");
804
+ var FormLabel2 = import_styled_components6.default.label`
805
+ font-family: "Montserrat", sans-serif;
806
+ font-style: normal;
807
+ font-weight: 500;
808
+ font-size: 1rem;
809
+ color: ${(props) => props.$textColor || "var(--black)"};
810
+ display: block;
811
+ margin-bottom: 0.5rem;
812
+ text-align: left;
813
+ `;
814
+ var StyledPhoneInput = (0, import_styled_components6.default)(import_react_international_phone.PhoneInput)`
815
+ width: 100%;
816
+ height: 3.125rem;
817
+ border-radius: 0.125rem;
818
+ background: var(--gray-500);
819
+ font-family: "Montserrat", sans-serif;
820
+
821
+ /* Style for the inner phone input container */
822
+ .react-international-phone-input-container {
823
+ width: 100%;
824
+ height: 100%;
825
+ display: flex;
826
+ align-items: center;
827
+ overflow: hidden;
828
+ font-family: "Montserrat", sans-serif;
829
+ font-size: 1rem;
830
+ font-weight: 500;
831
+ color: var(--black);
832
+ background: var(--gray-500);
833
+ border-radius: 0.125rem;
834
+
835
+ /* Apply dynamic border color here */
836
+ border: 1px solid ${(props) => props.$bordercolor || "transparent"};
837
+ }
838
+
839
+ /* Style for the country selector button */
840
+ .react-international-phone-country-selector-button {
841
+ height: 100%;
842
+ background: var(--gray-500);
843
+ display: flex;
844
+ align-items: center;
845
+ justify-content: center;
846
+ font-family: "Montserrat", sans-serif;
847
+ font-size: 1rem;
848
+ font-weight: 500;
849
+ cursor: pointer;
850
+ border: none;
851
+ outline: none;
852
+ }
853
+
854
+ /* Style for the input itself */
855
+ input.react-international-phone-input {
856
+ flex: 1;
857
+ height: 100%;
858
+ padding: 0 0.5rem;
859
+ margin: 0;
860
+ background: var(--gray-500);
861
+ font-family: "Montserrat", sans-serif;
862
+ font-style: normal;
863
+ font-weight: 500;
864
+ font-size: 1rem;
865
+ line-height: 1.5rem;
866
+ color: var(--black);
867
+ outline: none;
868
+ border: none;
869
+ border-radius: 0.125rem;
870
+
871
+ &::placeholder {
872
+ font-size: 1rem;
873
+ line-height: 1.5rem;
874
+ color: #b6b6b6;
875
+ font-weight: 800;
876
+ }
877
+
878
+ &:-webkit-autofill {
879
+ background: var(--gray-500) !important;
880
+ -webkit-text-fill-color: var(--black) !important;
881
+ transition: background-color 5000s ease-in-out 0s;
882
+ }
883
+
884
+ &:-webkit-autofill::first-line {
885
+ font-family: "Montserrat", sans-serif;
886
+ font-size: 1rem;
887
+ font-weight: 500;
888
+ }
889
+ }
890
+ `;
891
+ var FormErrorMessage2 = import_styled_components6.default.small`
892
+ font-size: 0.75rem;
893
+ line-height: 1.125rem;
894
+ color: var(--red);
895
+ margin-top: 0.25rem;
896
+ display: block;
897
+ `;
898
+ var FormControl2 = import_styled_components6.default.div.withConfig({
899
+ shouldForwardProp: (prop) => !["isInvalid"].includes(prop)
900
+ })`
901
+ ${FormLabel2} {
902
+ ${(props) => props.isInvalid && import_styled_components6.css`
903
+ color: var(--red);
904
+ `};
905
+ }
906
+
907
+ ${StyledPhoneInput} {
908
+ .react-international-phone-input-container {
909
+ ${(props) => props.isInvalid && import_styled_components6.css`
910
+ border: 1px solid var(--red);
911
+
912
+ &:not(:focus)::placeholder {
913
+ color: var(--red);
914
+ font-weight: 600;
915
+ }
916
+ `};
917
+ }
918
+ }
919
+ `;
920
+
921
+ // src/components/PhoneInput/index.tsx
895
922
  var import_jsx_runtime4 = require("react/jsx-runtime");
923
+ var PhoneInputBase = ({ id, label, error, showErrorMessage = true, borderColor, textColor, value, ...rest }, ref) => {
924
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(FormControl2, { isInvalid: !!error, children: [
925
+ !!label && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(FormLabel2, { htmlFor: id, $textColor: textColor, children: label }),
926
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
927
+ StyledPhoneInput,
928
+ {
929
+ ref,
930
+ defaultCountry: "br",
931
+ disableCountryGuess: true,
932
+ disableDialCodeAndPrefix: true,
933
+ showDisabledDialCodeAndPrefix: false,
934
+ $bordercolor: borderColor,
935
+ placeholder: rest.placeholder,
936
+ "aria-invalid": !!error && showErrorMessage ? "true" : "false",
937
+ value: String(value),
938
+ onChange: (phone, meta) => {
939
+ if (typeof rest.onChange === "function") {
940
+ rest.onChange({
941
+ phone,
942
+ dialCode: meta.country.dialCode,
943
+ inputValue: meta.inputValue
944
+ });
945
+ }
946
+ },
947
+ inputProps: {
948
+ id,
949
+ name: rest.name || "phone",
950
+ required: rest.required,
951
+ autoFocus: rest.autoFocus,
952
+ autoComplete: rest.autoComplete || "tel"
953
+ }
954
+ }
955
+ ),
956
+ !!error && showErrorMessage && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(FormErrorMessage2, { "data-testid": "error-message", children: typeof error === "string" ? error : error?.message })
957
+ ] });
958
+ };
959
+ var PhoneInput2 = (0, import_react5.forwardRef)(PhoneInputBase);
960
+
961
+ // src/components/Form/index.tsx
962
+ var import_jsx_runtime5 = require("react/jsx-runtime");
963
+ var phoneUtil = import_google_libphonenumber.PhoneNumberUtil.getInstance();
964
+ var isPhoneValid = (phone) => {
965
+ try {
966
+ return phoneUtil.isValidNumber(phoneUtil.parseAndKeepRawInput(phone));
967
+ } catch (error) {
968
+ console.log("erro ao validar telefone = " + error);
969
+ return false;
970
+ }
971
+ };
896
972
  var schema = yup.object().shape({
897
973
  name: yup.string().required("Nome \xE9 obrigat\xF3rio"),
898
974
  email: yup.string().required("Email \xE9 obrigat\xF3rio").email("Email inv\xE1lido"),
899
- phone: yup.string().required("Telefone \xE9 obrigat\xF3rio").test(
900
- "min-digits",
901
- "N\xFAmero de telefone inv\xE1lido!",
902
- (value) => {
903
- const digitsOnly = value?.replace(/\D/g, "") || "";
904
- return digitsOnly.length >= 8;
905
- }
906
- )
975
+ phone: yup.object().shape({
976
+ phone: yup.string().required("Telefone \xE9 obrigat\xF3rio!").test(
977
+ "is-valid-phone",
978
+ "N\xFAmero de telefone inv\xE1lido!",
979
+ function(value) {
980
+ const digitsOnly = value?.replace(/\D/g, "") || "";
981
+ return digitsOnly.length >= 8 && isPhoneValid(value);
982
+ }
983
+ ),
984
+ inputValue: yup.string().required("Telefone \xE9 obrigat\xF3rio!"),
985
+ dialCode: yup.string().required("C\xF3digo de pa\xEDs \xE9 obrigat\xF3rio")
986
+ })
907
987
  });
908
- var MitreFormComponent = import_react5.default.forwardRef(({
988
+ var MitreFormComponent = import_react6.default.forwardRef(({
909
989
  productId,
910
990
  apiUrl,
911
991
  apiToken,
@@ -919,15 +999,35 @@ var MitreFormComponent = import_react5.default.forwardRef(({
919
999
  backgroundColor = "#cecece",
920
1000
  innerPadding = "1rem"
921
1001
  }, ref) => {
922
- const [loading, setIsLoading] = (0, import_react5.useState)(false);
1002
+ const [loading, setIsLoading] = (0, import_react6.useState)(false);
923
1003
  const { error, handleError, clearError } = useError();
924
- const [successMessage, setSuccessMessage] = (0, import_react5.useState)("");
925
- const { control, register, handleSubmit, formState: { errors }, reset, watch } = (0, import_react_hook_form.useForm)({
926
- resolver: (0, import_yup.yupResolver)(schema)
1004
+ const [successMessage, setSuccessMessage] = (0, import_react6.useState)("");
1005
+ const [formKey, setFormKey] = (0, import_react6.useState)(0);
1006
+ const { control, register, handleSubmit, formState: { errors }, reset, clearErrors } = (0, import_react_hook_form.useForm)({
1007
+ resolver: (0, import_yup.yupResolver)(schema),
1008
+ mode: "onSubmit"
927
1009
  });
928
- const phoneValue = watch("phone");
1010
+ const resetForm = () => {
1011
+ reset({
1012
+ name: "",
1013
+ email: "",
1014
+ phone: { phone: "", inputValue: "", dialCode: "" }
1015
+ }, {
1016
+ keepErrors: false,
1017
+ keepDirty: false,
1018
+ keepTouched: false,
1019
+ keepIsSubmitted: false,
1020
+ keepSubmitCount: false,
1021
+ keepValues: false,
1022
+ keepDefaultValues: false
1023
+ });
1024
+ clearErrors();
1025
+ setFormKey((k) => k + 1);
1026
+ };
929
1027
  const sendMessage = async (data) => {
930
1028
  const { name, email, phone } = data;
1029
+ const phoneValue = phone.inputValue;
1030
+ const phoneDigitsOnly = phoneValue?.replace(/\D/g, "") || "";
931
1031
  const message = "Gostaria de mais informa\xE7\xF5es sobre o produto";
932
1032
  try {
933
1033
  setIsLoading(true);
@@ -943,7 +1043,7 @@ var MitreFormComponent = import_react5.default.forwardRef(({
943
1043
  body: JSON.stringify({
944
1044
  name,
945
1045
  email,
946
- phone,
1046
+ phone: phoneDigitsOnly,
947
1047
  message,
948
1048
  productId,
949
1049
  utm_source,
@@ -953,24 +1053,21 @@ var MitreFormComponent = import_react5.default.forwardRef(({
953
1053
  })
954
1054
  });
955
1055
  if (!response.ok) {
1056
+ console.log("response = " + JSON.stringify(response));
956
1057
  throw new Error("Falha ao enviar a mensagem!");
957
1058
  }
958
1059
  setSuccessMessage("Mensagem enviada com sucesso!");
959
- reset({
960
- name: "",
961
- email: "",
962
- phone: ""
963
- });
1060
+ resetForm();
964
1061
  } catch (err) {
965
1062
  handleError(err);
966
1063
  } finally {
967
1064
  setIsLoading(false);
968
1065
  }
969
1066
  };
970
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_jsx_runtime4.Fragment, { children: [
971
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(global_default, {}),
972
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(GlobalStyles, {}),
973
- error && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
1067
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(import_jsx_runtime5.Fragment, { children: [
1068
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(global_default, {}),
1069
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(GlobalStyles, {}),
1070
+ error && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
974
1071
  Alert,
975
1072
  {
976
1073
  type: "error",
@@ -980,7 +1077,7 @@ var MitreFormComponent = import_react5.default.forwardRef(({
980
1077
  children: error.message
981
1078
  }
982
1079
  ),
983
- successMessage && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
1080
+ successMessage && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
984
1081
  Alert,
985
1082
  {
986
1083
  type: "success",
@@ -990,13 +1087,13 @@ var MitreFormComponent = import_react5.default.forwardRef(({
990
1087
  children: successMessage
991
1088
  }
992
1089
  ),
993
- /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(FormContainer, { $backgroundColor: backgroundColor, $innerPadding: innerPadding, ref, children: [
994
- showHeader && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(HeaderContainer, { children: [
995
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Title, { $textColor: textColor, children: "Atendimento por mensagem" }),
996
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Text, { $textColor: textColor, children: "Informe seus dados e retornaremos a mensagem." })
1090
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(FormContainer, { $backgroundColor: backgroundColor, $innerPadding: innerPadding, ref, children: [
1091
+ showHeader && /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(HeaderContainer, { children: [
1092
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Title, { $textColor: textColor, children: "Atendimento por mensagem" }),
1093
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Text, { $textColor: textColor, children: "Informe seus dados e retornaremos a mensagem." })
997
1094
  ] }),
998
- /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(Form, { $textColor: textColor, onSubmit: handleSubmit(sendMessage), noValidate: true, children: [
999
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
1095
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(Form, { $textColor: textColor, onSubmit: handleSubmit(sendMessage), noValidate: true, children: [
1096
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1000
1097
  Input2,
1001
1098
  {
1002
1099
  id: "name",
@@ -1010,7 +1107,7 @@ var MitreFormComponent = import_react5.default.forwardRef(({
1010
1107
  required: true
1011
1108
  }
1012
1109
  ),
1013
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
1110
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1014
1111
  Input2,
1015
1112
  {
1016
1113
  id: "email",
@@ -1025,24 +1122,40 @@ var MitreFormComponent = import_react5.default.forwardRef(({
1025
1122
  required: true
1026
1123
  }
1027
1124
  ),
1028
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
1029
- Input2,
1125
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1126
+ import_react_hook_form.Controller,
1030
1127
  {
1031
- id: "phone",
1032
- label: "Telefone *",
1033
- placeholder: "(11) 00000-0000",
1034
- mask: "phone",
1035
- borderColor: colorPrimary,
1036
- textColor,
1037
- required: true
1038
- }
1128
+ name: "phone",
1129
+ control,
1130
+ defaultValue: { phone: "", inputValue: "", dialCode: "" },
1131
+ shouldUnregister: true,
1132
+ render: ({ field }) => {
1133
+ const errorMsg = errors.phone?.inputValue?.message || errors.phone?.phone?.message || errors.phone?.message;
1134
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1135
+ PhoneInput2,
1136
+ {
1137
+ id: "phone",
1138
+ label: "Telefone *",
1139
+ placeholder: "(11) 00000-0000",
1140
+ borderColor: colorPrimary,
1141
+ textColor,
1142
+ error: errorMsg,
1143
+ required: true,
1144
+ value: field.value.phone,
1145
+ onChange: field.onChange,
1146
+ name: field.name
1147
+ }
1148
+ );
1149
+ }
1150
+ },
1151
+ formKey
1039
1152
  ),
1040
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("h6", { children: "* Campos de preenchimento obrigat\xF3rio." }),
1041
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(ButtonContainer, { children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Button2, { bgColor: colorPrimary, color: textColor, type: "submit", isSubmitting: loading, children: "Enviar mensagem" }) }),
1042
- /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("p", { children: [
1153
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("h6", { children: "* Campos de preenchimento obrigat\xF3rio." }),
1154
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(ButtonContainer, { children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Button2, { bgColor: colorPrimary, color: textColor, type: "submit", isSubmitting: loading, children: "Enviar mensagem" }) }),
1155
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("p", { children: [
1043
1156
  "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",
1044
1157
  " ",
1045
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
1158
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1046
1159
  "a",
1047
1160
  {
1048
1161
  href: "https://www.mitrerealty.com.br/politica-de-privacidade",