Password input
A password field with a toggle that shows it as text.
Previews are drawn in the theme you made.
Edit itBasic #
At least twelve characters.
import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { modifyFields } from 'foldkit/struct'
import { Bundles, PasswordInput, PasswordInputSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
// The text is the application's own field; whether it shows is the
// PasswordInput's, so the Bundle holds that and `controls` hands it to the view.
const App = Bundle.compose({ password: Schema.String }).pipe(
Bundle.withMessages({ Typed: { password: Schema.String } }),
Bundle.withChild('field', Bundles.PasswordInput, { args: {} }),
)
export default example({
title: 'Basic',
slots: PasswordInputSlots,
recipe: Recipes.PasswordInput,
state: App.placements.runtime({
Model: App.Model,
initial: { password: '' },
update: App.placements.update((model, { password }) => ({
model: modifyFields(model, { password: () => password }),
})),
}),
view: ({ style, model, onMessage, h }) =>
PasswordInput.view(
{
id: 'password',
label: 'Password',
description: 'At least twelve characters.',
value: model.password,
onInput: password => onMessage(App.Message.Typed({ password })),
...PasswordInput.controls(model.field, message =>
onMessage(App.Message.GotFieldMessage({ message })),
),
style,
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
A new password #
Strength
- At least 12 characters, not met
- A number, not met
- A symbol, not met
import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { modifyFields } from 'foldkit/struct'
import { Bundles, Meter, PasswordInput, PasswordInputSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Gauge = Recipes.Meter.style({})
const App = Bundle.compose({ password: Schema.String }).pipe(
Bundle.withMessages({ Typed: { password: Schema.String } }),
Bundle.withChild('field', Bundles.PasswordInput, { args: {} }),
)
// A strength Meter over the rules kept, the rules listed under the field,
// and a hint while Caps Lock is on, read from each key typed. The Meter is
// the application's drawing, so a page with none ships no Meter.
export default example({
title: 'A new password',
slots: PasswordInputSlots,
recipe: Recipes.PasswordInput,
state: App.placements.runtime({
Model: App.Model,
initial: { password: '' },
update: App.placements.update((model, { password }) => ({
model: modifyFields(model, { password: () => password }),
})),
}),
view: ({ style, model, onMessage, h }) => {
const rules = [
{ label: 'At least 12 characters', met: model.password.length >= 12 },
{ label: 'A number', met: /[0-9]/.test(model.password) },
{ label: 'A symbol', met: /[^a-zA-Z0-9]/.test(model.password) },
]
return PasswordInput.view(
{
id: 'new-password',
label: 'New password',
autocomplete: 'new-password',
value: model.password,
onInput: password => onMessage(App.Message.Typed({ password })),
...PasswordInput.controls(model.field, message =>
onMessage(App.Message.GotFieldMessage({ message })),
),
strength: Meter.view(
{
id: 'strength',
label: 'Strength',
value: rules.filter(rule => rule.met).length,
max: 3,
style: Gauge,
},
h,
),
requirements: rules,
style,
},
h,
)
},
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Sign in, in French #
Oublié ?
import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { modifyFields } from 'foldkit/struct'
import { Bundles, PasswordInput, PasswordInputSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const App = Bundle.compose({ password: Schema.String }).pipe(
Bundle.withMessages({ Typed: { password: Schema.String } }),
Bundle.withChild('field', Bundles.PasswordInput, { args: { visible: true } }),
)
// `labelAside` stands beside the label, and
// `words` renames the toggle: both are the application's to draw.
export default example({
title: 'Sign in, in French',
slots: PasswordInputSlots,
recipe: Recipes.PasswordInput,
state: App.placements.runtime({
Model: App.Model,
initial: { password: 'hunter2' },
update: App.placements.update((model, { password }) => ({
model: modifyFields(model, { password: () => password }),
})),
}),
view: ({ style, model, onMessage, h }) =>
PasswordInput.view(
{
id: 'sign-in',
label: 'Mot de passe',
labelAside: h.span([], ['Oublié ?']),
words: { toggle: 'Afficher le mot de passe' },
value: model.password,
onInput: password => onMessage(App.Message.Typed({ password })),
...PasswordInput.controls(model.field, message =>
onMessage(App.Message.GotFieldMessage({ message })),
),
style,
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Usage
Each example above is a module of its own, shown whole and run as shown, over the theme module Create a theme writes. The page hands it the style picked above.
Related
- What a form submits Give the widget a name and a form reads it as it reads a native field.