Examples
Practical examples। Live demos।
कोई example चुनिए और फ़ुल-स्क्रीन में खोलिए: live preview और source code साथ-साथ।
कोई example चुनिए और फ़ुल-स्क्रीन में खोलिए: live preview और source code साथ-साथ।
state पढ़ने वाले functions के रूप में लिखे rules, field छूने के बाद दिखने वाले errors, pending state वाला debounced async check, और submit जो इन सबके पूरा होने का wait करता है।
import 'nuclo' const values = { username: '', email: '', password: '', confirm: '' } const touched = new Set<keyof typeof values>() let availability: 'idle' | 'checking' | 'taken' | 'free' = 'idle' let submitted: string | null = null const taken = ['ada', 'grace', 'linus'] // stands in for a server // Validation is a plain function over the state. It runs on every // update(), so the messages are always current. const errors = () => ({ username: values.username.length < 3 ? 'At least 3 characters' : availability === 'taken' ? 'Already taken' : null, email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(values.email) ? null : 'Enter a valid email', password: values.password.length < 8 ? 'At least 8 characters' : !/\d/.test(values.password) ? 'Include a number' : null, confirm: values.confirm === values.password ? null : 'Passwords do not match', }) const valid = () => Object.values(errors()).every((e) => e === null) && availability !== 'checking' // An async check: debounced, with a pending state the UI can show. let timer: ReturnType<typeof setTimeout> function checkUsername() { clearTimeout(timer) if (values.username.length < 3) { availability = 'idle'; return } availability = 'checking' timer = setTimeout(() => { availability = taken.includes(values.username.toLowerCase()) ? 'taken' : 'free' update() }, 500) } // A field shows its error only once it was touched (blurred): // red while invalid, green once it passes. function Field( key: keyof typeof values, text: string, type = 'text', placeholder = '', hint: () => string = () => '', ) { const show = () => touched.has(key) && errors()[key] const ok = () => touched.has(key) && !errors()[key] return div( label(text, ' ', small(hint)), input({ type, placeholder, value: () => values[key], style: () => ({ borderColor: show() ? '#ef4444' : ok() ? '#22c55e' : '' }), 'aria-invalid': () => String(!!show()), onInput: (e) => { values[key] = e.currentTarget.value if (key === 'username') checkUsername() update() }, onBlur: () => { touched.add(key); update() }, }), p({ role: 'alert' }, () => show() || ''), ) } export const SignUp = () => form( Field('username', 'Username', 'text', 'ada, grace and linus are taken', () => availability === 'checking' ? 'checking…' : availability === 'free' ? 'available' : ''), Field('email', 'Email', 'email', '[email protected]'), Field('password', 'Password', 'password', '8+ characters, one number'), Field('confirm', 'Confirm password', 'password'), div( // Disabled until every rule passes and the async check is done. button('Create account', { type: 'button', disabled: () => !valid(), onClick: () => { submitted = JSON.stringify({ username: values.username, email: values.email }) update() }, }), small(() => (valid() ? '' : 'Fix the fields above')), ), when(() => submitted !== null, p('Submitted ', code(() => submitted ?? ''))), )