mirror of https://github.com/sveltejs/svelte
fix dirtybit (#4095)
parent
6c0fb5be64
commit
f9a9484e39
@ -0,0 +1,22 @@
|
||||
export default {
|
||||
html: `
|
||||
<input class="input" placeholder="Type here" type="text">
|
||||
Dirty: false
|
||||
Valid: false
|
||||
`,
|
||||
|
||||
async test({ assert, target, window }) {
|
||||
const input = target.querySelector('input');
|
||||
|
||||
input.value = 'foo';
|
||||
const inputEvent = new window.InputEvent('input');
|
||||
|
||||
await input.dispatchEvent(inputEvent);
|
||||
|
||||
assert.htmlEqual(target.innerHTML, `
|
||||
<input class="input" placeholder="Type here" type="text">
|
||||
Dirty: true
|
||||
Valid: true
|
||||
`);
|
||||
},
|
||||
};
|
@ -0,0 +1,29 @@
|
||||
<script>
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
export function createValidator() {
|
||||
const { subscribe, set } = writable({ dirty: false, valid: false });
|
||||
|
||||
function action(node, binding) {
|
||||
return {
|
||||
update(value) {
|
||||
set({ dirty: true, valid: value !== '' });
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return [{ subscribe }, action];
|
||||
}
|
||||
const [validity, validate] = createValidator();
|
||||
let email = null;
|
||||
</script>
|
||||
|
||||
<input class="input"
|
||||
type="text"
|
||||
bind:value={email}
|
||||
placeholder="Type here"
|
||||
use:validate={email}
|
||||
/>
|
||||
|
||||
Dirty: {$validity.dirty}
|
||||
Valid: {$validity.valid}
|
Loading…
Reference in new issue