Skip to content

Commit 740d5fa

Browse files
dimfeldtechniq
authored andcommitted
make DateField format and mask reactive to locale changes (#206)
* make DateField format and mask reactive to locale changes * Make Input component reactive to changes in mask
1 parent 8c2b49a commit 740d5fa

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

packages/svelte-ux/src/lib/components/DateField.svelte

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
const { format: format_ux } = getSettings();
1414
1515
export let value: Date | null = null;
16-
export let format = $format_ux.settings.formats.dates.baseParsing ?? 'MM/dd/yyyy';
17-
export let mask = format.toLowerCase();
16+
export let format: string | undefined = undefined;
17+
export let mask: string | undefined = undefined;
1818
export let replace = 'dmyh';
1919
export let picker = false;
2020
21+
$: actualFormat = format ?? $format_ux.settings.formats.dates.baseParsing ?? 'MM/dd/yyyy';
22+
$: actualMask = mask ?? actualFormat.toLowerCase();
23+
2124
// Field props
2225
export let label = '';
2326
export let error = '';
@@ -38,7 +41,7 @@
3841
function onInputChange(e: any) {
3942
inputValue = e.detail.value;
4043
const lastValue = value;
41-
const parsedValue = parseDate(inputValue ?? '', format, new Date());
44+
const parsedValue = parseDate(inputValue ?? '', actualFormat, new Date());
4245
value = isNaN(parsedValue.valueOf()) ? null : parsedValue;
4346
if (value != lastValue) {
4447
dispatch('change', { value });
@@ -65,8 +68,8 @@
6568
let:id
6669
>
6770
<Input
68-
value={value ? $format_ux(value, PeriodType.Day, { custom: format }) : inputValue}
69-
{mask}
71+
value={value ? $format_ux(value, PeriodType.Day, { custom: actualFormat }) : inputValue}
72+
mask={actualMask}
7073
{replace}
7174
{id}
7275
on:change={onInputChange}

packages/svelte-ux/src/lib/components/Input.svelte

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
export let mask = '';
2525
export let replace = '_';
2626
export let accept: string | RegExp = '\\d';
27-
export let placeholder = mask;
27+
let placeholderProp: string | undefined = undefined;
28+
export { placeholderProp as placeholder };
2829
export let disabled = false;
2930
31+
$: placeholder = placeholderProp ?? mask;
32+
3033
const settingsClasses = getComponentClasses('Input');
3134
3235
let isFocused = false;
@@ -61,7 +64,11 @@
6164
6265
function onInput(e: Event & { currentTarget: HTMLInputElement }) {
6366
const el = e.currentTarget;
67+
applyMask(el, mask);
68+
dispatch('change', { value });
69+
}
6470
71+
function applyMask(el: HTMLInputElement, mask: string) {
6572
if (mask) {
6673
// For selection (including just cursor position), ...
6774
const [i, j] = [el.selectionStart, el.selectionEnd].map((i) => {
@@ -75,10 +82,10 @@
7582
} else {
7683
value = el.value;
7784
}
78-
79-
dispatch('change', { value });
8085
}
8186
87+
$: if (inputEl) applyMask(inputEl, mask);
88+
8289
onMount(() => {
8390
// Format on initial to handle partial values as well as different (but compatible) formats (ex. phone numbers)
8491
if (mask) {

0 commit comments

Comments
 (0)