From 9fc291cce8a9cda52947b955ea98b7a9217bbe3d Mon Sep 17 00:00:00 2001 From: kennyfrc Date: Sun, 26 Nov 2023 17:12:29 +0800 Subject: [PATCH] examples: v0.3.0 --- examples/001_counter.html | 7 --- examples/002_formval.html | 73 +++++++++++------------ examples/003_todo.html | 7 --- examples/004_cart.html | 8 --- examples/005_nested1.html | 10 +--- examples/006_nested2.html | 7 --- examples/007_nested3.html | 4 -- examples/008_nested4.html | 7 --- examples/009_dataFromProps.html | 10 +--- examples/010_taskmgmt.html | 7 --- examples/011_playlist.html | 7 --- examples/012_blog.html | 7 --- examples/XXX_counter.html | 12 +--- examples/partials/_001_counter.html | 7 --- examples/partials/_002_formval.html | 73 +++++++++++------------ examples/partials/_003_todo.html | 7 --- examples/partials/_004_cart.html | 8 --- examples/partials/_005_nested1.html | 10 +--- examples/partials/_006_nested2.html | 7 --- examples/partials/_007_nested3.html | 4 -- examples/partials/_008_nested4.html | 7 --- examples/partials/_009_dataFromProps.html | 10 +--- examples/partials/_010_taskmgmt.html | 7 --- examples/partials/_011_playlist.html | 7 --- examples/partials/_012_blog.html | 7 --- examples/partials/_XXX_counter.html | 12 +--- 26 files changed, 86 insertions(+), 246 deletions(-) diff --git a/examples/001_counter.html b/examples/001_counter.html index d360284a..8b5c096b 100644 --- a/examples/001_counter.html +++ b/examples/001_counter.html @@ -21,13 +21,6 @@

Counter

class CounterElement extends ReactiveElement { count = 0 - constructor() { - super(); - this.setup({ - observables: ['count'] - }) - } - template() { return html` diff --git a/examples/002_formval.html b/examples/002_formval.html index 8c85640a..9d2c201b 100644 --- a/examples/002_formval.html +++ b/examples/002_formval.html @@ -21,21 +21,34 @@

Registration Form

const { html, ReactiveElement } = cami; class FormElement extends ReactiveElement { - emailError = ''; - passwordError = ''; - - inputStream = null; - + emailError = '' + passwordError = '' email = ''; password = ''; emailIsValid = null; isEmailAvailable = null; - constructor() { - super(); - this.setup({ - observables: ['emailError', 'passwordError'] - }) + inputValidation$ = this.stream(); + passwordValidation$ = this.stream(); + + onConnect() { + this.inputValidation$ + .map(e => this.validateEmail(e.target.value)) + .debounce(300) + .subscribe(({ isEmailValid, emailError, email }) => { + this.emailError = emailError; + this.isEmailValid = isEmailValid; + this.email = email; + this.isEmailAvailable = this.queryEmail(this.email) + }); + + this.passwordValidation$ + .map(e => this.validatePassword(e.target.value)) + .debounce(300) + .subscribe(({ isValid, password }) => { + this.passwordError = isValid ? '' : 'Password must be at least 8 characters long.'; + this.password = password; + }); } validateEmail(email) { @@ -52,11 +65,18 @@

Registration Form

emailError = ''; isEmailValid = true; } - return { isEmailValid, emailError }; + return { isEmailValid, emailError, email }; } validatePassword(password) { - return password !== '' && password?.length >= 8; + let isValid = false; + if (password === '') { + isValid = null; + } else if (password?.length >= 8) { + isValid = true; + } + + return { isValid, password } } queryEmail(email) { @@ -96,36 +116,13 @@

Registration Form

Email: { - if (!this.inputStream) { - this.inputStream = this.eventStream(e) - this.inputStream - .map(e => this.validateEmail(e.target.value)) - .debounce(1000) - .subscribe(({ isEmailValid, emailError }) => { - this.emailError = emailError; - this.isEmailValid = isEmailValid; - this.email = e.target.value; - this.isEmailAvailable = this.queryEmail(this.email) - }); - } else { - this.inputStream.next(e); - } - }} value=${this.email}> - ${this.isEmailAvailable?.status === 'success' && this.isEmailAvailable?.data?.length > 0 && this.emailError === '' ? 'Email is already taken.' : ''} + @input=${(e) => this.inputValidation$.next(e) } value=${this.email}> + ${this.isEmailAvailable?.status === 'success' && this.isEmailAvailable?.data?.length > 0 && this.emailError === '' ? 'Email is already taken.' : ''} ${this.emailError}