use-mask-input 3.6.0 → 3.6.1

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.
Files changed (39) hide show
  1. package/CHANGELOG.md +47 -76
  2. package/README.md +2 -251
  3. package/dist/index.cjs +157 -84
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.cts +52 -11
  6. package/dist/index.d.ts +52 -11
  7. package/dist/index.js +158 -85
  8. package/dist/index.js.map +1 -1
  9. package/package.json +21 -21
  10. package/src/api/index.ts +4 -0
  11. package/src/api/useHookFormMask.spec.ts +146 -0
  12. package/src/api/useHookFormMask.ts +56 -0
  13. package/src/api/useMaskInput-server.spec.tsx +30 -0
  14. package/src/api/useMaskInput.spec.tsx +220 -0
  15. package/src/api/useMaskInput.ts +64 -0
  16. package/src/api/withHookFormMask.spec.ts +155 -0
  17. package/src/api/withHookFormMask.ts +54 -0
  18. package/src/api/withMask.spec.ts +93 -0
  19. package/src/api/withMask.ts +25 -0
  20. package/src/core/elementResolver.spec.ts +175 -0
  21. package/src/core/elementResolver.ts +84 -0
  22. package/src/core/index.ts +3 -0
  23. package/src/core/maskConfig.spec.ts +183 -0
  24. package/src/{utils/getMaskOptions.ts → core/maskConfig.ts} +12 -3
  25. package/src/core/maskEngine.spec.ts +108 -0
  26. package/src/core/maskEngine.ts +47 -0
  27. package/src/index.tsx +12 -5
  28. package/src/{types.ts → types/index.ts} +13 -0
  29. package/src/utils/flow.spec.ts +27 -30
  30. package/src/utils/flow.ts +2 -2
  31. package/src/utils/index.ts +1 -1
  32. package/src/utils/isServer.spec.ts +15 -0
  33. package/src/utils/moduleInterop.spec.ts +37 -0
  34. package/src/useHookFormMask.ts +0 -47
  35. package/src/useMaskInput.ts +0 -41
  36. package/src/utils/getMaskOptions.spec.ts +0 -126
  37. package/src/withHookFormMask.ts +0 -34
  38. package/src/withMask.ts +0 -18
  39. /package/src/{inputmask.types.ts → types/inputmask.types.ts} +0 -0
package/dist/index.cjs CHANGED
@@ -28,17 +28,17 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  mod
29
29
  ));
30
30
 
31
- // node_modules/inputmask/dist/inputmask.js
31
+ // ../../node_modules/.pnpm/inputmask@5.0.10-beta.61/node_modules/inputmask/dist/inputmask.js
32
32
  var require_inputmask = __commonJS({
33
- "node_modules/inputmask/dist/inputmask.js"(exports, module) {
33
+ "../../node_modules/.pnpm/inputmask@5.0.10-beta.61/node_modules/inputmask/dist/inputmask.js"(exports$1, module) {
34
34
  !(function(e, t) {
35
- if ("object" == typeof exports && "object" == typeof module) module.exports = t();
35
+ if ("object" == typeof exports$1 && "object" == typeof module) module.exports = t();
36
36
  else if ("function" == typeof define && define.amd) define([], t);
37
37
  else {
38
38
  var n = t();
39
- for (var i in n) ("object" == typeof exports ? exports : e)[i] = n[i];
39
+ for (var i in n) ("object" == typeof exports$1 ? exports$1 : e)[i] = n[i];
40
40
  }
41
- })("undefined" != typeof self ? self : exports, function() {
41
+ })("undefined" != typeof self ? self : exports$1, function() {
42
42
  return (function() {
43
43
  var e = {
44
44
  340: function(e2, t2) {
@@ -3679,35 +3679,52 @@ var require_inputmask = __commonJS({
3679
3679
  }
3680
3680
  });
3681
3681
 
3682
- // src/withHookFormMask.ts
3683
- var import_inputmask = __toESM(require_inputmask());
3684
-
3685
- // src/utils/flow.ts
3686
- function flow(...funcs) {
3687
- const { length } = funcs;
3688
- let index = length;
3689
- while (index > 0) {
3690
- index -= 1;
3691
- if (typeof funcs[index] !== "function") {
3692
- throw new TypeError("Expected a function");
3682
+ // src/core/elementResolver.ts
3683
+ function isHTMLElement(element) {
3684
+ return element !== null && typeof element === "object" && "nodeType" in element && "querySelector" in element && typeof element.querySelector === "function";
3685
+ }
3686
+ function findInputElement(element) {
3687
+ if (!element) return null;
3688
+ if (!isHTMLElement(element)) {
3689
+ return null;
3690
+ }
3691
+ if (element.nodeName === "INPUT" || element.nodeName === "TEXTAREA") {
3692
+ return element;
3693
+ }
3694
+ if (!("querySelector" in element) || typeof element.querySelector !== "function") {
3695
+ return null;
3696
+ }
3697
+ try {
3698
+ const inputElement = element.querySelector("input") ?? element.querySelector("textarea");
3699
+ if (inputElement && isHTMLElement(inputElement)) {
3700
+ return inputElement;
3693
3701
  }
3702
+ } catch {
3703
+ return null;
3694
3704
  }
3695
- return (...args) => {
3696
- let i = 0;
3697
- let result = length ? funcs[i].apply(this, args) : args[0];
3698
- while (i + 1 < length) {
3699
- i += 1;
3700
- result = funcs[i].call(this, result);
3705
+ return null;
3706
+ }
3707
+ function resolveInputRef(input) {
3708
+ if (!input) {
3709
+ return null;
3710
+ }
3711
+ if (typeof input === "object" && "current" in input) {
3712
+ const refValue = input.current;
3713
+ if (isHTMLElement(refValue)) {
3714
+ return refValue;
3701
3715
  }
3702
- return result;
3703
- };
3716
+ return null;
3717
+ }
3718
+ if (isHTMLElement(input)) {
3719
+ return input;
3720
+ }
3721
+ return null;
3704
3722
  }
3705
3723
 
3706
- // src/utils/isServer.ts
3707
- var isServer = !(typeof window !== "undefined" && window.document?.createElement);
3708
- var isServer_default = isServer;
3724
+ // src/core/maskEngine.ts
3725
+ var import_inputmask = __toESM(require_inputmask());
3709
3726
 
3710
- // src/utils/getMaskOptions.ts
3727
+ // src/core/maskConfig.ts
3711
3728
  function getMaskOptions(mask, _options) {
3712
3729
  const options = {
3713
3730
  jitMasking: false,
@@ -3769,7 +3786,6 @@ function getMaskOptions(mask, _options) {
3769
3786
  ...options
3770
3787
  },
3771
3788
  // alias for brazilians <3
3772
- // ty <3
3773
3789
  "brl-currency": {
3774
3790
  alias: "currency",
3775
3791
  prefix: "R$ ",
@@ -3797,6 +3813,31 @@ function getMaskOptions(mask, _options) {
3797
3813
  };
3798
3814
  }
3799
3815
 
3816
+ // src/utils/flow.ts
3817
+ function flow(...funcs) {
3818
+ const { length } = funcs;
3819
+ let index = length;
3820
+ while (index > 0) {
3821
+ index -= 1;
3822
+ if (typeof funcs[index] !== "function") {
3823
+ throw new TypeError("Expected a function");
3824
+ }
3825
+ }
3826
+ return (...args) => {
3827
+ let i = 0;
3828
+ let result = length ? funcs[i].apply(void 0, args) : args[0];
3829
+ while (i + 1 < length) {
3830
+ i += 1;
3831
+ result = funcs[i].call(void 0, result);
3832
+ }
3833
+ return result;
3834
+ };
3835
+ }
3836
+
3837
+ // src/utils/isServer.ts
3838
+ var isServer = !(typeof window !== "undefined" && window.document?.createElement);
3839
+ var isServer_default = isServer;
3840
+
3800
3841
  // src/utils/moduleInterop.ts
3801
3842
  function interopDefaultSync(module) {
3802
3843
  if (typeof module === "object" && module !== null) {
@@ -3808,75 +3849,107 @@ function interopDefaultSync(module) {
3808
3849
  return module;
3809
3850
  }
3810
3851
 
3811
- // src/withHookFormMask.ts
3812
- function withHookFormMask(register, mask, options) {
3813
- let newRef;
3814
- if (register) {
3815
- const { ref } = register;
3816
- const maskInput = interopDefaultSync(import_inputmask.default)(getMaskOptions(mask, options));
3817
- newRef = flow((_ref) => {
3818
- if (_ref) maskInput.mask(_ref);
3819
- return _ref;
3820
- }, ref);
3852
+ // src/core/maskEngine.ts
3853
+ function createMaskInstance(mask, options) {
3854
+ const inputmaskInstance = interopDefaultSync(import_inputmask.default);
3855
+ return inputmaskInstance(getMaskOptions(mask, options));
3856
+ }
3857
+ function applyMaskToElement(element, mask, options) {
3858
+ if (!element) return;
3859
+ const maskInstance = createMaskInstance(mask, options);
3860
+ const inputElement = element.nodeName === "INPUT" ? element : element.querySelector("input");
3861
+ if (inputElement) {
3862
+ maskInstance.mask(inputElement);
3863
+ } else {
3864
+ maskInstance.mask(element);
3821
3865
  }
3822
- return {
3823
- ...register,
3824
- ref: newRef
3825
- };
3826
3866
  }
3827
3867
 
3828
- // src/withMask.ts
3829
- var import_inputmask2 = __toESM(require_inputmask());
3830
- function withMask(mask, options) {
3868
+ // src/api/useMaskInput.ts
3869
+ function useMaskInput(props) {
3870
+ const { mask, register, options } = props;
3871
+ const ref = react.useRef(null);
3872
+ const maskInput = react.useMemo(() => createMaskInstance(mask, options), [mask, options]);
3873
+ if (isServer_default) {
3874
+ return () => {
3875
+ };
3876
+ }
3877
+ react.useEffect(() => {
3878
+ if (isServer_default || !ref.current) return;
3879
+ if (!isHTMLElement(ref.current)) {
3880
+ return;
3881
+ }
3882
+ const inputElement = findInputElement(ref.current);
3883
+ if (inputElement && isHTMLElement(inputElement)) {
3884
+ maskInput.mask(inputElement);
3885
+ }
3886
+ if (register && isHTMLElement(ref.current)) {
3887
+ register(ref.current);
3888
+ }
3889
+ }, [mask, register, options, maskInput, ref]);
3831
3890
  return (input) => {
3832
- if (isServer_default || mask === null || !input) return;
3833
- const maskInput = interopDefaultSync(import_inputmask2.default)(getMaskOptions(mask, options));
3834
- maskInput.mask(input);
3891
+ if (!input) {
3892
+ ref.current = null;
3893
+ return;
3894
+ }
3895
+ ref.current = resolveInputRef(input);
3835
3896
  };
3836
3897
  }
3837
3898
 
3838
- // src/useHookFormMask.ts
3839
- var import_inputmask3 = __toESM(require_inputmask());
3899
+ // src/api/useHookFormMask.ts
3840
3900
  function useHookFormMask(registerFn) {
3841
3901
  return (fieldName, mask, options) => {
3842
3902
  if (!registerFn) throw new Error("registerFn is required");
3843
- const { ref, ...restRegister } = registerFn(fieldName, options);
3844
- const maskInput = interopDefaultSync(import_inputmask3.default)(getMaskOptions(mask, options));
3845
- const newRef = flow((_ref) => {
3846
- if (_ref) {
3847
- const { nodeName } = _ref;
3848
- if (nodeName !== "INPUT") {
3849
- maskInput.mask(_ref.querySelector("input"));
3850
- } else {
3851
- maskInput.mask(_ref);
3852
- }
3853
- }
3903
+ const registerReturn = registerFn(fieldName, options);
3904
+ const { ref } = registerReturn;
3905
+ const applyMaskToRef = (_ref) => {
3906
+ if (_ref) applyMaskToElement(_ref, mask, options);
3854
3907
  return _ref;
3855
- }, ref);
3856
- return {
3857
- ...restRegister,
3858
- ref: newRef
3859
3908
  };
3909
+ const refWithMask = ref ? flow(applyMaskToRef, ref) : applyMaskToRef;
3910
+ const result = {
3911
+ ...registerReturn,
3912
+ ref: refWithMask
3913
+ };
3914
+ Object.defineProperty(result, "prevRef", {
3915
+ value: ref,
3916
+ enumerable: false,
3917
+ writable: true,
3918
+ configurable: true
3919
+ });
3920
+ return result;
3860
3921
  };
3861
3922
  }
3862
3923
 
3863
- // src/useMaskInput.ts
3864
- var import_inputmask4 = __toESM(require_inputmask());
3865
- function useInputMask(props) {
3866
- const { mask, register, options } = props;
3867
- const ref = react.useRef(null);
3868
- if (isServer_default) return ref;
3869
- react.useEffect(() => {
3870
- if (!isServer_default && ref.current) {
3871
- if (!ref.current) return;
3872
- const maskInput = interopDefaultSync(import_inputmask4.default)(getMaskOptions(mask, options));
3873
- maskInput.mask(ref.current);
3874
- if (register && ref.current) {
3875
- register(ref.current);
3876
- }
3877
- }
3878
- }, [mask, register, options]);
3879
- return ref;
3924
+ // src/api/withMask.ts
3925
+ var import_inputmask2 = __toESM(require_inputmask());
3926
+ function withMask(mask, options) {
3927
+ return (input) => {
3928
+ if (isServer_default || mask === null || !input) return;
3929
+ const maskInput = interopDefaultSync(import_inputmask2.default)(getMaskOptions(mask, options));
3930
+ maskInput.mask(input);
3931
+ };
3932
+ }
3933
+
3934
+ // src/api/withHookFormMask.ts
3935
+ function withHookFormMask(register, mask, options) {
3936
+ const { ref } = register;
3937
+ const applyMaskToRef = (_ref) => {
3938
+ if (_ref) applyMaskToElement(_ref, mask, options);
3939
+ return _ref;
3940
+ };
3941
+ const refWithMask = ref === null ? null : ref ? flow(applyMaskToRef, ref) : null;
3942
+ const result = {
3943
+ ...register,
3944
+ ref: refWithMask
3945
+ };
3946
+ Object.defineProperty(result, "prevRef", {
3947
+ value: ref,
3948
+ enumerable: false,
3949
+ writable: true,
3950
+ configurable: true
3951
+ });
3952
+ return result;
3880
3953
  }
3881
3954
  /*! Bundled license information:
3882
3955
 
@@ -3892,7 +3965,7 @@ inputmask/dist/inputmask.js:
3892
3965
  */
3893
3966
 
3894
3967
  exports.useHookFormMask = useHookFormMask;
3895
- exports.useMaskInput = useInputMask;
3968
+ exports.useMaskInput = useMaskInput;
3896
3969
  exports.withHookFormMask = withHookFormMask;
3897
3970
  exports.withMask = withMask;
3898
3971
  //# sourceMappingURL=index.cjs.map