Skip to content

Commit b13b2a0

Browse files
committed
fix: required causes textfield to use native validation, label stops floating when value deleted
Fixes #234
1 parent a208d08 commit b13b2a0

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

packages/textfield/Textfield.svelte

+12-5
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
{#if !noLabel && (label != null || $$slots.label)}
6767
<FloatingLabel
6868
bind:this={floatingLabel}
69-
floatAbove={value != null && value !== ''}
69+
floatAbove={focused || (value != null && value !== '')}
7070
{required}
7171
wrapped
7272
{...prefixFilter($$restProps, 'label$')}
@@ -83,7 +83,7 @@
8383
{#if !noLabel && (label != null || $$slots.label)}
8484
<FloatingLabel
8585
bind:this={floatingLabel}
86-
floatAbove={value != null && value !== ''}
86+
floatAbove={focused || (value != null && value !== '')}
8787
{required}
8888
wrapped
8989
{...prefixFilter($$restProps, 'label$')}
@@ -111,6 +111,8 @@
111111
bind:dirty
112112
bind:invalid
113113
{updateInvalid}
114+
on:blur={() => (focused = false)}
115+
on:focus={() => (focused = true)}
114116
on:blur
115117
on:focus
116118
aria-controls={helperId}
@@ -134,6 +136,8 @@
134136
bind:dirty
135137
bind:invalid
136138
{updateInvalid}
139+
on:blur={() => (focused = false)}
140+
on:focus={() => (focused = true)}
137141
on:blur
138142
on:focus
139143
aria-controls={helperId}
@@ -290,6 +294,7 @@
290294
let internalClasses = {};
291295
let internalStyles = {};
292296
let helperId;
297+
let focused = false;
293298
let addLayoutListener = getContext('SMUI:addLayoutListener');
294299
let removeLayoutListener;
295300
let initPromiseResolve;
@@ -351,9 +356,11 @@
351356
.map((mutation) => mutation.attributeName)
352357
.filter((attributeName) => attributeName);
353358
};
354-
const observer = new MutationObserver((mutationsList) =>
355-
handler(getAttributesList(mutationsList))
356-
);
359+
const observer = new MutationObserver((mutationsList) => {
360+
if (useNativeValidation) {
361+
handler(getAttributesList(mutationsList));
362+
}
363+
});
357364
const config = { attributes: true };
358365
observer.observe(input.getElement(), config);
359366
return observer;

site/src/routes/demo/textfield/_Showcase.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
</div>
3636

3737
<pre
38-
class="status">Value: {value}, Focused: {focused}, Dirty: {dirty}, Invalid: {invalid}</pre>
38+
class="status">Focused: {focused}, Dirty: {dirty}, Invalid: {invalid}, Value: {value}</pre>
3939

4040
<script>
4141
import Textfield from '@smui/textfield';

0 commit comments

Comments
 (0)