@eldrajs/ui 0.3.19 → 0.3.21
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 +6 -0
- package/README.md +63 -31
- package/dist/{PhoneInput-CHTpjbtS.js → PhoneInput-f6wxvduD.js} +1 -1
- package/dist/{VariantPicker-CrKQ7jpB.js → VariantPicker-CCci26If.js} +524 -311
- package/dist/components/autocomplete/Autocomplete.vue.d.ts +2 -0
- package/dist/components/chip/Chip.vue.d.ts +2 -0
- package/dist/components/currency-input/CurrencyInput.vue.d.ts +5 -1
- package/dist/components/dialog/Dialog.vue.d.ts +2 -0
- package/dist/components/drawer/Drawer.vue.d.ts +2 -0
- package/dist/components/filter-panel/FilterGroup.vue.d.ts +2 -0
- package/dist/components/filter-panel/FilterPanel.vue.d.ts +2 -0
- package/dist/components/filter-panel/facets/ColourFacet.vue.d.ts +2 -0
- package/dist/components/filter-panel/facets/ListFacet.vue.d.ts +2 -0
- package/dist/components/filter-panel/facets/RangeFacet.vue.d.ts +2 -0
- package/dist/components/filter-panel/facets/SizeFacet.vue.d.ts +2 -0
- package/dist/components/filter-panel/facets/ToggleFacet.vue.d.ts +2 -0
- package/dist/components/lightbox/Lightbox.vue.d.ts +2 -0
- package/dist/components/load-more/LoadMore.vue.d.ts +1 -1
- package/dist/components/nav-menu/NavMenu.vue.d.ts +2 -0
- package/dist/components/pagination/Pagination.vue.d.ts +2 -0
- package/dist/components/range-slider/RangeSlider.vue.d.ts +2 -0
- package/dist/components/search-bar/SearchBar.vue.d.ts +2 -0
- package/dist/components/search-modal/SearchModal.vue.d.ts +2 -0
- package/dist/components/select/MultiSelect.vue.d.ts +2 -0
- package/dist/components/select/Select.vue.d.ts +2 -0
- package/dist/components/site-form/SiteForm.vue.d.ts +2 -0
- package/dist/components/toast/Toast.vue.d.ts +2 -0
- package/dist/components/toast/Toaster.vue.d.ts +2 -0
- package/dist/components/unit-input/UnitInput.vue.d.ts +8 -0
- package/dist/components/unit-input/types.d.ts +13 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -3
- package/dist/messages/en-US.d.ts +4 -0
- package/dist/messages/is-IS.js +3 -1
- package/dist/phone/PhoneInput.vue.d.ts +2 -0
- package/dist/phone/vee-validate.js +3 -3
- package/dist/phone.js +1 -1
- package/dist/{useFieldControl-B1JvPEeM.js → useFieldControl-BnO3B37W.js} +17 -6
- package/dist/{useMessages-BdN9F1cW.js → useMessages-D8bTGX7c.js} +5 -1
- package/dist/utils/amount.d.ts +71 -0
- package/dist/vee-validate/useFieldControl.d.ts +6 -0
- package/dist/vee-validate.js +35 -5
- package/dist/vue-i18n.js +1 -1
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { $ as FIELD_KEY, A as ICON_STROKE_WIDTH$1, C as optionClass, D as optionSwatchClass, E as optionLabelClass, F as FIELD_CLEAR_BUTTON, G as FIELD_SEARCH_APPEARANCE, H as FIELD_PREFIX_PAD, I as FIELD_CLEAR_BUTTON_BOX, J as FIELD_SUFFIX_ROW_BASE, K as FIELD_SIZE, L as FIELD_CLEAR_SIZE, M as FIELD_ACTION_ROOM_REM, N as FIELD_BASE, O as panelClass, P as FIELD_BOX, Q as fieldState, S as listboxClass, T as optionIconClass, U as FIELD_PREFIX_ROW, V as FIELD_LIVE, W as FIELD_RADIUS, X as FIELD_TRAILING_PAD, Y as FIELD_TEXT, Z as FIELD_TRAILING_PAD_PAIR, b as TRIGGER_VALUE_CELL, d as useEldraUiLocale, et as mergeClass, f as Spinner_default, i as SelectPanel_default, j as FIELD_ACTION_PAIR_SIZE, k as ICON_SIZE_CLASS, n as matchRange, nt as joinIds, p as usePopover, q as FIELD_SUFFIX_ROW, r as useOptionList, rt as useUiId, tt as useControllableModel, w as optionHintClass, x as TRIGGER_VALUE_STACK, y as useListbox, z as FIELD_LEADING_ICON } from "./useOptionList-UMUMayec.js";
|
|
2
2
|
import { r as useEldraScope } from "./useEldraScope-BDbGzt7R.js";
|
|
3
|
-
import { r as useMessages } from "./useMessages-
|
|
3
|
+
import { r as useMessages } from "./useMessages-D8bTGX7c.js";
|
|
4
4
|
import { Comment, Fragment, Teleport, Text, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createSlots, createStaticVNode, createTextVNode, createVNode, defineComponent, getCurrentInstance, guardReactiveProps, h, inject, mergeProps, nextTick, normalizeClass, normalizeProps, normalizeStyle, onBeforeUnmount, onBeforeUpdate, onMounted, onUpdated, openBlock, provide, ref, renderList, renderSlot, resolveDynamicComponent, shallowRef, toDisplayString, toHandlers, toValue, unref, useAttrs, useSlots, watch, watchPostEffect, withCtx, withModifiers } from "vue";
|
|
5
5
|
import { numberFormat } from "@eldrajs/sdk/format";
|
|
6
6
|
// src/utils/mask.ts
|
|
@@ -2105,6 +2105,294 @@ function tagPadding(hasRemoveButton) {
|
|
|
2105
2105
|
function useHeadingTag(level) {
|
|
2106
2106
|
return computed(() => `h${toValue(level)}`);
|
|
2107
2107
|
}
|
|
2108
|
+
// src/utils/amount.ts
|
|
2109
|
+
var NOT_A_NUMBER = {
|
|
2110
|
+
kind: "invalid",
|
|
2111
|
+
reason: "notANumber"
|
|
2112
|
+
};
|
|
2113
|
+
var count = (text, mark) => text.split(mark).length - 1;
|
|
2114
|
+
var escaped = (mark) => mark === "." ? "\\." : mark;
|
|
2115
|
+
var grouped = (text, mark) => new RegExp(`^[1-9]\\d{0,2}(${escaped(mark)}\\d{3})+$`).test(text);
|
|
2116
|
+
var groupingSoFar = (text, mark) => new RegExp(`^[1-9]\\d{0,2}(${escaped(mark)}\\d{3})*${escaped(mark)}\\d{0,2}$`).test(text);
|
|
2117
|
+
var groupedWithFraction = (text, group, decimal) => new RegExp(`^[1-9]\\d{0,2}(${escaped(group)}\\d{3})+${escaped(decimal)}\\d*$`).test(text);
|
|
2118
|
+
function splitAmount(rest, digits, decimal) {
|
|
2119
|
+
const group = decimal === "," ? "." : ",";
|
|
2120
|
+
const at = (mark) => ({
|
|
2121
|
+
whole: rest.slice(0, rest.lastIndexOf(mark)),
|
|
2122
|
+
fraction: rest.slice(rest.lastIndexOf(mark) + 1),
|
|
2123
|
+
mark: true
|
|
2124
|
+
});
|
|
2125
|
+
const hasDecimal = rest.includes(decimal);
|
|
2126
|
+
const hasGroup = rest.includes(group);
|
|
2127
|
+
if (!hasDecimal && !hasGroup) return {
|
|
2128
|
+
whole: rest,
|
|
2129
|
+
fraction: "",
|
|
2130
|
+
mark: false
|
|
2131
|
+
};
|
|
2132
|
+
if (hasDecimal && !hasGroup) {
|
|
2133
|
+
if (digits === 0 && grouped(rest, decimal)) return {
|
|
2134
|
+
whole: rest,
|
|
2135
|
+
fraction: "",
|
|
2136
|
+
mark: false
|
|
2137
|
+
};
|
|
2138
|
+
return count(rest, decimal) === 1 ? at(decimal) : NOT_A_NUMBER;
|
|
2139
|
+
}
|
|
2140
|
+
if (!hasDecimal) {
|
|
2141
|
+
if (grouped(rest, group)) return {
|
|
2142
|
+
whole: rest,
|
|
2143
|
+
fraction: "",
|
|
2144
|
+
mark: false
|
|
2145
|
+
};
|
|
2146
|
+
const several = count(rest, group) > 1;
|
|
2147
|
+
if (groupingSoFar(rest, group) && (several || digits === 0) || !several && rest.endsWith(group)) return { kind: "pending" };
|
|
2148
|
+
if (several) return NOT_A_NUMBER;
|
|
2149
|
+
return /^\d*[.,]\d{1,2}$/.test(rest) ? at(group) : NOT_A_NUMBER;
|
|
2150
|
+
}
|
|
2151
|
+
if (groupedWithFraction(rest, group, decimal)) return at(decimal);
|
|
2152
|
+
if (groupedWithFraction(rest, decimal, group)) return rest.endsWith(group) ? { kind: "pending" } : at(group);
|
|
2153
|
+
return NOT_A_NUMBER;
|
|
2154
|
+
}
|
|
2155
|
+
/** Full-width digits and marks, which an East Asian IME composes, read as their ASCII forms. */
|
|
2156
|
+
function asciiDigits(text) {
|
|
2157
|
+
return text.replace(/[0-9]/g, (digit) => String.fromCharCode(digit.charCodeAt(0) - 65248)).replace(/./g, ".").replace(/,/g, ",");
|
|
2158
|
+
}
|
|
2159
|
+
/**
|
|
2160
|
+
* Reads a number field's whole text strictly in its locale: `decimal` is the locale's decimal
|
|
2161
|
+
* mark, `digits` how many fraction digits the field takes, `symbols` the currency or unit strings
|
|
2162
|
+
* its format prints (stripped wherever they are).
|
|
2163
|
+
*/
|
|
2164
|
+
function parseAmount(text, symbols, digits, decimal) {
|
|
2165
|
+
let rest = asciiDigits(text);
|
|
2166
|
+
for (const symbol of symbols) if (symbol) rest = rest.replaceAll(symbol, "");
|
|
2167
|
+
rest = rest.replace(/[\s\p{Sc}]/gu, "");
|
|
2168
|
+
if (rest === "") return { kind: "empty" };
|
|
2169
|
+
if (/[^\d.,]/.test(rest)) return NOT_A_NUMBER;
|
|
2170
|
+
if (!/\d/.test(rest)) return rest.length === 1 ? { kind: "pending" } : NOT_A_NUMBER;
|
|
2171
|
+
const split = splitAmount(rest, digits, decimal);
|
|
2172
|
+
if ("kind" in split) return split;
|
|
2173
|
+
const { fraction, mark } = split;
|
|
2174
|
+
let read = fraction;
|
|
2175
|
+
if (digits === 0 && fraction !== "") {
|
|
2176
|
+
if (/^0{1,2}$/.test(fraction)) read = "";
|
|
2177
|
+
else return /^\d{1,2}$/.test(fraction) ? {
|
|
2178
|
+
kind: "invalid",
|
|
2179
|
+
reason: "noDecimals"
|
|
2180
|
+
} : NOT_A_NUMBER;
|
|
2181
|
+
}
|
|
2182
|
+
if (read.length > digits) return NOT_A_NUMBER;
|
|
2183
|
+
const whole = split.whole.replace(/[.,]/g, "").replace(/^0+(?=\d)/, "") || "0";
|
|
2184
|
+
const value = Number(`${whole}.${read || "0"}`);
|
|
2185
|
+
if (!Number.isFinite(value) || value * 10 ** digits > Number.MAX_SAFE_INTEGER) return NOT_A_NUMBER;
|
|
2186
|
+
return {
|
|
2187
|
+
kind: "number",
|
|
2188
|
+
value,
|
|
2189
|
+
whole,
|
|
2190
|
+
fraction,
|
|
2191
|
+
mark
|
|
2192
|
+
};
|
|
2193
|
+
}
|
|
2194
|
+
/**
|
|
2195
|
+
* `parseAmount`, and when the locale cannot read the text, the other convention: what a paste,
|
|
2196
|
+
* a drop, autofill or dictation brings in whole (`1,000,000` into an Icelandic field is a
|
|
2197
|
+
* million, `1.000` into an English money field a thousand). A typed keystroke is read strictly.
|
|
2198
|
+
*/
|
|
2199
|
+
function readAmount(text, symbols, digits, decimal, lenient = false) {
|
|
2200
|
+
const strict = parseAmount(text, symbols, digits, decimal);
|
|
2201
|
+
if (!lenient || strict.kind !== "invalid" || strict.reason !== "notANumber") return strict;
|
|
2202
|
+
const other = parseAmount(text, symbols, digits, decimal === "," ? "." : ",");
|
|
2203
|
+
return other.kind === "number" ? other : strict;
|
|
2204
|
+
}
|
|
2205
|
+
var DIGIT = /\d/;
|
|
2206
|
+
var digitCount = (text) => [...text].filter((c) => DIGIT.test(c)).length;
|
|
2207
|
+
/** The text with every symbol blanked out, so a `.` in `kr.` is never read as a mark. */
|
|
2208
|
+
function masked(text, symbols) {
|
|
2209
|
+
let out = text;
|
|
2210
|
+
for (const symbol of symbols) if (symbol) out = out.replaceAll(symbol, " ".repeat(symbol.length));
|
|
2211
|
+
return out;
|
|
2212
|
+
}
|
|
2213
|
+
function createAmountEditor(options) {
|
|
2214
|
+
const { format, decimal, digits, max } = options;
|
|
2215
|
+
const symbols = (() => {
|
|
2216
|
+
return format(0).formatToParts(1e3).filter((part) => [
|
|
2217
|
+
"currency",
|
|
2218
|
+
"unit",
|
|
2219
|
+
"literal"
|
|
2220
|
+
].includes(part.type)).map((part) => part.value).sort((a, b) => b.length - a.length);
|
|
2221
|
+
})();
|
|
2222
|
+
function frame(wholeValue) {
|
|
2223
|
+
const parts = format(0).formatToParts(wholeValue);
|
|
2224
|
+
const first = parts.findIndex((p) => p.type === "integer");
|
|
2225
|
+
let last = -1;
|
|
2226
|
+
parts.forEach((p, i) => {
|
|
2227
|
+
if (p.type === "integer" || p.type === "group") last = i;
|
|
2228
|
+
});
|
|
2229
|
+
const join = (list) => list.map((p) => p.value).join("");
|
|
2230
|
+
return {
|
|
2231
|
+
prefix: join(parts.slice(0, first)),
|
|
2232
|
+
body: parts.slice(first, last + 1),
|
|
2233
|
+
suffix: join(parts.slice(last + 1).filter((p) => p.type !== "decimal" && p.type !== "fraction"))
|
|
2234
|
+
};
|
|
2235
|
+
}
|
|
2236
|
+
/**
|
|
2237
|
+
* The field text for an amount's own characters and their reading. `verbatim` shows a number's
|
|
2238
|
+
* characters as typed, for grouping the person typed themselves.
|
|
2239
|
+
*/
|
|
2240
|
+
function render(raw, reading, verbatim = false) {
|
|
2241
|
+
if (raw === "" || reading.kind === "empty") return {
|
|
2242
|
+
text: "",
|
|
2243
|
+
raw: "",
|
|
2244
|
+
owned: [],
|
|
2245
|
+
reading: { kind: "empty" }
|
|
2246
|
+
};
|
|
2247
|
+
if (reading.kind !== "number" || verbatim) {
|
|
2248
|
+
const { prefix, suffix } = frame(reading.kind === "number" ? Number(reading.whole) : 0);
|
|
2249
|
+
return {
|
|
2250
|
+
text: prefix + raw + suffix,
|
|
2251
|
+
raw,
|
|
2252
|
+
owned: [...prefix].map(() => false).concat([...raw].map(() => true), [...suffix].map(() => false)),
|
|
2253
|
+
reading
|
|
2254
|
+
};
|
|
2255
|
+
}
|
|
2256
|
+
const { prefix, body, suffix } = frame(Number(reading.whole));
|
|
2257
|
+
const owned = [...prefix].map(() => false);
|
|
2258
|
+
let text = prefix;
|
|
2259
|
+
for (const part of body) {
|
|
2260
|
+
text += part.value;
|
|
2261
|
+
for (const _ of part.value) owned.push(part.type === "integer");
|
|
2262
|
+
}
|
|
2263
|
+
if (reading.mark) {
|
|
2264
|
+
const tail = raw.slice(reading.whole.length);
|
|
2265
|
+
text += tail;
|
|
2266
|
+
for (const _ of tail) owned.push(true);
|
|
2267
|
+
}
|
|
2268
|
+
text += suffix;
|
|
2269
|
+
for (const _ of suffix) owned.push(false);
|
|
2270
|
+
return {
|
|
2271
|
+
text,
|
|
2272
|
+
raw,
|
|
2273
|
+
owned,
|
|
2274
|
+
reading
|
|
2275
|
+
};
|
|
2276
|
+
}
|
|
2277
|
+
function canonical(reading, mark = decimal) {
|
|
2278
|
+
return reading.whole + (reading.mark ? mark + reading.fraction : "");
|
|
2279
|
+
}
|
|
2280
|
+
function read(raw, lenient = false) {
|
|
2281
|
+
const reading = readAmount(raw, symbols, digits, decimal, lenient);
|
|
2282
|
+
return render(reading.kind === "number" ? canonical(reading) : raw, reading);
|
|
2283
|
+
}
|
|
2284
|
+
/** The text for a number at rest: grouped, with `minimumFractionDigits` (a fixed currency's). */
|
|
2285
|
+
function fromValue(value, minimumFractionDigits = 0) {
|
|
2286
|
+
if (value === null) return render("", { kind: "empty" });
|
|
2287
|
+
const [whole = "0", fraction = ""] = new Intl.NumberFormat("en-US", {
|
|
2288
|
+
useGrouping: false,
|
|
2289
|
+
minimumFractionDigits,
|
|
2290
|
+
maximumFractionDigits: Math.max(digits, minimumFractionDigits)
|
|
2291
|
+
}).format(value).split(".");
|
|
2292
|
+
return render(whole + (fraction ? decimal + fraction : ""), {
|
|
2293
|
+
kind: "number",
|
|
2294
|
+
value,
|
|
2295
|
+
whole,
|
|
2296
|
+
fraction,
|
|
2297
|
+
mark: fraction !== ""
|
|
2298
|
+
});
|
|
2299
|
+
}
|
|
2300
|
+
/** How many of the amount's own characters come before a position in the field. */
|
|
2301
|
+
function rawIndex(state, position) {
|
|
2302
|
+
let index = 0;
|
|
2303
|
+
for (let i = 0; i < position && i < state.owned.length; i += 1) if (state.owned[i]) index += 1;
|
|
2304
|
+
return index;
|
|
2305
|
+
}
|
|
2306
|
+
/** The field position right after the amount's `index`-th own character. */
|
|
2307
|
+
function textIndex(state, index) {
|
|
2308
|
+
const first = state.owned.indexOf(true);
|
|
2309
|
+
if (first === -1) return state.text.length;
|
|
2310
|
+
if (index <= 0) return first;
|
|
2311
|
+
let seen = 0;
|
|
2312
|
+
for (let i = 0; i < state.owned.length; i += 1) {
|
|
2313
|
+
if (!state.owned[i]) continue;
|
|
2314
|
+
seen += 1;
|
|
2315
|
+
if (seen === index) return i + 1;
|
|
2316
|
+
}
|
|
2317
|
+
return state.owned.lastIndexOf(true) + 1;
|
|
2318
|
+
}
|
|
2319
|
+
/**
|
|
2320
|
+
* One `input` event: the field held `previous`, it now holds `text` with the caret at `caret`.
|
|
2321
|
+
* What changed is found by comparing the two texts around the caret, so a keystroke, a paste
|
|
2322
|
+
* into a selection, autofill and a composition all arrive the same way; the symbol and the
|
|
2323
|
+
* grouping in `previous` are this field's own and are never read as typed marks.
|
|
2324
|
+
*/
|
|
2325
|
+
function edit(previous, text, caret, inputType = "") {
|
|
2326
|
+
const before = previous.text;
|
|
2327
|
+
const end = Math.min(Math.max(caret, 0), text.length);
|
|
2328
|
+
let start = 0;
|
|
2329
|
+
while (start < end && start < before.length && before[start] === text[start]) start += 1;
|
|
2330
|
+
let tail = 0;
|
|
2331
|
+
while (tail < text.length - end && tail < before.length - start && before[before.length - 1 - tail] === text[text.length - 1 - tail]) tail += 1;
|
|
2332
|
+
const inserted = text.slice(start, text.length - tail);
|
|
2333
|
+
let rawStart = rawIndex(previous, start);
|
|
2334
|
+
let rawEnd = rawIndex(previous, before.length - tail);
|
|
2335
|
+
if (inserted === "" && rawStart === rawEnd && before.length - tail - start === 1) {
|
|
2336
|
+
if (inputType === "deleteContentForward") rawEnd = Math.min(previous.raw.length, rawEnd + 1);
|
|
2337
|
+
else rawStart = Math.max(0, rawStart - 1);
|
|
2338
|
+
}
|
|
2339
|
+
const candidate = previous.raw.slice(0, rawStart) + inserted + previous.raw.slice(rawEnd);
|
|
2340
|
+
const candidateCaret = rawStart + Math.min(inserted.length, Math.max(0, end - start));
|
|
2341
|
+
const keystroke = inserted.length === 1 && (inputType === "" || inputType === "insertText");
|
|
2342
|
+
let reading = readAmount(candidate, symbols, digits, decimal, !keystroke);
|
|
2343
|
+
const refuse = {
|
|
2344
|
+
refused: true,
|
|
2345
|
+
caret: textIndex(previous, rawStart)
|
|
2346
|
+
};
|
|
2347
|
+
if (reading.kind === "number" && reading.value > max) return refuse;
|
|
2348
|
+
if (keystroke && reading.kind === "invalid" && reading.reason === "notANumber") {
|
|
2349
|
+
if (readAmount(candidate, symbols, digits, decimal, true).kind === "number") reading = { kind: "pending" };
|
|
2350
|
+
else if (previous.reading.kind !== "invalid") return refuse;
|
|
2351
|
+
}
|
|
2352
|
+
if (reading.kind !== "number") {
|
|
2353
|
+
const next = render(candidate, reading);
|
|
2354
|
+
return {
|
|
2355
|
+
refused: false,
|
|
2356
|
+
next,
|
|
2357
|
+
caret: textIndex(next, candidateCaret)
|
|
2358
|
+
};
|
|
2359
|
+
}
|
|
2360
|
+
const plain = masked(candidate, symbols);
|
|
2361
|
+
const marks = (plain.match(/[.,]/g) ?? []).length;
|
|
2362
|
+
if (keystroke && marks > (reading.mark ? 1 : 0)) {
|
|
2363
|
+
const next = render(candidate, reading, true);
|
|
2364
|
+
return {
|
|
2365
|
+
refused: false,
|
|
2366
|
+
next,
|
|
2367
|
+
caret: textIndex(next, candidateCaret)
|
|
2368
|
+
};
|
|
2369
|
+
}
|
|
2370
|
+
const mark = reading.mark ? Math.max(plain.lastIndexOf(","), plain.lastIndexOf(".")) : -1;
|
|
2371
|
+
const shown = keystroke && mark !== -1 && reading.whole.length <= 3 ? plain[mark] : decimal;
|
|
2372
|
+
const next = render(canonical(reading, shown), reading);
|
|
2373
|
+
let rawCaret;
|
|
2374
|
+
if (mark !== -1 && mark < candidateCaret) {
|
|
2375
|
+
const typed = digitCount(candidate.slice(mark + 1, candidateCaret));
|
|
2376
|
+
rawCaret = reading.whole.length + 1 + Math.min(typed, reading.fraction.length);
|
|
2377
|
+
} else {
|
|
2378
|
+
const after = digitCount(candidate.slice(candidateCaret, mark === -1 ? void 0 : mark));
|
|
2379
|
+
rawCaret = Math.max(0, reading.whole.length - after);
|
|
2380
|
+
}
|
|
2381
|
+
return {
|
|
2382
|
+
refused: false,
|
|
2383
|
+
next,
|
|
2384
|
+
caret: textIndex(next, rawCaret)
|
|
2385
|
+
};
|
|
2386
|
+
}
|
|
2387
|
+
return {
|
|
2388
|
+
symbols,
|
|
2389
|
+
render,
|
|
2390
|
+
read,
|
|
2391
|
+
fromValue,
|
|
2392
|
+
edit,
|
|
2393
|
+
textIndex
|
|
2394
|
+
};
|
|
2395
|
+
}
|
|
2108
2396
|
// src/utils/number-format.ts
|
|
2109
2397
|
/**
|
|
2110
2398
|
* Builds the `Intl.NumberFormat` a `NumberFormatOptions` describes, so a caller that needs to
|
|
@@ -2522,6 +2810,15 @@ var UnitInput_default = /* @__PURE__ */ defineComponent({
|
|
|
2522
2810
|
type: Boolean,
|
|
2523
2811
|
default: false
|
|
2524
2812
|
},
|
|
2813
|
+
fixedFraction: {
|
|
2814
|
+
type: Boolean,
|
|
2815
|
+
default: false
|
|
2816
|
+
},
|
|
2817
|
+
nullable: {
|
|
2818
|
+
type: Boolean,
|
|
2819
|
+
default: false
|
|
2820
|
+
},
|
|
2821
|
+
placeholder: { default: void 0 },
|
|
2525
2822
|
size: { default: "md" },
|
|
2526
2823
|
id: { default: void 0 },
|
|
2527
2824
|
name: { default: void 0 },
|
|
@@ -2551,38 +2848,24 @@ var UnitInput_default = /* @__PURE__ */ defineComponent({
|
|
|
2551
2848
|
emits: [
|
|
2552
2849
|
"update:modelValue",
|
|
2553
2850
|
"update:formattedValue",
|
|
2851
|
+
"update:valid",
|
|
2554
2852
|
"clear",
|
|
2555
2853
|
"blur",
|
|
2556
2854
|
"focus"
|
|
2557
2855
|
],
|
|
2558
2856
|
setup(__props, { expose: __expose, emit: __emit }) {
|
|
2559
2857
|
/**
|
|
2560
|
-
* A unit field: a number on one side, and an `Intl`-formatted string in the field at all times
|
|
2858
|
+
* A unit field: a number on one side, and an `Intl`-formatted string in the field at all times,
|
|
2859
|
+
* formatted as it is typed (`$1,234.5`, `1.234 kr.`, `1,234 km`) with the caret kept where it was.
|
|
2561
2860
|
*
|
|
2562
|
-
*
|
|
2563
|
-
*
|
|
2564
|
-
*
|
|
2565
|
-
*
|
|
2566
|
-
*
|
|
2567
|
-
*
|
|
2568
|
-
* **The field is always formatted.** There is no editing mode and no focus-dependent text: focused
|
|
2569
|
-
* or not, the field shows `$1,234.5`, `1.234 kr.` or `1,234 km`, reformatted on every keystroke
|
|
2570
|
-
* with the caret mapped through the new text by *numeric content* (`mapCursorByNumericContent`) —
|
|
2571
|
-
* count the digits before the caret, then walk the reformatted string until as many have gone by.
|
|
2572
|
-
* That is what lets a group separator appear to the left of the caret without the caret moving.
|
|
2861
|
+
* Every `input` event, however the text arrived (a keystroke, a paste into a selection, a drop,
|
|
2862
|
+
* autofill, a virtual keyboard, dictation, the end of an IME composition), goes through one
|
|
2863
|
+
* function, `createAmountEditor().edit` in `src/utils/amount.ts`: it finds what changed against
|
|
2864
|
+
* the text the field showed, reads the result with one parser and formats it back. Keydown never
|
|
2865
|
+
* inserts or moves anything. The rules are in the README, under `UnitInput` and `CurrencyInput`.
|
|
2573
2866
|
*
|
|
2574
2867
|
* It is an `Input` in every respect a customer can see: the same box, sizes, paddings, focus ring
|
|
2575
2868
|
* and error boundary, imported from `../input/classes.ts` rather than copied.
|
|
2576
|
-
*
|
|
2577
|
-
* **Typing is not filtered, it is stripped.** Unlike `QuantityStepper`, a non-numeric keystroke is
|
|
2578
|
-
* not refused at `beforeinput`; it lands, and the reformat that follows removes it. That is the
|
|
2579
|
-
* private behaviour, and it is the one that can survive a field whose own text is full of
|
|
2580
|
-
* non-numeric characters (a symbol, a group separator, a unit) — a filter that judged the
|
|
2581
|
-
* *resulting value* would have to understand the formatted string it is judging.
|
|
2582
|
-
*
|
|
2583
|
-
* **`preserveEmptyUntilBlur`.** Clearing the text while editing leaves the field empty and emits
|
|
2584
|
-
* nothing, so a customer can retype an amount from scratch; leaving the field falls back to the
|
|
2585
|
-
* formatted `clamp(0)` — `min` — and emits that.
|
|
2586
2869
|
*/
|
|
2587
2870
|
const eldra = useEldraScope();
|
|
2588
2871
|
/** How far the drag handle travels per `step`, in pixels — the private library's own number. */
|
|
@@ -2597,15 +2880,9 @@ var UnitInput_default = /* @__PURE__ */ defineComponent({
|
|
|
2597
2880
|
const controlId = useUiId("unit-input", () => props.id ?? (field?.value.labelsControl === true ? field.value.id : void 0));
|
|
2598
2881
|
/** Own ids first, then the wrapper's — the one `describedBy` rule (see the README's Fields). */
|
|
2599
2882
|
const describedBy = computed(() => joinIds(props.describedBy, field?.value.describedBy));
|
|
2600
|
-
const isInvalid = computed(() => props.invalid ?? field?.value.invalid ?? false);
|
|
2601
2883
|
const isRequired = computed(() => props.required ?? field?.value.required ?? false);
|
|
2602
2884
|
const controlRef = ref(null);
|
|
2603
2885
|
const dragHandleRef = ref(null);
|
|
2604
|
-
/** The text in the field, and the same text with everything but digits and a `.` taken out. */
|
|
2605
|
-
const localValue = ref("");
|
|
2606
|
-
const localUnformattedValue = ref("");
|
|
2607
|
-
/** Set while an emptied field is being retyped: the field stays empty and emits nothing. */
|
|
2608
|
-
const preserveEmptyUntilBlur = ref(false);
|
|
2609
2886
|
/**
|
|
2610
2887
|
* Whether the parent actually **binds** `modelValue` — read off the vnode rather than from the
|
|
2611
2888
|
* resolved prop, because an unbound prop and one bound to `undefined` are the same value and a
|
|
@@ -2617,14 +2894,15 @@ var UnitInput_default = /* @__PURE__ */ defineComponent({
|
|
|
2617
2894
|
if (!vnodeProps) return false;
|
|
2618
2895
|
return Object.prototype.hasOwnProperty.call(vnodeProps, "modelValue") || Object.prototype.hasOwnProperty.call(vnodeProps, "model-value");
|
|
2619
2896
|
});
|
|
2620
|
-
const formattedValue = computed({
|
|
2621
|
-
get: () => localValue.value,
|
|
2622
|
-
set: (value) => updateLocalFormattedValue(value)
|
|
2623
|
-
});
|
|
2624
|
-
const { onKeydown: onUndoRedoKeydown } = useUndoRedo(formattedValue);
|
|
2625
2897
|
const resolvedLocale = computed(() => props.locale ?? ambientLocale.value);
|
|
2626
|
-
/** Whether this field may be edited at all
|
|
2898
|
+
/** Whether this field may be edited at all. */
|
|
2627
2899
|
const isEditable = computed(() => !props.readonly && !props.disabled);
|
|
2900
|
+
/** A currency's own decimals (ISK 0, EUR 2); `null` for a unit. */
|
|
2901
|
+
const currencyDigits = computed(() => props.isCurrency ? currencyFractionDigits(props.currency) : null);
|
|
2902
|
+
const fixedDigits = computed(() => props.isCurrency && props.fixedFraction ? currencyDigits.value : null);
|
|
2903
|
+
/** How many fraction digits the field takes: none in a currency that has none. */
|
|
2904
|
+
const fractionDigits = computed(() => currencyDigits.value === 0 ? 0 : fixedDigits.value ?? props.maxFraction);
|
|
2905
|
+
const restingFraction = computed(() => fixedDigits.value ?? 0);
|
|
2628
2906
|
function numberFormat(maxFraction, minFraction) {
|
|
2629
2907
|
return createNumberFormat({
|
|
2630
2908
|
locale: resolvedLocale.value,
|
|
@@ -2636,32 +2914,20 @@ var UnitInput_default = /* @__PURE__ */ defineComponent({
|
|
|
2636
2914
|
currencyDisplay: props.isCurrency && props.narrowSymbol ? "narrowSymbol" : void 0
|
|
2637
2915
|
});
|
|
2638
2916
|
}
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
*/
|
|
2646
|
-
const
|
|
2647
|
-
|
|
2648
|
-
const
|
|
2649
|
-
|
|
2650
|
-
return accumulator;
|
|
2651
|
-
}, {});
|
|
2652
|
-
const formatted = format.format(1e3);
|
|
2653
|
-
const symbol = parts.unit || parts.currency;
|
|
2654
|
-
if (symbol) {
|
|
2655
|
-
const symbolIndex = formatted.indexOf(symbol);
|
|
2656
|
-
parts.symbolBefore = symbolIndex === 0;
|
|
2657
|
-
parts.symbolAfter = symbolIndex > 0;
|
|
2658
|
-
parts.symbolLength = symbol.length;
|
|
2659
|
-
}
|
|
2660
|
-
if (parts.literal) parts.symbolLength = (parts.symbolLength ?? 0) + parts.literal.length;
|
|
2661
|
-
return parts;
|
|
2917
|
+
const editor = computed(() => createAmountEditor({
|
|
2918
|
+
format: (minFraction) => numberFormat(Math.max(fractionDigits.value, minFraction), minFraction),
|
|
2919
|
+
decimal: localeSeparators(resolvedLocale.value).decimal,
|
|
2920
|
+
digits: fractionDigits.value,
|
|
2921
|
+
max: props.max
|
|
2922
|
+
}));
|
|
2923
|
+
/** What the placeholder shows: the same format at zero, with up to two fraction digits spelled out. */
|
|
2924
|
+
const emptyValuePlaceholder = computed(() => {
|
|
2925
|
+
if (props.placeholder !== void 0) return props.placeholder;
|
|
2926
|
+
const digits = Math.min(2, fractionDigits.value);
|
|
2927
|
+
return numberFormat(digits, digits).format(0);
|
|
2662
2928
|
});
|
|
2663
2929
|
function formatValue(value) {
|
|
2664
|
-
return
|
|
2930
|
+
return editor.value.fromValue(value, restingFraction.value).text;
|
|
2665
2931
|
}
|
|
2666
2932
|
function clampValue(value) {
|
|
2667
2933
|
return Math.min(props.max, Math.max(props.min, value));
|
|
@@ -2672,160 +2938,154 @@ var UnitInput_default = /* @__PURE__ */ defineComponent({
|
|
|
2672
2938
|
const numberValue = Number(value);
|
|
2673
2939
|
return Number.isFinite(numberValue) ? numberValue : null;
|
|
2674
2940
|
}
|
|
2675
|
-
function formatModelValue(value) {
|
|
2676
|
-
const numberValue = normalizeNumericValue(value);
|
|
2677
|
-
return numberValue === null ? "" : formatValue(numberValue);
|
|
2678
|
-
}
|
|
2679
2941
|
const normalizedStep = computed(() => {
|
|
2680
2942
|
const next = Number(props.step);
|
|
2681
2943
|
return Number.isFinite(next) && next > 0 ? next : 1;
|
|
2682
2944
|
});
|
|
2683
|
-
/** What an emptied field
|
|
2684
|
-
const
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
return
|
|
2712
|
-
}
|
|
2713
|
-
function
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
* therefore move the caret with them, and none of it depends on how many characters the format
|
|
2728
|
-
* added or removed.
|
|
2729
|
-
*/
|
|
2730
|
-
function mapCursorByNumericContent(sourceValue, sourceCursor, formatted) {
|
|
2731
|
-
const normalizedBeforeCursor = unformatValue(sourceValue.slice(0, sourceCursor));
|
|
2732
|
-
if (normalizedBeforeCursor === "") return editableStartIndex(formatted);
|
|
2733
|
-
let seen = "";
|
|
2734
|
-
for (let index = 0; index < formatted.length; index += 1) {
|
|
2735
|
-
const character = formatted[index] ?? "";
|
|
2736
|
-
if (!isEditableNumberCharacter(character)) continue;
|
|
2737
|
-
seen += character === formParts.value.decimal ? "." : character;
|
|
2738
|
-
if (seen.length >= normalizedBeforeCursor.length) return index + 1;
|
|
2739
|
-
}
|
|
2740
|
-
return editableEndIndex(formatted);
|
|
2945
|
+
/** What an emptied field means when it is not `nullable`, and what a step starts from. */
|
|
2946
|
+
const fallbackValue = computed(() => clampValue(0));
|
|
2947
|
+
const state = shallowRef(editor.value.fromValue(null));
|
|
2948
|
+
const localValue = computed(() => state.value.text);
|
|
2949
|
+
const ownValue = ref(null);
|
|
2950
|
+
const focused = ref(false);
|
|
2951
|
+
const composing = ref(false);
|
|
2952
|
+
/** Whether the text was edited since focus, so leaving an untouched empty field changes nothing. */
|
|
2953
|
+
const edited = ref(false);
|
|
2954
|
+
/** The value the field had when editing began: the model while the text cannot be read. */
|
|
2955
|
+
const editStart = ref(null);
|
|
2956
|
+
const reading = computed(() => state.value.reading);
|
|
2957
|
+
const unreadable = computed(() => reading.value.kind === "invalid" || reading.value.kind === "pending");
|
|
2958
|
+
/** Unreadable text says why at once; a text still being typed only once the field is left. */
|
|
2959
|
+
const invalidReason = computed(() => {
|
|
2960
|
+
if (reading.value.kind === "invalid") return reading.value.reason;
|
|
2961
|
+
if (reading.value.kind === "pending" && !focused.value) return "notANumber";
|
|
2962
|
+
return null;
|
|
2963
|
+
});
|
|
2964
|
+
const isInvalid = computed(() => (props.invalid ?? field?.value.invalid ?? false) || invalidReason.value !== null);
|
|
2965
|
+
function currentValue() {
|
|
2966
|
+
return hasModelValueProp.value ? normalizeNumericValue(props.modelValue) : ownValue.value;
|
|
2967
|
+
}
|
|
2968
|
+
/** The number the text stands for: what the form posts and what a step starts from. */
|
|
2969
|
+
const numericModel = computed(() => {
|
|
2970
|
+
const now = reading.value;
|
|
2971
|
+
if (now.kind === "number") return now.value;
|
|
2972
|
+
if (now.kind === "empty") return props.nullable ? null : edited.value ? fallbackValue.value : null;
|
|
2973
|
+
return editStart.value;
|
|
2974
|
+
});
|
|
2975
|
+
function setState(next, emitFormatted = true) {
|
|
2976
|
+
state.value = next;
|
|
2977
|
+
if (emitFormatted) emit("update:formattedValue", next.text);
|
|
2978
|
+
}
|
|
2979
|
+
function emitModel(value) {
|
|
2980
|
+
ownValue.value = value;
|
|
2981
|
+
emit("update:modelValue", value);
|
|
2982
|
+
}
|
|
2983
|
+
/** Whether the text already says `value`, so a parent echoing it never rewrites "12," or "34,0". */
|
|
2984
|
+
function textAgreesWith(value) {
|
|
2985
|
+
const now = reading.value;
|
|
2986
|
+
if (now.kind === "empty") return value === null || edited.value && !props.nullable && value === fallbackValue.value;
|
|
2987
|
+
if (now.kind === "number") return now.value === value;
|
|
2988
|
+
return true;
|
|
2741
2989
|
}
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
localValue.value = value;
|
|
2746
|
-
localUnformattedValue.value = unformatValue(value);
|
|
2747
|
-
if (emitModel) emit("update:modelValue", normalizeNumericValue(localUnformattedValue.value));
|
|
2748
|
-
if (emitFormatted) emit("update:formattedValue", value);
|
|
2990
|
+
function setCaret(target, caret) {
|
|
2991
|
+
if (typeof document === "undefined" || document.activeElement !== target) return;
|
|
2992
|
+
target.setSelectionRange(caret, caret);
|
|
2749
2993
|
}
|
|
2750
|
-
/**
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
*/
|
|
2755
|
-
async function
|
|
2756
|
-
if (preserveEmptyUntilBlur.value) return;
|
|
2757
|
-
let nextValue = inputValue;
|
|
2994
|
+
/** The caret at the end of the amount, before a trailing symbol. */
|
|
2995
|
+
function endOfAmount() {
|
|
2996
|
+
return editor.value.textIndex(state.value, state.value.raw.length);
|
|
2997
|
+
}
|
|
2998
|
+
/** Shows what a bound parent holds, when it wrote back something other than the text says. */
|
|
2999
|
+
async function reconcile(target, emitted) {
|
|
2758
3000
|
await nextTick();
|
|
2759
|
-
if (hasModelValueProp.value)
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
3001
|
+
if (!hasModelValueProp.value) return;
|
|
3002
|
+
const held = normalizeNumericValue(props.modelValue);
|
|
3003
|
+
if (held === emitted || textAgreesWith(held)) return;
|
|
3004
|
+
setState(editor.value.fromValue(held, restingFraction.value));
|
|
3005
|
+
target.value = state.value.text;
|
|
3006
|
+
setCaret(target, endOfAmount());
|
|
3007
|
+
}
|
|
3008
|
+
/** One change of the field's text, from an `input` event or an undo. */
|
|
3009
|
+
function applyText(target, text, caret, inputType) {
|
|
3010
|
+
if (!focused.value && !unreadable.value) editStart.value = currentValue();
|
|
3011
|
+
const result = editor.value.edit(state.value, text, caret, inputType);
|
|
3012
|
+
if (result.refused) {
|
|
3013
|
+
target.value = state.value.text;
|
|
3014
|
+
setCaret(target, result.caret);
|
|
3015
|
+
return;
|
|
2769
3016
|
}
|
|
3017
|
+
const wasUnreadable = unreadable.value;
|
|
3018
|
+
edited.value = true;
|
|
3019
|
+
setState(result.next);
|
|
3020
|
+
target.value = result.next.text;
|
|
3021
|
+
setCaret(target, result.caret);
|
|
3022
|
+
const now = result.next.reading;
|
|
3023
|
+
if (now.kind === "number") {
|
|
3024
|
+
emitModel(now.value);
|
|
3025
|
+
reconcile(target, now.value);
|
|
3026
|
+
} else if (now.kind === "empty") emitModel(props.nullable ? null : fallbackValue.value);
|
|
3027
|
+
else if (!wasUnreadable) emitModel(editStart.value);
|
|
3028
|
+
if (!focused.value) nextTick(settle);
|
|
3029
|
+
}
|
|
3030
|
+
/** The text at rest: grouped, a fixed currency's decimals padded, a bare mark or zero tail gone. */
|
|
3031
|
+
function settle() {
|
|
3032
|
+
const now = reading.value;
|
|
3033
|
+
if (now.kind === "number") {
|
|
3034
|
+
const next = editor.value.fromValue(now.value, restingFraction.value);
|
|
3035
|
+
if (next.text !== state.value.text) setState(next);
|
|
3036
|
+
} else if (now.kind === "empty" && edited.value && !props.nullable) setState(editor.value.fromValue(fallbackValue.value, restingFraction.value));
|
|
3037
|
+
const target = controlRef.value;
|
|
3038
|
+
if (target && target.value !== state.value.text) target.value = state.value.text;
|
|
2770
3039
|
}
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
3040
|
+
const { onKeydown: onUndoRedoKeydown } = useUndoRedo(computed({
|
|
3041
|
+
get: () => state.value.text,
|
|
3042
|
+
set: (value) => {
|
|
3043
|
+
const target = controlRef.value;
|
|
3044
|
+
if (target) applyText(target, value, value.length, "historyUndo");
|
|
3045
|
+
}
|
|
3046
|
+
}));
|
|
3047
|
+
/** Put a collapsed caret inside the amount when it lands on a symbol. */
|
|
2776
3048
|
function setCursorToValue() {
|
|
2777
3049
|
const target = controlRef.value;
|
|
2778
|
-
if (!target) return
|
|
2779
|
-
const
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
const
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
target.setSelectionRange(cursorIndex, cursorIndex);
|
|
2788
|
-
return true;
|
|
2789
|
-
}
|
|
2790
|
-
return false;
|
|
2791
|
-
}
|
|
2792
|
-
/** Select the digits only, never the symbol — what a double-click and Ctrl/Cmd+A both mean here. */
|
|
3050
|
+
if (!target || state.value.raw === "") return;
|
|
3051
|
+
const start = target.selectionStart ?? 0;
|
|
3052
|
+
if (start !== (target.selectionEnd ?? 0)) return;
|
|
3053
|
+
const first = editor.value.textIndex(state.value, 0);
|
|
3054
|
+
const last = endOfAmount();
|
|
3055
|
+
if (start < first) target.setSelectionRange(first, first);
|
|
3056
|
+
else if (start > last) target.setSelectionRange(last, last);
|
|
3057
|
+
}
|
|
3058
|
+
/** Select the amount only, never the symbol — what a double-click and Ctrl/Cmd+A both mean here. */
|
|
2793
3059
|
function selectOnlyNumbers() {
|
|
2794
3060
|
const target = controlRef.value;
|
|
2795
|
-
if (!target) return;
|
|
2796
|
-
|
|
2797
|
-
const lastIndex = formattedValue.value.search(/\d(?=\D*$)/) + 1;
|
|
2798
|
-
target.setSelectionRange(firstIndex, lastIndex);
|
|
3061
|
+
if (!target || state.value.raw === "") return;
|
|
3062
|
+
target.setSelectionRange(editor.value.textIndex(state.value, 0), endOfAmount());
|
|
2799
3063
|
}
|
|
2800
|
-
|
|
2801
|
-
const input = controlRef.value;
|
|
2802
|
-
if (!input) return;
|
|
2803
|
-
const nextFormattedValue = formatValue(normalizedClampedValue(value));
|
|
2804
|
-
const nextCursor = Math.min(cursor ?? input.selectionStart ?? nextFormattedValue.length, nextFormattedValue.length);
|
|
2805
|
-
preserveEmptyUntilBlur.value = false;
|
|
2806
|
-
updateLocalFormattedValue(nextFormattedValue);
|
|
2807
|
-
input.value = nextFormattedValue;
|
|
2808
|
-
await reconcileControlledValue(input, nextCursor, nextFormattedValue);
|
|
2809
|
-
}
|
|
2810
|
-
async function updateSteppedValue(direction) {
|
|
3064
|
+
function applyNumericValue(value) {
|
|
2811
3065
|
const input = controlRef.value;
|
|
2812
3066
|
if (!input) return;
|
|
2813
|
-
|
|
2814
|
-
|
|
3067
|
+
const next = Number(clampValue(value).toFixed(fractionDigits.value));
|
|
3068
|
+
edited.value = true;
|
|
3069
|
+
setState(editor.value.fromValue(next, restingFraction.value));
|
|
3070
|
+
input.value = state.value.text;
|
|
3071
|
+
emitModel(next);
|
|
3072
|
+
setCaret(input, endOfAmount());
|
|
3073
|
+
reconcile(input, next);
|
|
3074
|
+
}
|
|
3075
|
+
function steppedFrom() {
|
|
3076
|
+
return numericModel.value ?? fallbackValue.value;
|
|
2815
3077
|
}
|
|
2816
3078
|
function onFocus(event) {
|
|
3079
|
+
focused.value = true;
|
|
3080
|
+
edited.value = false;
|
|
3081
|
+
if (!unreadable.value) editStart.value = currentValue();
|
|
2817
3082
|
if (isEditable.value) setTimeout(() => setCursorToValue());
|
|
2818
3083
|
emit("focus", event);
|
|
2819
3084
|
}
|
|
2820
3085
|
function onBlur(event) {
|
|
3086
|
+
focused.value = false;
|
|
3087
|
+
settle();
|
|
2821
3088
|
emit("blur", event);
|
|
2822
|
-
if (!preserveEmptyUntilBlur.value) return;
|
|
2823
|
-
preserveEmptyUntilBlur.value = false;
|
|
2824
|
-
if (localUnformattedValue.value !== "") return;
|
|
2825
|
-
const fallbackFormatted = formatValue(fallbackBlurValue.value);
|
|
2826
|
-
updateLocalFormattedValue(fallbackFormatted);
|
|
2827
|
-
const target = controlRef.value;
|
|
2828
|
-
if (target) target.value = fallbackFormatted;
|
|
2829
3089
|
}
|
|
2830
3090
|
function onClick() {
|
|
2831
3091
|
if (isEditable.value) setCursorToValue();
|
|
@@ -2834,100 +3094,56 @@ var UnitInput_default = /* @__PURE__ */ defineComponent({
|
|
|
2834
3094
|
if (isEditable.value) selectOnlyNumbers();
|
|
2835
3095
|
}
|
|
2836
3096
|
function onClear() {
|
|
2837
|
-
|
|
2838
|
-
updateLocalFormattedValue("");
|
|
3097
|
+
setState(editor.value.fromValue(null));
|
|
2839
3098
|
const target = controlRef.value;
|
|
2840
3099
|
if (target) target.value = "";
|
|
3100
|
+
emitModel(null);
|
|
2841
3101
|
emit("clear");
|
|
2842
3102
|
target?.focus();
|
|
2843
3103
|
}
|
|
2844
|
-
|
|
3104
|
+
function onKeydown(event) {
|
|
2845
3105
|
if (!isEditable.value) return;
|
|
2846
|
-
const input = controlRef.value;
|
|
2847
|
-
const key = event.key;
|
|
2848
|
-
const isDelete = key === "Del" || key === "Delete";
|
|
2849
|
-
const isBackspace = key === "Backspace";
|
|
2850
|
-
const isArrowLeft = key === "ArrowLeft";
|
|
2851
|
-
const isArrowRight = key === "ArrowRight";
|
|
2852
|
-
const isArrowUp = key === "ArrowUp";
|
|
2853
|
-
const isArrowDown = key === "ArrowDown";
|
|
2854
|
-
const isCtrlA = (event.ctrlKey || event.metaKey) && (key === "a" || key === "A");
|
|
2855
|
-
const cursorStart = input?.selectionStart || 0;
|
|
2856
|
-
const cursorEnd = input?.selectionEnd || 0;
|
|
2857
|
-
const part = formParts.value;
|
|
2858
3106
|
onUndoRedoKeydown(event);
|
|
2859
|
-
|
|
3107
|
+
const key = event.key;
|
|
3108
|
+
if (key === "ArrowUp" || key === "ArrowDown") {
|
|
2860
3109
|
event.preventDefault();
|
|
2861
|
-
|
|
3110
|
+
if (!unreadable.value) applyNumericValue(steppedFrom() + normalizedStep.value * (key === "ArrowUp" ? 1 : -1));
|
|
2862
3111
|
return;
|
|
2863
3112
|
}
|
|
2864
|
-
if (
|
|
2865
|
-
if (setCursorToValue()) return;
|
|
2866
|
-
if (isArrowLeft) {
|
|
2867
|
-
if (!(formattedValue.value[cursorStart - 2] || "").match(/\d/)) input?.setSelectionRange(cursorStart - 2, cursorStart - 2);
|
|
2868
|
-
} else if (!(formattedValue.value[cursorStart + 1] || "").match(/\d/)) input?.setSelectionRange(cursorStart + 2, cursorStart + 2);
|
|
2869
|
-
});
|
|
2870
|
-
else if (isCtrlA) {
|
|
3113
|
+
if ((event.ctrlKey || event.metaKey) && (key === "a" || key === "A")) {
|
|
2871
3114
|
selectOnlyNumbers();
|
|
2872
3115
|
event.preventDefault();
|
|
2873
|
-
return;
|
|
2874
|
-
} else if (isDelete && cursorStart === cursorEnd) {
|
|
2875
|
-
if (!(formattedValue.value[cursorStart] || "").match(/\d/)) {
|
|
2876
|
-
if (part.symbolAfter && cursorStart === formattedValue.value.length - part.symbolLength) {
|
|
2877
|
-
input?.setSelectionRange(cursorStart, cursorStart);
|
|
2878
|
-
event.preventDefault();
|
|
2879
|
-
return;
|
|
2880
|
-
}
|
|
2881
|
-
input?.setSelectionRange(cursorStart + 1, cursorStart + 1);
|
|
2882
|
-
setTimeout(() => input?.setSelectionRange(cursorStart, cursorStart));
|
|
2883
|
-
}
|
|
2884
|
-
} else if (isBackspace && cursorStart === cursorEnd) {
|
|
2885
|
-
if (!(formattedValue.value[cursorStart - 1] || "").match(/\d/)) {
|
|
2886
|
-
if (part.symbolBefore && cursorStart === part.symbolLength) {
|
|
2887
|
-
input?.setSelectionRange(cursorStart, cursorStart);
|
|
2888
|
-
event.preventDefault();
|
|
2889
|
-
return;
|
|
2890
|
-
}
|
|
2891
|
-
input?.setSelectionRange(cursorStart - 1, cursorStart - 1);
|
|
2892
|
-
}
|
|
2893
|
-
}
|
|
2894
|
-
if (part.decimal && formattedValue.value.includes(part.decimal) && (key === "," || key === ".")) {
|
|
2895
|
-
event.preventDefault();
|
|
2896
|
-
return;
|
|
2897
|
-
}
|
|
2898
|
-
if (key === "," && part.decimal === "." || key === "." && part.decimal === ",") {
|
|
2899
|
-
const valueBefore = input?.value.slice(0, cursorStart) ?? "";
|
|
2900
|
-
const valueAfter = input?.value.slice(cursorStart) ?? "";
|
|
2901
|
-
formattedValue.value = valueBefore + part.decimal + valueAfter;
|
|
2902
|
-
event.preventDefault();
|
|
2903
3116
|
}
|
|
2904
3117
|
}
|
|
2905
|
-
|
|
3118
|
+
const CARET_KEYS = /* @__PURE__ */ new Set([
|
|
3119
|
+
"Home",
|
|
3120
|
+
"End",
|
|
3121
|
+
"ArrowLeft",
|
|
3122
|
+
"ArrowRight"
|
|
3123
|
+
]);
|
|
3124
|
+
/** After the browser moved the caret: Home and End stop at the amount, never in a symbol. */
|
|
3125
|
+
function onKeyup(event) {
|
|
3126
|
+
if (isEditable.value && !event.shiftKey && CARET_KEYS.has(event.key)) setCursorToValue();
|
|
3127
|
+
}
|
|
3128
|
+
function onInput(event) {
|
|
2906
3129
|
const target = event.target;
|
|
2907
|
-
if (!target
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
const unformatted = unformatValue(value);
|
|
2911
|
-
if (unformatted === "") {
|
|
2912
|
-
preserveEmptyUntilBlur.value = true;
|
|
2913
|
-
target.value = "";
|
|
2914
|
-
updateLocalFormattedValue("", { emitModel: false });
|
|
2915
|
-
return;
|
|
2916
|
-
}
|
|
2917
|
-
preserveEmptyUntilBlur.value = false;
|
|
2918
|
-
if (Number(unformatted) > props.max) {
|
|
2919
|
-
target.value = formattedValue.value;
|
|
3130
|
+
if (!target) return;
|
|
3131
|
+
if (!isEditable.value) {
|
|
3132
|
+
target.value = state.value.text;
|
|
2920
3133
|
return;
|
|
2921
3134
|
}
|
|
2922
|
-
|
|
2923
|
-
if (
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
3135
|
+
const inputEvent = event;
|
|
3136
|
+
if (composing.value || inputEvent.isComposing) return;
|
|
3137
|
+
applyText(target, target.value, target.selectionStart ?? target.value.length, inputEvent.inputType ?? "");
|
|
3138
|
+
}
|
|
3139
|
+
function onCompositionStart() {
|
|
3140
|
+
composing.value = true;
|
|
3141
|
+
}
|
|
3142
|
+
function onCompositionEnd() {
|
|
3143
|
+
composing.value = false;
|
|
3144
|
+
const target = controlRef.value;
|
|
3145
|
+
if (!target || !isEditable.value) return;
|
|
3146
|
+
applyText(target, target.value, target.selectionStart ?? target.value.length, "insertCompositionText");
|
|
2931
3147
|
}
|
|
2932
3148
|
let dragPointerId = null;
|
|
2933
3149
|
let dragStartY = 0;
|
|
@@ -2946,12 +3162,10 @@ var UnitInput_default = /* @__PURE__ */ defineComponent({
|
|
|
2946
3162
|
removeDragListeners();
|
|
2947
3163
|
if (activePointerId !== null) dragHandleRef.value?.releasePointerCapture?.(activePointerId);
|
|
2948
3164
|
}
|
|
2949
|
-
|
|
2950
|
-
if (!isEditable.value) return;
|
|
3165
|
+
function onDragAdjust(direction, steps) {
|
|
3166
|
+
if (!isEditable.value || unreadable.value) return;
|
|
2951
3167
|
const delta = direction === "up" ? steps : -steps;
|
|
2952
|
-
|
|
2953
|
-
await applyNumericValue(currentNumericValue() + normalizedStep.value * delta, input?.selectionStart ?? input?.value.length);
|
|
2954
|
-
setCursorToValue();
|
|
3168
|
+
applyNumericValue(steppedFrom() + normalizedStep.value * delta);
|
|
2955
3169
|
}
|
|
2956
3170
|
function onDragPointerMove(event) {
|
|
2957
3171
|
if (dragPointerId !== null && event.pointerId !== dragPointerId) return;
|
|
@@ -2979,40 +3193,19 @@ var UnitInput_default = /* @__PURE__ */ defineComponent({
|
|
|
2979
3193
|
}
|
|
2980
3194
|
onBeforeUnmount(() => stopDrag());
|
|
2981
3195
|
watch(() => props.modelValue, (value) => {
|
|
2982
|
-
if (
|
|
2983
|
-
const
|
|
2984
|
-
if (
|
|
2985
|
-
|
|
2986
|
-
emitModel: false,
|
|
2987
|
-
emitFormatted: hasModelValueProp.value
|
|
2988
|
-
});
|
|
2989
|
-
}, { immediate: true });
|
|
2990
|
-
watch(hasModelValueProp, (bound) => {
|
|
2991
|
-
if (preserveEmptyUntilBlur.value) return;
|
|
2992
|
-
if (!bound) {
|
|
2993
|
-
if (localValue.value === "") return;
|
|
2994
|
-
updateLocalFormattedValue("", {
|
|
2995
|
-
emitModel: false,
|
|
2996
|
-
emitFormatted: false
|
|
2997
|
-
});
|
|
2998
|
-
return;
|
|
2999
|
-
}
|
|
3000
|
-
const nextFormattedValue = formatModelValue(props.modelValue);
|
|
3001
|
-
if (nextFormattedValue === localValue.value) return;
|
|
3002
|
-
updateLocalFormattedValue(nextFormattedValue, { emitModel: false });
|
|
3196
|
+
if (!hasModelValueProp.value) return;
|
|
3197
|
+
const next = normalizeNumericValue(value);
|
|
3198
|
+
if (textAgreesWith(next)) return;
|
|
3199
|
+
setState(editor.value.fromValue(next, restingFraction.value));
|
|
3003
3200
|
}, { immediate: true });
|
|
3004
3201
|
/** The locale, the currency or the unit changing reformats what is in the field. */
|
|
3005
|
-
watch(
|
|
3006
|
-
|
|
3007
|
-
()
|
|
3008
|
-
()
|
|
3009
|
-
() => props.isCurrency
|
|
3010
|
-
], () => {
|
|
3011
|
-
if (preserveEmptyUntilBlur.value) return;
|
|
3012
|
-
const numeric = normalizeNumericValue(localUnformattedValue.value);
|
|
3013
|
-
if (numeric === null) return;
|
|
3014
|
-
updateLocalFormattedValue(formatValue(numeric), { emitModel: false });
|
|
3202
|
+
watch(editor, () => {
|
|
3203
|
+
const now = reading.value;
|
|
3204
|
+
if (now.kind !== "number") return;
|
|
3205
|
+
setState(editor.value.fromValue(now.value, restingFraction.value), hasModelValueProp.value);
|
|
3015
3206
|
});
|
|
3207
|
+
watch(unreadable, (value) => emit("update:valid", !value));
|
|
3208
|
+
onMounted(() => emit("update:valid", !unreadable.value));
|
|
3016
3209
|
const showOwnLabel = computed(() => props.label !== void 0 && props.label !== "" && field?.value.labelsControl !== true);
|
|
3017
3210
|
const showClear = computed(() => props.clearable && !props.disabled && !props.readonly && localValue.value.length > 0);
|
|
3018
3211
|
const showDragHandle = computed(() => props.enableDragAdjust && !props.disabled && !props.readonly);
|
|
@@ -3064,9 +3257,13 @@ var UnitInput_default = /* @__PURE__ */ defineComponent({
|
|
|
3064
3257
|
actionSize.value,
|
|
3065
3258
|
props.classes?.dragHandle
|
|
3066
3259
|
]);
|
|
3067
|
-
/** `formatValue`
|
|
3068
|
-
*
|
|
3069
|
-
__expose({
|
|
3260
|
+
/** `formatValue` formats one number the way this field would at rest; `isValid` is whether the
|
|
3261
|
+
* text can be read, and `invalidReason` why not. */
|
|
3262
|
+
__expose({
|
|
3263
|
+
formatValue,
|
|
3264
|
+
isValid: computed(() => !unreadable.value),
|
|
3265
|
+
invalidReason
|
|
3266
|
+
});
|
|
3070
3267
|
return (_ctx, _cache) => {
|
|
3071
3268
|
return openBlock(), createElementBlock("div", {
|
|
3072
3269
|
"data-eldra-ui": "",
|
|
@@ -3113,14 +3310,17 @@ var UnitInput_default = /* @__PURE__ */ defineComponent({
|
|
|
3113
3310
|
required: isRequired.value || void 0,
|
|
3114
3311
|
readonly: __props.readonly || void 0,
|
|
3115
3312
|
disabled: __props.disabled || void 0,
|
|
3116
|
-
"aria-invalid": isInvalid.value ? "true" :
|
|
3313
|
+
"aria-invalid": isInvalid.value ? "true" : _ctx.$attrs["aria-invalid"],
|
|
3117
3314
|
"aria-describedby": describedBy.value,
|
|
3118
3315
|
onInput,
|
|
3119
3316
|
onKeydown,
|
|
3317
|
+
onKeyup,
|
|
3120
3318
|
onClick,
|
|
3121
3319
|
onDblclick: onDoubleClick,
|
|
3122
3320
|
onFocus,
|
|
3123
|
-
onBlur
|
|
3321
|
+
onBlur,
|
|
3322
|
+
onCompositionstart: onCompositionStart,
|
|
3323
|
+
onCompositionend: onCompositionEnd
|
|
3124
3324
|
}), null, 16, _hoisted_4$10),
|
|
3125
3325
|
__props.name ? (openBlock(), createElementBlock("input", {
|
|
3126
3326
|
key: 1,
|
|
@@ -3207,6 +3407,9 @@ var CurrencyInput_default = /* @__PURE__ */ defineComponent({
|
|
|
3207
3407
|
type: Boolean,
|
|
3208
3408
|
default: false
|
|
3209
3409
|
},
|
|
3410
|
+
fixedFraction: { type: Boolean },
|
|
3411
|
+
nullable: { type: Boolean },
|
|
3412
|
+
placeholder: {},
|
|
3210
3413
|
size: {},
|
|
3211
3414
|
id: {},
|
|
3212
3415
|
name: {},
|
|
@@ -3218,7 +3421,7 @@ var CurrencyInput_default = /* @__PURE__ */ defineComponent({
|
|
|
3218
3421
|
disabled: { type: Boolean },
|
|
3219
3422
|
classes: {}
|
|
3220
3423
|
},
|
|
3221
|
-
setup(__props) {
|
|
3424
|
+
setup(__props, { expose: __expose }) {
|
|
3222
3425
|
/**
|
|
3223
3426
|
* A money field: `UnitInput` with `isCurrency` always on — the same twenty-line wrapper the
|
|
3224
3427
|
* private library ships, and a one-to-one port of it (see `UnitInput`'s own doc comment, and the
|
|
@@ -3257,8 +3460,18 @@ var CurrencyInput_default = /* @__PURE__ */ defineComponent({
|
|
|
3257
3460
|
if (bindsModelValue.value) result.modelValue = props.modelValue;
|
|
3258
3461
|
return result;
|
|
3259
3462
|
});
|
|
3463
|
+
const unit = ref(null);
|
|
3464
|
+
/** The `UnitInput`'s own `formatValue`, `isValid` and `invalidReason`. */
|
|
3465
|
+
__expose({
|
|
3466
|
+
formatValue: (amount) => unit.value?.formatValue(amount) ?? "",
|
|
3467
|
+
isValid: computed(() => unit.value?.isValid ?? true),
|
|
3468
|
+
invalidReason: computed(() => unit.value?.invalidReason ?? null)
|
|
3469
|
+
});
|
|
3260
3470
|
return (_ctx, _cache) => {
|
|
3261
3471
|
return openBlock(), createBlock(UnitInput_default, mergeProps({
|
|
3472
|
+
ref_key: "unit",
|
|
3473
|
+
ref: unit
|
|
3474
|
+
}, {
|
|
3262
3475
|
...forwarded.value,
|
|
3263
3476
|
..._ctx.$attrs
|
|
3264
3477
|
}, { "is-currency": "" }), createSlots({ _: 2 }, [renderList(_ctx.$slots, (_, slotName) => {
|
|
@@ -7245,7 +7458,7 @@ var SearchBar_default = /* @__PURE__ */ defineComponent({
|
|
|
7245
7458
|
}
|
|
7246
7459
|
onMounted(() => {
|
|
7247
7460
|
if (props.recent === void 0) storedRecent.value = readStoredRecent();
|
|
7248
|
-
if (props.autofocus) fieldRef.value?.focus();
|
|
7461
|
+
if (props.autofocus) nextTick(() => fieldRef.value?.focus());
|
|
7249
7462
|
});
|
|
7250
7463
|
const recentList = computed(() => {
|
|
7251
7464
|
if (!props.showRecent || recentCleared.value) return [];
|
|
@@ -8583,4 +8796,4 @@ var VariantPicker_default = /* @__PURE__ */ defineComponent({
|
|
|
8583
8796
|
};
|
|
8584
8797
|
}
|
|
8585
8798
|
});
|
|
8586
|
-
export {
|
|
8799
|
+
export { Autocomplete_default as $, defaultUnitFormat as A, useHeadingTag as B, filterNumericBeforeInput as C, createNumberFormat as D, UnitInput_default as E, parseLocaleNumber as F, Checkbox_default as G, TAG_SHAPE as H, toMinorUnits as I, FieldError_default as J, VisuallyHidden_default as K, createAmountEditor as L, formatNumber as M, formatUnit as N, currencyFractionDigits as O, localeSeparators as P, FORM_SUBMITTING_KEY as Q, parseAmount as R, Switch_default as S, CurrencyInput_default as T, tagPadding as U, TAG_FILL as V, CheckboxGroup_default as W, Icon_default as X, Button_default as Y, FORM_LAYOUT_KEY as Z, radioRecipe as _, ownsShortcut as a, FormLayout_default as b, RadioGroup_default as c, PLAIN_BOX_ONLY as d, Input_default as et, PLAIN_COLUMNS as f, optionCursor as g, labelRecipe as h, claimShortcut as i, stripMask as it, formatCurrency as j, currencySymbol as k, HINT as l, dotRecipe as m, Textarea_default as n, applyMask as nt, releaseShortcut as o, PLAIN_OPTION as p, RequiredMark_default as q, SearchBar_default as r, defaultCharacterMeaning as rt, SearchResultsPanel_default as s, VariantPicker_default as t, useSlotPresence as tt, INPUT as u, QuantityStepper_default as v, Select_default as w, focusInvalid as x, MultiSelect_default as y, readAmount as z };
|