File input
A native file picker with a button for a label, and the chosen files listed with a remove button each.
Previews are drawn in the theme you made.
Edit itSeveral files #
Images only, any number.
import { FileInput, FileInputSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { fileList, Message } from '../../snippets/fileList.js'
// The application owns the list (`fileList` holds it and writes it back into
// the input); the trigger is the input's `label`, so a press opens the
// browser's dialog with no script between. A second pick adds to the list.
export default example({
title: 'Several files',
slots: FileInputSlots,
recipe: Recipes.FileInput,
state: fileList('attachments', { multiple: true }),
view: ({ style, model, onMessage, h }) =>
FileInput.view(
{
id: 'attachments',
name: 'attachments',
label: 'Attach files',
description: 'Images only, any number.',
accept: ['image/*'],
multiple: true,
files: model.files,
onChange: files => onMessage(Message.Picked({ files })),
onRemove: file => onMessage(Message.Removed({ file })),
words: { remove: 'Take {name} off' },
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.
One file, required #
A CSV file is required.
import { FileInput, FileInputSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { fileList, Message } from '../../snippets/fileList.js'
// A single file replaces the last pick. `required` is the browser's own, and
// `invalid` with a `description` says why the form would not submit.
export default example({
title: 'One file, required',
slots: FileInputSlots,
recipe: Recipes.FileInput,
state: fileList('statement', { multiple: false }),
view: ({ style, model, onMessage, h }) =>
FileInput.view(
{
id: 'statement',
name: 'statement',
label: 'Choose a statement',
description:
model.files.length === 0
? 'A CSV file is required.'
: 'One file; choosing again replaces it.',
accept: ['.csv', 'text/csv'],
required: true,
invalid: model.files.length === 0,
files: model.files,
onChange: files => onMessage(Message.Picked({ files })),
onRemove: file => onMessage(Message.Removed({ file })),
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
- Keeping focus when something is removed A control that leaves the page takes its focus with it: say where focus goes next, to a neighbour (
FocusRecovery), to the opener (focusReturn) or to a control (keepFocus). - What a form submits Give the widget a name and a form reads it as it reads a native field.