use-mask-input 3.11.1 → 3.12.0
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/CHANGELOG.md +32 -0
- package/README.md +102 -1
- package/dist/antd.cjs +1 -1
- package/dist/antd.cjs.map +1 -1
- package/dist/antd.d.cts +1 -1
- package/dist/antd.d.mts +1 -1
- package/dist/antd.mjs +1 -1
- package/dist/antd.mjs.map +1 -1
- package/dist/{withMask-BQpBm8zG.cjs → core-BopfTGvA.cjs} +2 -2
- package/dist/core-BopfTGvA.cjs.map +1 -0
- package/dist/{withMask-DlC13MV7.mjs → core-CNGYEIpO.mjs} +2 -2
- package/dist/core-CNGYEIpO.mjs.map +1 -0
- package/dist/{index-HfKPnOg0.d.cts → index-CqBLkcNO.d.cts} +12 -3
- package/dist/{index-HfKPnOg0.d.mts → index-CqBLkcNO.d.mts} +12 -3
- package/dist/index-DAfluDSv.d.cts +25 -0
- package/dist/index-DGAwRzYx.d.mts +25 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -23
- package/dist/index.d.mts +2 -23
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/vue.cjs +2 -0
- package/dist/vue.cjs.map +1 -0
- package/dist/vue.d.cts +92 -0
- package/dist/vue.d.mts +92 -0
- package/dist/vue.mjs +2 -0
- package/dist/vue.mjs.map +1 -0
- package/dist/withMask--iSmiRbB.cjs +2 -0
- package/dist/withMask--iSmiRbB.cjs.map +1 -0
- package/dist/withMask-DOmJHTJ4.mjs +2 -0
- package/dist/withMask-DOmJHTJ4.mjs.map +1 -0
- package/package.json +50 -6
- package/src/@types/inputmask.d.ts +6 -0
- package/src/types/index.ts +6 -26
- package/src/types/mask.ts +37 -0
- package/src/vue/applyMask.ts +99 -0
- package/src/vue/directive.ts +59 -0
- package/src/vue/index.ts +13 -0
- package/src/vue/resolveVueElement.ts +34 -0
- package/src/vue/types.ts +35 -0
- package/src/vue/useMaskInput.ts +56 -0
- package/dist/withMask-BQpBm8zG.cjs.map +0 -1
- package/dist/withMask-DlC13MV7.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
+
## 3.12.0
|
|
2
|
+
|
|
3
|
+
### Minor Changes
|
|
4
|
+
|
|
5
|
+
- 0b5757d: Add Vue 3 support via a new `use-mask-input/vue` entry point.
|
|
6
|
+
|
|
7
|
+
Two exports: a `vMaskInput` directive and a `useMaskInput` composable. In `<script setup>` the directive needs no registration and no plugin — Vue resolves `vMaskInput` to `v-mask-input` on its own.
|
|
8
|
+
|
|
9
|
+
```vue
|
|
10
|
+
<script setup>
|
|
11
|
+
import { vMaskInput } from "use-mask-input/vue";
|
|
12
|
+
</script>
|
|
13
|
+
|
|
14
|
+
<template>
|
|
15
|
+
<input v-mask-input="'cpf'" />
|
|
16
|
+
<input
|
|
17
|
+
v-model="cpf"
|
|
18
|
+
v-mask-input="{ mask: 'cpf', options: { autoUnmask: true } }"
|
|
19
|
+
/>
|
|
20
|
+
</template>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
`v-model` works with no adapter, and vee-validate 4 needs no integration code at all: with `autoUnmask`, field state and submit values receive the raw value while the input keeps displaying the mask.
|
|
24
|
+
|
|
25
|
+
Also: `react` and `react-dom` are now optional peer dependencies, so installing the package in a Vue project no longer warns about missing React. `vue` and `vee-validate` are optional peers. The Vue entry never imports React.
|
|
26
|
+
|
|
27
|
+
### Patch Changes
|
|
28
|
+
|
|
29
|
+
- 0b5757d: Vue: binding `null` to `v-mask-input` now removes a mask that is already on the element.
|
|
30
|
+
|
|
31
|
+
The binding is reactive, so `v-mask-input="enabled ? 'cpf' : null"` reads as a toggle. Previously switching it off left the previous Inputmask instance attached and the field kept formatting.
|
|
32
|
+
|
|
1
33
|
## 3.11.1
|
|
2
34
|
|
|
3
35
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<div align="center">
|
|
2
2
|
<h1>🥸 use-mask-input</h1>
|
|
3
|
-
<p>Input masks for React
|
|
3
|
+
<p>Input masks for <strong>React</strong> and <strong>Vue 3</strong>. Works with React Hook Form, TanStack Form, vee-validate, Ant Design, and plain inputs.</p>
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/use-mask-input)
|
|
6
6
|
[](https://www.npmjs.com/package/use-mask-input)
|
|
@@ -108,6 +108,102 @@ function EmailInput() {
|
|
|
108
108
|
}
|
|
109
109
|
```
|
|
110
110
|
|
|
111
|
+
## Vue 3
|
|
112
|
+
|
|
113
|
+
Everything above is React. Vue 3 has its own entry point:
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
import { vMaskInput, useMaskInput } from 'use-mask-input/vue';
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### The directive
|
|
120
|
+
|
|
121
|
+
In `<script setup>`, importing `vMaskInput` is enough. Vue resolves a `vFoo` binding to `v-foo`, so there is no registration step and no plugin.
|
|
122
|
+
|
|
123
|
+
```vue
|
|
124
|
+
<script setup>
|
|
125
|
+
import { vMaskInput } from 'use-mask-input/vue';
|
|
126
|
+
</script>
|
|
127
|
+
|
|
128
|
+
<template>
|
|
129
|
+
<input v-mask-input="'cpf'" />
|
|
130
|
+
<input v-mask-input="'(99) 99999-9999'" />
|
|
131
|
+
<input v-mask-input="['999-999', '999-999-999']" />
|
|
132
|
+
<input v-mask-input="{ mask: 'currency', options: { prefix: 'R$ ' } }" />
|
|
133
|
+
</template>
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Outside `<script setup>`, register it yourself: `app.directive('mask-input', vMaskInput)`.
|
|
137
|
+
|
|
138
|
+
### With `v-model`
|
|
139
|
+
|
|
140
|
+
`v-model` works with no extra code. Add `autoUnmask` and the bound value is the raw one, while the input keeps showing the mask:
|
|
141
|
+
|
|
142
|
+
```vue
|
|
143
|
+
<input v-model="cpf" v-mask-input="{ mask: 'cpf', options: { autoUnmask: true } }" />
|
|
144
|
+
<!-- displays 123.456.789-01, and cpf === '12345678901' -->
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Without `autoUnmask`, `v-model` receives the masked string. Directive order does not matter: `v-model` before or after `v-mask-input` behaves identically.
|
|
148
|
+
|
|
149
|
+
Binding `null` turns masking off and removes any mask already applied, so `v-mask-input="enabled ? 'cpf' : null"` works as a toggle.
|
|
150
|
+
|
|
151
|
+
### With vee-validate
|
|
152
|
+
|
|
153
|
+
No helper, no wrapper. `useField` plus `v-model` plus the directive is the whole integration, and `handleSubmit` receives the unmasked value:
|
|
154
|
+
|
|
155
|
+
```vue
|
|
156
|
+
<script setup>
|
|
157
|
+
import { useField, useForm } from 'vee-validate';
|
|
158
|
+
import { vMaskInput } from 'use-mask-input/vue';
|
|
159
|
+
|
|
160
|
+
const { handleSubmit } = useForm();
|
|
161
|
+
const { value, errorMessage } = useField('cpf', (v) => v?.length === 11 || 'Invalid CPF');
|
|
162
|
+
|
|
163
|
+
const onSubmit = handleSubmit((values) => console.log(values)); // { cpf: '12345678901' }
|
|
164
|
+
</script>
|
|
165
|
+
|
|
166
|
+
<template>
|
|
167
|
+
<form @submit="onSubmit">
|
|
168
|
+
<input v-model="value" v-mask-input="{ mask: 'cpf', options: { autoUnmask: true } }" />
|
|
169
|
+
<span>{{ errorMessage }}</span>
|
|
170
|
+
</form>
|
|
171
|
+
</template>
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### With component libraries
|
|
175
|
+
|
|
176
|
+
Put the directive on the component. Vue applies it to the root element and the mask finds the inner input, so PrimeVue, Element Plus and Ant Design Vue work as-is:
|
|
177
|
+
|
|
178
|
+
```vue
|
|
179
|
+
<InputText v-mask-input="'cpf'" />
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### The composable
|
|
183
|
+
|
|
184
|
+
For imperative reads, or when you want the raw value without `autoUnmask`:
|
|
185
|
+
|
|
186
|
+
```vue
|
|
187
|
+
<script setup>
|
|
188
|
+
import { useMaskInput } from 'use-mask-input/vue';
|
|
189
|
+
|
|
190
|
+
const { maskRef, unmaskedValue } = useMaskInput('cpf');
|
|
191
|
+
const submit = () => console.log(unmaskedValue());
|
|
192
|
+
</script>
|
|
193
|
+
|
|
194
|
+
<template>
|
|
195
|
+
<input :ref="maskRef" />
|
|
196
|
+
</template>
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### Vue caveats
|
|
200
|
+
|
|
201
|
+
- **`unmaskedValue()` is not reactive.** `{{ unmaskedValue() }}` renders once and never updates, because reading the DOM registers no reactive dependency. Use it in event handlers and imperative code. For a value the template tracks, use `v-model` with `autoUnmask: true`.
|
|
202
|
+
- **`noValuePatching: true` is unsupported.** It disables the property accessor that the whole `v-model` integration relies on.
|
|
203
|
+
- **Replace the options object, don't mutate it.** Options are compared shallowly, so an in-place mutation may not re-apply the mask.
|
|
204
|
+
|
|
205
|
+
There is deliberately no `<MaskInput>` component and no vee-validate helper, because `v-model` plus the directive already covers both.
|
|
206
|
+
|
|
111
207
|
## APIs
|
|
112
208
|
|
|
113
209
|
| API | Description |
|
|
@@ -122,6 +218,8 @@ function EmailInput() {
|
|
|
122
218
|
| `useHookFormMaskAntd` | Hook. `useHookFormMask` for Ant Design. |
|
|
123
219
|
| `formatWithMask` | Function. Formats a raw value using a mask, without a mounted element. |
|
|
124
220
|
| `unformatWithMask` | Function. Removes the mask from a formatted value, without a mounted element. |
|
|
221
|
+
| `vMaskInput` | Vue directive. `use-mask-input/vue`. The Vue default choice. |
|
|
222
|
+
| `useMaskInput` (Vue) | Composable. `use-mask-input/vue`. Returns `{ maskRef, unmaskedValue }`. |
|
|
125
223
|
|
|
126
224
|
## Built-in Aliases
|
|
127
225
|
|
|
@@ -134,6 +232,9 @@ function EmailInput() {
|
|
|
134
232
|
- Ant Design (`use-mask-input/antd`)
|
|
135
233
|
- React Final Form
|
|
136
234
|
- Next.js / SSR
|
|
235
|
+
- **Vue 3** (`use-mask-input/vue`)
|
|
236
|
+
- **vee-validate** (no adapter needed)
|
|
237
|
+
- Vue component libraries: PrimeVue, Element Plus, Ant Design Vue
|
|
137
238
|
|
|
138
239
|
## License
|
|
139
240
|
|
package/dist/antd.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./core-BopfTGvA.cjs"),t=require("./withMask--iSmiRbB.cjs");let n=require("react");function r(t){return(0,n.useMemo)(()=>{let n=new Map;return(r,i,a)=>{if(!t)throw Error(`registerFn is required`);let o=t(r,a),{ref:s}=o,c=e.o(r,i);n.has(c)||n.set(c,t=>{let n=t?e.h(t.input):null;n&&e.t(n,i,a),s&&s(n)});let l={...o,ref:n.get(c)};return e.s(l,s),l}},[t])}function i(r){let{mask:i,register:a,options:o}=r,s=(0,n.useRef)(null),c=(0,n.useRef)(i),l=(0,n.useRef)(o),u=(0,n.useRef)(null),d=(0,n.useCallback)(()=>e.a(s.current),[]);c.current=i,l.current=o;let f=(0,n.useCallback)(n=>{if(!n){s.current=null;return}s.current=e.h(n.input),s.current&&s.current!==u.current&&(t.t(c.current,l.current)(s.current),u.current=s.current)},[]);return(0,n.useEffect)(()=>{e.u||!s.current||!a||a(s.current)},[a]),e.u?e.c((()=>{}),()=>``):e.c(f,d)}exports.useHookFormMaskAntd=r,exports.useMaskInputAntd=i;
|
|
2
2
|
//# sourceMappingURL=antd.cjs.map
|
package/dist/antd.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"antd.cjs","names":["makeMaskCacheKey","resolveInputRef","getUnmaskedValue","resolveInputRef","isServer","setUnmaskedValue"],"sources":["../src/antd/useHookFormMaskAntd.ts","../src/antd/useMaskInputAntd.ts"],"sourcesContent":["import { useMemo } from 'react';\n\nimport { applyMaskToElement, resolveInputRef } from '../core';\nimport { makeMaskCacheKey, setPrevRef } from '../utils';\n\nimport type { InputRef } from 'antd';\nimport type { RefCallback } from 'react';\nimport type {\n FieldValues, Path,\n RegisterOptions,\n UseFormRegister,\n} from 'react-hook-form';\n\nimport type { Mask, Options, UseHookFormMaskReturn } from '../types';\n\nexport type UseHookFormMaskAntdReturn<T extends FieldValues> = Omit<\n UseHookFormMaskReturn<T>,\n 'ref'\n> & { ref: RefCallback<InputRef | null> };\n\n/**\n * Ant Design version of useHookFormMask.\n * Creates a masked register that works with Ant Design Input (ref receives InputRef).\n *\n * @template T - The form data type\n * @template D - The register options type\n * @param registerFn - The register function from useForm hook\n * @returns A function that registers a field with mask support for Ant Design Input\n */\nexport default function useHookFormMaskAntd<\n T extends FieldValues, D extends RegisterOptions,\n>(registerFn: UseFormRegister<T>): ((fieldName: Path<T>, mask: Mask, options?: (\n D & Options) | Options | D) => UseHookFormMaskAntdReturn<T>) {\n //\n return useMemo(() => {\n const refCache = new Map<string, RefCallback<InputRef | null>>();\n\n return (fieldName: Path<T>, mask: Mask, options?: (\n D & Options) | Options | D): UseHookFormMaskAntdReturn<T> => {\n if (!registerFn) throw new Error('registerFn is required');\n\n const registerReturn = registerFn(fieldName, options as Options);\n const { ref } = registerReturn as UseHookFormMaskReturn<T>;\n\n const cacheKey = makeMaskCacheKey(fieldName, mask);\n\n if (!refCache.has(cacheKey)) {\n const refWithMask: RefCallback<InputRef | null> = (inputRef) => {\n const element = inputRef ? resolveInputRef(inputRef.input) : null;\n if (element) applyMaskToElement(element, mask, options as Options);\n if (ref) ref(element);\n };\n refCache.set(cacheKey, refWithMask);\n }\n\n const result = {\n ...registerReturn,\n ref: refCache.get(cacheKey),\n } as UseHookFormMaskAntdReturn<T>;\n\n setPrevRef(result, ref);\n\n return result;\n };\n }, [registerFn]);\n}\n","import { useCallback, useEffect, useRef } from 'react';\n\nimport withMask from '../api/withMask';\nimport { resolveInputRef } from '../core';\nimport isServer from '../utils/isServer';\nimport { getUnmaskedValue, setUnmaskedValue } from '../utils';\n\nimport type { InputRef } from 'antd';\n\nimport type { Mask, Options, UnmaskedValueApi } from '../types';\n\ntype UseMaskInputAntdReturn = ((input: InputRef | null) => void) & UnmaskedValueApi;\n\ninterface UseMaskInputOptions {\n mask: Mask;\n register?: (element: HTMLElement) => void;\n options?: Options;\n}\n\n/**\n * React hook for applying input masks to Ant Design form elements.\n *\n * @param props - Configuration object\n * @param props.mask - The mask pattern to apply\n * @param props.register - Optional callback that receives the element\n * @param props.options - Optional mask configuration options\n * @returns A ref callback function to attach to the Ant Design Input element\n */\nexport default function useMaskInputAntd(props: UseMaskInputOptions): UseMaskInputAntdReturn {\n const { mask, register, options } = props;\n const ref = useRef<HTMLInputElement | null>(null);\n const maskRef = useRef(mask);\n const optionsRef = useRef(options);\n const maskedElementRef = useRef<HTMLInputElement | null>(null);\n const unmaskedValue = useCallback(() => getUnmaskedValue(ref.current), []);\n\n maskRef.current = mask;\n optionsRef.current = options;\n\n const refCallback = useCallback((input: InputRef | null): void => {\n if (!input) {\n ref.current = null;\n return;\n }\n\n ref.current = resolveInputRef(input.input);\n\n if (ref.current && ref.current !== maskedElementRef.current) {\n withMask(maskRef.current, optionsRef.current)(ref.current);\n maskedElementRef.current = ref.current;\n }\n }, []);\n\n useEffect(() => {\n if (isServer || !ref.current || !register) return;\n register(ref.current);\n }, [register]);\n\n if (isServer) {\n const noop = (() => {\n // server doesn't have dom, so just do nothing\n }) as unknown as UseMaskInputAntdReturn;\n\n return setUnmaskedValue(noop, () => '');\n }\n\n return setUnmaskedValue(refCallback as UseMaskInputAntdReturn, unmaskedValue);\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"antd.cjs","names":["makeMaskCacheKey","resolveInputRef","getUnmaskedValue","resolveInputRef","isServer","setUnmaskedValue"],"sources":["../src/antd/useHookFormMaskAntd.ts","../src/antd/useMaskInputAntd.ts"],"sourcesContent":["import { useMemo } from 'react';\n\nimport { applyMaskToElement, resolveInputRef } from '../core';\nimport { makeMaskCacheKey, setPrevRef } from '../utils';\n\nimport type { InputRef } from 'antd';\nimport type { RefCallback } from 'react';\nimport type {\n FieldValues, Path,\n RegisterOptions,\n UseFormRegister,\n} from 'react-hook-form';\n\nimport type { Mask, Options, UseHookFormMaskReturn } from '../types';\n\nexport type UseHookFormMaskAntdReturn<T extends FieldValues> = Omit<\n UseHookFormMaskReturn<T>,\n 'ref'\n> & { ref: RefCallback<InputRef | null> };\n\n/**\n * Ant Design version of useHookFormMask.\n * Creates a masked register that works with Ant Design Input (ref receives InputRef).\n *\n * @template T - The form data type\n * @template D - The register options type\n * @param registerFn - The register function from useForm hook\n * @returns A function that registers a field with mask support for Ant Design Input\n */\nexport default function useHookFormMaskAntd<\n T extends FieldValues, D extends RegisterOptions,\n>(registerFn: UseFormRegister<T>): ((fieldName: Path<T>, mask: Mask, options?: (\n D & Options) | Options | D) => UseHookFormMaskAntdReturn<T>) {\n //\n return useMemo(() => {\n const refCache = new Map<string, RefCallback<InputRef | null>>();\n\n return (fieldName: Path<T>, mask: Mask, options?: (\n D & Options) | Options | D): UseHookFormMaskAntdReturn<T> => {\n if (!registerFn) throw new Error('registerFn is required');\n\n const registerReturn = registerFn(fieldName, options as Options);\n const { ref } = registerReturn as UseHookFormMaskReturn<T>;\n\n const cacheKey = makeMaskCacheKey(fieldName, mask);\n\n if (!refCache.has(cacheKey)) {\n const refWithMask: RefCallback<InputRef | null> = (inputRef) => {\n const element = inputRef ? resolveInputRef(inputRef.input) : null;\n if (element) applyMaskToElement(element, mask, options as Options);\n if (ref) ref(element);\n };\n refCache.set(cacheKey, refWithMask);\n }\n\n const result = {\n ...registerReturn,\n ref: refCache.get(cacheKey),\n } as UseHookFormMaskAntdReturn<T>;\n\n setPrevRef(result, ref);\n\n return result;\n };\n }, [registerFn]);\n}\n","import { useCallback, useEffect, useRef } from 'react';\n\nimport withMask from '../api/withMask';\nimport { resolveInputRef } from '../core';\nimport isServer from '../utils/isServer';\nimport { getUnmaskedValue, setUnmaskedValue } from '../utils';\n\nimport type { InputRef } from 'antd';\n\nimport type { Mask, Options, UnmaskedValueApi } from '../types';\n\ntype UseMaskInputAntdReturn = ((input: InputRef | null) => void) & UnmaskedValueApi;\n\ninterface UseMaskInputOptions {\n mask: Mask;\n register?: (element: HTMLElement) => void;\n options?: Options;\n}\n\n/**\n * React hook for applying input masks to Ant Design form elements.\n *\n * @param props - Configuration object\n * @param props.mask - The mask pattern to apply\n * @param props.register - Optional callback that receives the element\n * @param props.options - Optional mask configuration options\n * @returns A ref callback function to attach to the Ant Design Input element\n */\nexport default function useMaskInputAntd(props: UseMaskInputOptions): UseMaskInputAntdReturn {\n const { mask, register, options } = props;\n const ref = useRef<HTMLInputElement | null>(null);\n const maskRef = useRef(mask);\n const optionsRef = useRef(options);\n const maskedElementRef = useRef<HTMLInputElement | null>(null);\n const unmaskedValue = useCallback(() => getUnmaskedValue(ref.current), []);\n\n maskRef.current = mask;\n optionsRef.current = options;\n\n const refCallback = useCallback((input: InputRef | null): void => {\n if (!input) {\n ref.current = null;\n return;\n }\n\n ref.current = resolveInputRef(input.input);\n\n if (ref.current && ref.current !== maskedElementRef.current) {\n withMask(maskRef.current, optionsRef.current)(ref.current);\n maskedElementRef.current = ref.current;\n }\n }, []);\n\n useEffect(() => {\n if (isServer || !ref.current || !register) return;\n register(ref.current);\n }, [register]);\n\n if (isServer) {\n const noop = (() => {\n // server doesn't have dom, so just do nothing\n }) as unknown as UseMaskInputAntdReturn;\n\n return setUnmaskedValue(noop, () => '');\n }\n\n return setUnmaskedValue(refCallback as UseMaskInputAntdReturn, unmaskedValue);\n}\n"],"mappings":"sKA6BA,SAAwB,EAEtB,EAC6D,CAE7D,OAAA,EAAA,EAAA,QAAA,KAAqB,CACnB,IAAM,EAAW,IAAI,IAErB,OAAQ,EAAoB,EAAY,IACuB,CAC7D,GAAI,CAAC,EAAY,MAAU,MAAM,wBAAwB,EAEzD,IAAM,EAAiB,EAAW,EAAW,CAAkB,EACzD,CAAE,OAAQ,EAEV,EAAWA,EAAAA,EAAiB,EAAW,CAAI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AuC,CAAa,CAC9E"}
|
package/dist/antd.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { c as UnmaskedValueApi, d as UseFormRegister, l as RegisterOptions, n as UseHookFormMaskReturn, o as Mask, p as Path, s as Options, u as FieldValues } from "./index-CqBLkcNO.cjs";
|
|
2
2
|
import { InputRef } from "antd";
|
|
3
3
|
import { RefCallback } from "react";
|
|
4
4
|
//#region src/antd/useHookFormMaskAntd.d.ts
|
package/dist/antd.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { c as UnmaskedValueApi, d as UseFormRegister, l as RegisterOptions, n as UseHookFormMaskReturn, o as Mask, p as Path, s as Options, u as FieldValues } from "./index-CqBLkcNO.mjs";
|
|
2
2
|
import { RefCallback } from "react";
|
|
3
3
|
import { InputRef } from "antd";
|
|
4
4
|
//#region src/antd/useHookFormMaskAntd.d.ts
|
package/dist/antd.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{a as e,c as t,
|
|
1
|
+
import{a as e,c as t,h as n,o as r,s as i,t as a,u as o}from"./core-CNGYEIpO.mjs";import{t as s}from"./withMask-DOmJHTJ4.mjs";import{useCallback as c,useEffect as l,useMemo as u,useRef as d}from"react";function f(e){return u(()=>{let t=new Map;return(o,s,c)=>{if(!e)throw Error(`registerFn is required`);let l=e(o,c),{ref:u}=l,d=r(o,s);t.has(d)||t.set(d,e=>{let t=e?n(e.input):null;t&&a(t,s,c),u&&u(t)});let f={...l,ref:t.get(d)};return i(f,u),f}},[e])}function p(r){let{mask:i,register:a,options:u}=r,f=d(null),p=d(i),m=d(u),h=d(null),g=c(()=>e(f.current),[]);p.current=i,m.current=u;let _=c(e=>{if(!e){f.current=null;return}f.current=n(e.input),f.current&&f.current!==h.current&&(s(p.current,m.current)(f.current),h.current=f.current)},[]);return l(()=>{o||!f.current||!a||a(f.current)},[a]),o?t((()=>{}),()=>``):t(_,g)}export{f as useHookFormMaskAntd,p as useMaskInputAntd};
|
|
2
2
|
//# sourceMappingURL=antd.mjs.map
|
package/dist/antd.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"antd.mjs","names":[],"sources":["../src/antd/useHookFormMaskAntd.ts","../src/antd/useMaskInputAntd.ts"],"sourcesContent":["import { useMemo } from 'react';\n\nimport { applyMaskToElement, resolveInputRef } from '../core';\nimport { makeMaskCacheKey, setPrevRef } from '../utils';\n\nimport type { InputRef } from 'antd';\nimport type { RefCallback } from 'react';\nimport type {\n FieldValues, Path,\n RegisterOptions,\n UseFormRegister,\n} from 'react-hook-form';\n\nimport type { Mask, Options, UseHookFormMaskReturn } from '../types';\n\nexport type UseHookFormMaskAntdReturn<T extends FieldValues> = Omit<\n UseHookFormMaskReturn<T>,\n 'ref'\n> & { ref: RefCallback<InputRef | null> };\n\n/**\n * Ant Design version of useHookFormMask.\n * Creates a masked register that works with Ant Design Input (ref receives InputRef).\n *\n * @template T - The form data type\n * @template D - The register options type\n * @param registerFn - The register function from useForm hook\n * @returns A function that registers a field with mask support for Ant Design Input\n */\nexport default function useHookFormMaskAntd<\n T extends FieldValues, D extends RegisterOptions,\n>(registerFn: UseFormRegister<T>): ((fieldName: Path<T>, mask: Mask, options?: (\n D & Options) | Options | D) => UseHookFormMaskAntdReturn<T>) {\n //\n return useMemo(() => {\n const refCache = new Map<string, RefCallback<InputRef | null>>();\n\n return (fieldName: Path<T>, mask: Mask, options?: (\n D & Options) | Options | D): UseHookFormMaskAntdReturn<T> => {\n if (!registerFn) throw new Error('registerFn is required');\n\n const registerReturn = registerFn(fieldName, options as Options);\n const { ref } = registerReturn as UseHookFormMaskReturn<T>;\n\n const cacheKey = makeMaskCacheKey(fieldName, mask);\n\n if (!refCache.has(cacheKey)) {\n const refWithMask: RefCallback<InputRef | null> = (inputRef) => {\n const element = inputRef ? resolveInputRef(inputRef.input) : null;\n if (element) applyMaskToElement(element, mask, options as Options);\n if (ref) ref(element);\n };\n refCache.set(cacheKey, refWithMask);\n }\n\n const result = {\n ...registerReturn,\n ref: refCache.get(cacheKey),\n } as UseHookFormMaskAntdReturn<T>;\n\n setPrevRef(result, ref);\n\n return result;\n };\n }, [registerFn]);\n}\n","import { useCallback, useEffect, useRef } from 'react';\n\nimport withMask from '../api/withMask';\nimport { resolveInputRef } from '../core';\nimport isServer from '../utils/isServer';\nimport { getUnmaskedValue, setUnmaskedValue } from '../utils';\n\nimport type { InputRef } from 'antd';\n\nimport type { Mask, Options, UnmaskedValueApi } from '../types';\n\ntype UseMaskInputAntdReturn = ((input: InputRef | null) => void) & UnmaskedValueApi;\n\ninterface UseMaskInputOptions {\n mask: Mask;\n register?: (element: HTMLElement) => void;\n options?: Options;\n}\n\n/**\n * React hook for applying input masks to Ant Design form elements.\n *\n * @param props - Configuration object\n * @param props.mask - The mask pattern to apply\n * @param props.register - Optional callback that receives the element\n * @param props.options - Optional mask configuration options\n * @returns A ref callback function to attach to the Ant Design Input element\n */\nexport default function useMaskInputAntd(props: UseMaskInputOptions): UseMaskInputAntdReturn {\n const { mask, register, options } = props;\n const ref = useRef<HTMLInputElement | null>(null);\n const maskRef = useRef(mask);\n const optionsRef = useRef(options);\n const maskedElementRef = useRef<HTMLInputElement | null>(null);\n const unmaskedValue = useCallback(() => getUnmaskedValue(ref.current), []);\n\n maskRef.current = mask;\n optionsRef.current = options;\n\n const refCallback = useCallback((input: InputRef | null): void => {\n if (!input) {\n ref.current = null;\n return;\n }\n\n ref.current = resolveInputRef(input.input);\n\n if (ref.current && ref.current !== maskedElementRef.current) {\n withMask(maskRef.current, optionsRef.current)(ref.current);\n maskedElementRef.current = ref.current;\n }\n }, []);\n\n useEffect(() => {\n if (isServer || !ref.current || !register) return;\n register(ref.current);\n }, [register]);\n\n if (isServer) {\n const noop = (() => {\n // server doesn't have dom, so just do nothing\n }) as unknown as UseMaskInputAntdReturn;\n\n return setUnmaskedValue(noop, () => '');\n }\n\n return setUnmaskedValue(refCallback as UseMaskInputAntdReturn, unmaskedValue);\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"antd.mjs","names":[],"sources":["../src/antd/useHookFormMaskAntd.ts","../src/antd/useMaskInputAntd.ts"],"sourcesContent":["import { useMemo } from 'react';\n\nimport { applyMaskToElement, resolveInputRef } from '../core';\nimport { makeMaskCacheKey, setPrevRef } from '../utils';\n\nimport type { InputRef } from 'antd';\nimport type { RefCallback } from 'react';\nimport type {\n FieldValues, Path,\n RegisterOptions,\n UseFormRegister,\n} from 'react-hook-form';\n\nimport type { Mask, Options, UseHookFormMaskReturn } from '../types';\n\nexport type UseHookFormMaskAntdReturn<T extends FieldValues> = Omit<\n UseHookFormMaskReturn<T>,\n 'ref'\n> & { ref: RefCallback<InputRef | null> };\n\n/**\n * Ant Design version of useHookFormMask.\n * Creates a masked register that works with Ant Design Input (ref receives InputRef).\n *\n * @template T - The form data type\n * @template D - The register options type\n * @param registerFn - The register function from useForm hook\n * @returns A function that registers a field with mask support for Ant Design Input\n */\nexport default function useHookFormMaskAntd<\n T extends FieldValues, D extends RegisterOptions,\n>(registerFn: UseFormRegister<T>): ((fieldName: Path<T>, mask: Mask, options?: (\n D & Options) | Options | D) => UseHookFormMaskAntdReturn<T>) {\n //\n return useMemo(() => {\n const refCache = new Map<string, RefCallback<InputRef | null>>();\n\n return (fieldName: Path<T>, mask: Mask, options?: (\n D & Options) | Options | D): UseHookFormMaskAntdReturn<T> => {\n if (!registerFn) throw new Error('registerFn is required');\n\n const registerReturn = registerFn(fieldName, options as Options);\n const { ref } = registerReturn as UseHookFormMaskReturn<T>;\n\n const cacheKey = makeMaskCacheKey(fieldName, mask);\n\n if (!refCache.has(cacheKey)) {\n const refWithMask: RefCallback<InputRef | null> = (inputRef) => {\n const element = inputRef ? resolveInputRef(inputRef.input) : null;\n if (element) applyMaskToElement(element, mask, options as Options);\n if (ref) ref(element);\n };\n refCache.set(cacheKey, refWithMask);\n }\n\n const result = {\n ...registerReturn,\n ref: refCache.get(cacheKey),\n } as UseHookFormMaskAntdReturn<T>;\n\n setPrevRef(result, ref);\n\n return result;\n };\n }, [registerFn]);\n}\n","import { useCallback, useEffect, useRef } from 'react';\n\nimport withMask from '../api/withMask';\nimport { resolveInputRef } from '../core';\nimport isServer from '../utils/isServer';\nimport { getUnmaskedValue, setUnmaskedValue } from '../utils';\n\nimport type { InputRef } from 'antd';\n\nimport type { Mask, Options, UnmaskedValueApi } from '../types';\n\ntype UseMaskInputAntdReturn = ((input: InputRef | null) => void) & UnmaskedValueApi;\n\ninterface UseMaskInputOptions {\n mask: Mask;\n register?: (element: HTMLElement) => void;\n options?: Options;\n}\n\n/**\n * React hook for applying input masks to Ant Design form elements.\n *\n * @param props - Configuration object\n * @param props.mask - The mask pattern to apply\n * @param props.register - Optional callback that receives the element\n * @param props.options - Optional mask configuration options\n * @returns A ref callback function to attach to the Ant Design Input element\n */\nexport default function useMaskInputAntd(props: UseMaskInputOptions): UseMaskInputAntdReturn {\n const { mask, register, options } = props;\n const ref = useRef<HTMLInputElement | null>(null);\n const maskRef = useRef(mask);\n const optionsRef = useRef(options);\n const maskedElementRef = useRef<HTMLInputElement | null>(null);\n const unmaskedValue = useCallback(() => getUnmaskedValue(ref.current), []);\n\n maskRef.current = mask;\n optionsRef.current = options;\n\n const refCallback = useCallback((input: InputRef | null): void => {\n if (!input) {\n ref.current = null;\n return;\n }\n\n ref.current = resolveInputRef(input.input);\n\n if (ref.current && ref.current !== maskedElementRef.current) {\n withMask(maskRef.current, optionsRef.current)(ref.current);\n maskedElementRef.current = ref.current;\n }\n }, []);\n\n useEffect(() => {\n if (isServer || !ref.current || !register) return;\n register(ref.current);\n }, [register]);\n\n if (isServer) {\n const noop = (() => {\n // server doesn't have dom, so just do nothing\n }) as unknown as UseMaskInputAntdReturn;\n\n return setUnmaskedValue(noop, () => '');\n }\n\n return setUnmaskedValue(refCallback as UseMaskInputAntdReturn, unmaskedValue);\n}\n"],"mappings":"0MA6BA,SAAwB,EAEtB,E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