mitre-form-component 0.0.33 → 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
@@ -18,6 +18,7 @@ function useError() {
18
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) {
@@ -73,6 +74,17 @@ var ButtonContainer = styled.div`
73
74
  justify-content: center;
74
75
  width: 100%;
75
76
  margin-top: 0.75rem;
77
+
78
+ button {
79
+ ${opacityEffect}
80
+ color: var(--black);
81
+ font-weight: 600;
82
+ border: none;
83
+ border-radius: 8px;
84
+ width: 60%;
85
+ margin-top: 10px;
86
+ margin-bottom: 10px;
87
+ }
76
88
  `;
77
89
  var Form = styled.form`
78
90
  label {
@@ -110,17 +122,6 @@ var Form = styled.form`
110
122
  & > div {
111
123
  margin-bottom: 10px;,
112
124
  }
113
-
114
- button {
115
- ${opacityEffect}
116
- color: var(--black);
117
- font-weight: 600;
118
- border: none;
119
- border-radius: 8px;
120
- width: 60%;
121
- margin-top: 10px;
122
- margin-bottom: 10px;
123
- }
124
125
  `;
125
126
  var Title = styled.h2`
126
127
  font-size: 1.25rem;
@@ -301,8 +302,7 @@ var global_default = FontLoader;
301
302
  // src/components/Input/index.tsx
302
303
  import {
303
304
  forwardRef,
304
- useCallback,
305
- useRef
305
+ useCallback
306
306
  } from "react";
307
307
 
308
308
  // src/components/Input/masks.ts
@@ -343,10 +343,6 @@ function date(e) {
343
343
  return e;
344
344
  }
345
345
 
346
- // src/components/Input/index.tsx
347
- import { PhoneInput } from "react-international-phone";
348
- import "react-international-phone/style.css";
349
-
350
346
  // src/components/Input/styles.ts
351
347
  import styled2, { css } from "styled-components";
352
348
  var FormLabel = styled2.label`
@@ -354,7 +350,7 @@ var FormLabel = styled2.label`
354
350
  font-style: normal;
355
351
  font-weight: 500;
356
352
  font-size: 1rem;
357
- color: ${(props) => props.isInvalid ? "var(--red)" : props.$textColor || "var(--black)"};
353
+ color: ${(props) => props.$textColor || "var(--black)"};
358
354
  display: block;
359
355
  margin-bottom: 0.5rem;
360
356
  text-align: left;
@@ -409,7 +405,7 @@ var FormErrorMessage = styled2.small`
409
405
  display: block;
410
406
  `;
411
407
  var FormControl = styled2.div.withConfig({
412
- shouldForwardProp: (prop) => !["isInvalid", "$bordercolor"].includes(prop)
408
+ shouldForwardProp: (prop) => !["isInvalid"].includes(prop)
413
409
  })`
414
410
  ${FormLabel} {
415
411
  ${(props) => props.isInvalid && css`
@@ -438,7 +434,6 @@ var FormControl = styled2.div.withConfig({
438
434
  // src/components/Input/index.tsx
439
435
  import { jsx, jsxs } from "react/jsx-runtime";
440
436
  var InputBase = ({ id, label, error, showErrorMessage = true, borderColor, textColor, mask = "", type = "text", ...rest }, ref) => {
441
- const phoneInputRef = useRef(null);
442
437
  const handleKeyUp = useCallback(
443
438
  (e) => {
444
439
  if (mask === "cep") cep(e);
@@ -461,23 +456,6 @@ var InputBase = ({ id, label, error, showErrorMessage = true, borderColor, textC
461
456
  autoComplete: rest.autoComplete || "on",
462
457
  ...rest
463
458
  }
464
- ) : mask === "phone" ? /* @__PURE__ */ jsx(
465
- PhoneInput,
466
- {
467
- defaultCountry: "ua",
468
- value: String(rest.value),
469
- onChange: (value) => {
470
- if (typeof rest.onChange === "function") {
471
- const event = {
472
- target: {
473
- name: rest.name,
474
- value
475
- }
476
- };
477
- rest.onChange(event);
478
- }
479
- }
480
- }
481
459
  ) : /* @__PURE__ */ jsx(
482
460
  Input,
483
461
  {
@@ -783,19 +761,198 @@ var Alert = ({
783
761
  );
784
762
  };
785
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
+
786
930
  // src/components/Form/index.tsx
787
- 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
+ };
788
941
  var schema = yup.object().shape({
789
942
  name: yup.string().required("Nome \xE9 obrigat\xF3rio"),
790
943
  email: yup.string().required("Email \xE9 obrigat\xF3rio").email("Email inv\xE1lido"),
791
- phone: yup.string().required("Telefone \xE9 obrigat\xF3rio").test(
792
- "min-digits",
793
- "N\xFAmero de telefone inv\xE1lido!",
794
- (value) => {
795
- const digitsOnly = value?.replace(/\D/g, "") || "";
796
- return digitsOnly.length >= 8;
797
- }
798
- )
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
+ })
799
956
  });
800
957
  var MitreFormComponent = React3.forwardRef(({
801
958
  productId,
@@ -814,12 +971,32 @@ var MitreFormComponent = React3.forwardRef(({
814
971
  const [loading, setIsLoading] = useState3(false);
815
972
  const { error, handleError, clearError } = useError();
816
973
  const [successMessage, setSuccessMessage] = useState3("");
817
- const { control, register, handleSubmit, formState: { errors }, reset, watch } = useForm({
818
- 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"
819
978
  });
820
- 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
+ };
821
996
  const sendMessage = async (data) => {
822
997
  const { name, email, phone } = data;
998
+ const phoneValue = phone.inputValue;
999
+ const phoneDigitsOnly = phoneValue?.replace(/\D/g, "") || "";
823
1000
  const message = "Gostaria de mais informa\xE7\xF5es sobre o produto";
824
1001
  try {
825
1002
  setIsLoading(true);
@@ -835,7 +1012,7 @@ var MitreFormComponent = React3.forwardRef(({
835
1012
  body: JSON.stringify({
836
1013
  name,
837
1014
  email,
838
- phone,
1015
+ phone: phoneDigitsOnly,
839
1016
  message,
840
1017
  productId,
841
1018
  utm_source,
@@ -845,24 +1022,21 @@ var MitreFormComponent = React3.forwardRef(({
845
1022
  })
846
1023
  });
847
1024
  if (!response.ok) {
1025
+ console.log("response = " + JSON.stringify(response));
848
1026
  throw new Error("Falha ao enviar a mensagem!");
849
1027
  }
850
1028
  setSuccessMessage("Mensagem enviada com sucesso!");
851
- reset({
852
- name: "",
853
- email: "",
854
- phone: ""
855
- });
1029
+ resetForm();
856
1030
  } catch (err) {
857
1031
  handleError(err);
858
1032
  } finally {
859
1033
  setIsLoading(false);
860
1034
  }
861
1035
  };
862
- return /* @__PURE__ */ jsxs4(Fragment, { children: [
863
- /* @__PURE__ */ jsx4(global_default, {}),
864
- /* @__PURE__ */ jsx4(GlobalStyles, {}),
865
- error && /* @__PURE__ */ jsx4(
1036
+ return /* @__PURE__ */ jsxs5(Fragment, { children: [
1037
+ /* @__PURE__ */ jsx5(global_default, {}),
1038
+ /* @__PURE__ */ jsx5(GlobalStyles, {}),
1039
+ error && /* @__PURE__ */ jsx5(
866
1040
  Alert,
867
1041
  {
868
1042
  type: "error",
@@ -872,7 +1046,7 @@ var MitreFormComponent = React3.forwardRef(({
872
1046
  children: error.message
873
1047
  }
874
1048
  ),
875
- successMessage && /* @__PURE__ */ jsx4(
1049
+ successMessage && /* @__PURE__ */ jsx5(
876
1050
  Alert,
877
1051
  {
878
1052
  type: "success",
@@ -882,13 +1056,13 @@ var MitreFormComponent = React3.forwardRef(({
882
1056
  children: successMessage
883
1057
  }
884
1058
  ),
885
- /* @__PURE__ */ jsxs4(FormContainer, { $backgroundColor: backgroundColor, $innerPadding: innerPadding, ref, children: [
886
- showHeader && /* @__PURE__ */ jsxs4(HeaderContainer, { children: [
887
- /* @__PURE__ */ jsx4(Title, { $textColor: textColor, children: "Atendimento por mensagem" }),
888
- /* @__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." })
889
1063
  ] }),
890
- /* @__PURE__ */ jsxs4(Form, { $textColor: textColor, onSubmit: handleSubmit(sendMessage), noValidate: true, children: [
891
- /* @__PURE__ */ jsx4(
1064
+ /* @__PURE__ */ jsxs5(Form, { $textColor: textColor, onSubmit: handleSubmit(sendMessage), noValidate: true, children: [
1065
+ /* @__PURE__ */ jsx5(
892
1066
  Input2,
893
1067
  {
894
1068
  id: "name",
@@ -902,7 +1076,7 @@ var MitreFormComponent = React3.forwardRef(({
902
1076
  required: true
903
1077
  }
904
1078
  ),
905
- /* @__PURE__ */ jsx4(
1079
+ /* @__PURE__ */ jsx5(
906
1080
  Input2,
907
1081
  {
908
1082
  id: "email",
@@ -917,36 +1091,40 @@ var MitreFormComponent = React3.forwardRef(({
917
1091
  required: true
918
1092
  }
919
1093
  ),
920
- /* @__PURE__ */ jsx4(
1094
+ /* @__PURE__ */ jsx5(
921
1095
  Controller,
922
1096
  {
923
- control,
924
1097
  name: "phone",
925
- defaultValue: "",
926
- render: ({ field, fieldState }) => /* @__PURE__ */ jsx4(
927
- Input2,
928
- {
929
- id: "phone",
930
- label: "Telefone *",
931
- placeholder: "(11) 00000-0000",
932
- mask: "phone",
933
- borderColor: colorPrimary,
934
- textColor,
935
- error: fieldState.error?.message,
936
- required: true,
937
- value: field.value,
938
- onChange: field.onChange,
939
- name: field.name
940
- }
941
- )
942
- }
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
943
1121
  ),
944
- /* @__PURE__ */ jsx4("h6", { children: "* Campos de preenchimento obrigat\xF3rio." }),
945
- /* @__PURE__ */ jsx4(ButtonContainer, { children: /* @__PURE__ */ jsx4(Button2, { bgColor: colorPrimary, color: textColor, type: "submit", isSubmitting: loading, children: "Enviar mensagem" }) }),
946
- /* @__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: [
947
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",
948
1126
  " ",
949
- /* @__PURE__ */ jsx4(
1127
+ /* @__PURE__ */ jsx5(
950
1128
  "a",
951
1129
  {
952
1130
  href: "https://www.mitrerealty.com.br/politica-de-privacidade",