|
@@ -4,39 +4,44 @@ export default function (Alpine) {
|
|
|
let templateFn = () => expression
|
|
|
let lastInputValue = ''
|
|
|
|
|
|
- if (['function', 'dynamic'].includes(value)) {
|
|
|
- // This is an x-mask:function directive.
|
|
|
-
|
|
|
- let evaluator = evaluateLater(expression)
|
|
|
-
|
|
|
- effect(() => {
|
|
|
- templateFn = input => {
|
|
|
- let result
|
|
|
-
|
|
|
- // We need to prevent "auto-evaluation" of functions like
|
|
|
- // x-on expressions do so that we can use them as mask functions.
|
|
|
- Alpine.dontAutoEvaluateFunctions(() => {
|
|
|
- evaluator(value => {
|
|
|
- result = typeof value === 'function' ? value(input) : value
|
|
|
- }, { scope: {
|
|
|
- // These are "magics" we'll make available to the x-mask:function:
|
|
|
- '$input': input,
|
|
|
- '$money': formatMoney.bind({ el }),
|
|
|
- }})
|
|
|
- })
|
|
|
-
|
|
|
- return result
|
|
|
- }
|
|
|
-
|
|
|
- // Run on initialize which serves a dual purpose:
|
|
|
- // - Initializing the mask on the input if it has an initial value.
|
|
|
- // - Running the template function to set up reactivity, so that
|
|
|
- // when a dependency inside it changes, the input re-masks.
|
|
|
+ queueMicrotask(() => {
|
|
|
+ if (['function', 'dynamic'].includes(value)) {
|
|
|
+ // This is an x-mask:function directive.
|
|
|
+
|
|
|
+ let evaluator = evaluateLater(expression)
|
|
|
+
|
|
|
+ effect(() => {
|
|
|
+ templateFn = input => {
|
|
|
+ let result
|
|
|
+
|
|
|
+ // We need to prevent "auto-evaluation" of functions like
|
|
|
+ // x-on expressions do so that we can use them as mask functions.
|
|
|
+ Alpine.dontAutoEvaluateFunctions(() => {
|
|
|
+ evaluator(value => {
|
|
|
+ result = typeof value === 'function' ? value(input) : value
|
|
|
+ }, { scope: {
|
|
|
+ // These are "magics" we'll make available to the x-mask:function:
|
|
|
+ '$input': input,
|
|
|
+ '$money': formatMoney.bind({ el }),
|
|
|
+ }})
|
|
|
+ })
|
|
|
+
|
|
|
+ return result
|
|
|
+ }
|
|
|
+
|
|
|
+ // Run on initialize which serves a dual purpose:
|
|
|
+ // - Initializing the mask on the input if it has an initial value.
|
|
|
+ // - Running the template function to set up reactivity, so that
|
|
|
+ // when a dependency inside it changes, the input re-masks.
|
|
|
+ processInputValue(el)
|
|
|
+ })
|
|
|
+ } else {
|
|
|
processInputValue(el)
|
|
|
- })
|
|
|
- } else {
|
|
|
- processInputValue(el)
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Override x-model's initial value...
|
|
|
+ if (el._x_model) el._x_model.set(el.value)
|
|
|
+ })
|
|
|
|
|
|
el.addEventListener('input', () => processInputValue(el))
|
|
|
// Don't "restoreCursorPosition" on "blur", because Safari
|
|
@@ -56,7 +61,9 @@ export default function (Alpine) {
|
|
|
return lastInputValue = el.value
|
|
|
}
|
|
|
|
|
|
- let setInput = () => { lastInputValue = el.value = formatInput(input, template) }
|
|
|
+ let setInput = () => {
|
|
|
+ lastInputValue = el.value = formatInput(input, template)
|
|
|
+ }
|
|
|
|
|
|
if (shouldRestoreCursor) {
|
|
|
// When an input element's value is set, it moves the cursor to the end
|
|
@@ -79,7 +86,7 @@ export default function (Alpine) {
|
|
|
|
|
|
return rebuiltInput
|
|
|
}
|
|
|
- })
|
|
|
+ }).before('model')
|
|
|
}
|
|
|
|
|
|
export function restoreCursorPosition(el, template, callback) {
|