mirror of https://github.com/sveltejs/svelte
parent
0a6310f7a3
commit
8748c852ee
@ -0,0 +1,22 @@
|
|||||||
|
export default {
|
||||||
|
html: `
|
||||||
|
<input class="input" placeholder="Type here" type="text">
|
||||||
|
Dirty: false
|
||||||
|
Valid: false
|
||||||
|
`,
|
||||||
|
|
||||||
|
async test({ assert, component, 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