Advanced form inputs and uploads
These controls handle values that need more than a plain text field: dates, numbers, files, filters, and bounded ranges. They keep a clear contract between what a person sees, what the browser changes, and what the form or LiveView sends to the server. See Form controls for the wider control model, Text and structured inputs for text-like fields, and Selection controls for discrete choices.
Calendar selection
The calendar in lib/petal_components/calendar.ex presents a month grid for choosing dates without time or time zone data. It accepts and returns %Date{} values inside the component, while user actions carry ISO 8601 date strings such as 2026-03-14.
Selection shapes
The mode determines the value shape and the meaning of a click:
| Mode | Value | Selection behavior |
|---|---|---|
single | One %Date{} or nil | One day is selected. |
range | {from, to} or a map with from and to | The first day is the anchor. The second day completes the range. Either side can be nil during selection. |
multiple | A list of %Date{} values | Each selected day is kept in the list. |
min and max define an inclusive date window. disabled_dates adds fixed or computed blackout dates. Disabled days remain visible and reachable by keyboard, but they do not accept selection.
References: lib/…/calendar.ex• L49-67 lib/…/calendar.ex• L481-497
Month navigation
The calendar supports two server wiring choices. on_select and on_month_change send events with ISO values for a LiveView to own. Without those event names, day buttons can submit an ISO value in a form and month arrows become patch links with a query parameter. A dead view can therefore submit a date, but a LiveView must handle the month parameter if it expects patch navigation to change the displayed month.
References: lib/…/calendar.ex• L18-47 lib/…/calendar.ex• L204-225 lib/…/calendar.ex• L339-371
Keyboard behavior
The browser-side PetalCalendar behavior keeps one day as the tab stop. Arrow keys move by day or week, Home and End move to the week edges, and Page Up and Page Down change the month. Shift with a page key changes the year. Movement stops at min or max, and the selected or current date is preferred when the tab stop is rebuilt.
The calendar has no time-zone conversion. today defaults to UTC today, so an application serving users in another zone must provide the local date it wants the control to mark as today. Day and month labels are caller-provided, which lets the application connect them to its own translation system.
References: lib/…/calendar.ex• L69-89 lib/…/calendar.ex• L163-202 assets/…/petal_components.js• L9044-9217
Date-picker selection
The date picker in lib/petal_components/date_picker.ex joins a typeable text field to a calendar panel. The display is for people and follows format; hidden fields hold the ISO value that the form posts.
Value contract
Single mode posts one hidden field with the configured name. Range mode posts name[from] and name[to]. The visible text input is not named, so changing its display format does not change the server value. The component accepts a Date, an ISO string, or a range pair and also accepts date-time values by taking their date part.
The input parser first accepts YYYY-MM-DD, then tries the configured date format. It accepts reasonable differences in padding and letter case but rejects impossible dates. A failed parse restores the last valid display and leaves the posted value unchanged. A partial range can keep its first or second side without pretending that the range is complete.
Panel interactions
The panel can open from the input or the calendar button. Opening from the input keeps the caret in the input; opening from the button moves focus into the grid. Escape and click-away close the panel and return focus to the input. Enter commits a complete value and closes the panel. In range mode, the first day keeps the panel open and the second day completes the range and closes it.
The browser-side picker previews a complete typed date while the panel is open. It can update the hidden value and show the matching month without changing the text or caret. Incomplete text does not trigger a preview. When on_select is set, the server owns the value, so preview waits and a committed typed date sends the same selection event as a calendar click. Without on_select, the hook owns the hidden value and dispatches input and change events for plain forms and phx-change forms.
The interaction flow separates human display text from the committed form value:
flowchart LR
A[Type or choose a date] --> B{Complete and valid?}
B -- No --> C[Keep last valid value or partial range]
B -- Yes --> D[Store ISO date]
D --> E[Update calendar month and selection]
E --> F{Server owns value?}
F -- Yes --> G[Push selection event]
F -- No --> H[Dispatch form change]
References: lib/…/date_picker.ex• L41-89 lib/…/date_picker.ex• L254-273 assets/…/petal_components.js• L9578-9688
The errors, help_text, required, and disabled states follow the normal field surface. A picker with server-owned selection also needs a clear event if the application wants an empty input to clear the value; otherwise the picker restores the value that will still post.
References: lib/…/date_picker.ex• L15-98 lib/…/date_picker.ex• L108-183 lib/…/date_picker.ex• L192-325 assets/…/petal_components.js• L9310-9780
Number fields
The number field in lib/petal_components/number_field.ex provides a numeric spinbutton for quantities, prices, and percentages. It uses a text input with decimal input mode so a person can type an incomplete number without the browser replacing it with an empty value.
Numeric contract
The field posts the input's raw text under its name. min and max describe the allowed range, while step controls normal changes and big_step controls larger changes. A blank or unparseable value has no numeric aria-valuenow and does not claim to be at either bound.
The browser hook clamps values when a step action or blur commit applies a bound. It does not clamp each keystroke. This lets a person type 15 into a field whose intermediate 1 would otherwise be treated as a final value. precision rounds and pads the visible value on blur; it is display formatting, not a replacement for server-side numeric validation.
Interaction states
The input is the only tab stop. Optional buttons change the value but stay out of the normal tab order. A bound button uses aria-disabled so assistive technology can discover the control, while a fully disabled field uses native disabled behavior.
| Input | Change |
|---|---|
| Arrow Up or Down | Move by step. Shift uses big_step. |
| Page Up or Down | Move by big_step. |
| Home or End | Move to min or max when that bound exists. |
| Focused mouse wheel | Move by step; an unfocused field does not react. |
| Button press and hold | Make one change, then repeat at an increasing rate. |
The component can render stacked buttons, split buttons, or no buttons. In all variants the server must validate the submitted number and its bounds because the custom text input does not provide native number-field constraint validation.
References: lib/…/number_field.ex• L45-77 lib/…/number_field.ex• L156-182 assets/…/petal_components.js• L1813-1915
File uploads
The file upload in lib/petal_components/file_upload.ex is a presentation layer for a Phoenix LiveView upload configuration. It shows the drop surface, selected entries, previews, progress, cancel actions, and errors; allow_upload/3 performs validation and transfer, and consume_uploaded_entries/3 finishes storage after submit.
Upload lifecycle
The parent LiveView owns the upload name and rules. The form must have a phx-change binding so selected entries reach the server. On submit, the parent consumes the accepted entries and decides where to store them. A cancel action sends the entry reference back to the parent, which calls cancel_upload/3.
sequenceDiagram
participant Person
participant Browser
participant LiveView
participant Storage
Person->>Browser: Choose or drop files
Browser->>LiveView: phx-change with upload entries
LiveView-->>Browser: Validate and report progress
Person->>Browser: Cancel or submit
Browser->>LiveView: Cancel ref or submit event
LiveView->>Storage: Consume accepted entries
References: lib/…/file_upload.ex• L14-68 lib/…/file_upload.ex• L172-206
Upload states
The upload config supplies accepted types, maximum size, maximum entry count, progress, and errors. Config-level errors appear above the list. Entry-level errors stay with the file row, where the file name and problem are read together. Image entries get previews; other files get a type icon. A custom entry slot replaces the complete default row, so the application then owns its progress and cancel presentation.
Four surfaces use the same upload contract:
| Variant | Product use |
|---|---|
dropzone | Full drop area with a file list. |
compact | Browse control with a list and no large drop area. |
avatar | One circular image that a new file replaces. |
gallery | Preview tiles with an add tile until the entry limit is reached. |
Saved files are separate from in-flight upload entries. The existing slot displays their URLs and can send an application-defined remove event. They do not have upload progress and do not count toward LiveView's max_entries cap.
Drag and drop uses LiveView's drop target behavior. The component does not need a custom browser hook for it. A plain styled file input from PetalComponents.Field is the correct choice when the page is not a LiveView.
References: lib/…/file_upload.ex• L70-112 lib/…/file_upload.ex• L147-166 lib/…/file_upload.ex• L368-453
Filter controls
The filter bar in lib/petal_components/filters.ex gives a list or table a shared way to add, edit, remove, and clear filters. It is driven by the same state model as a data table, so the bar and table can show one filter state rather than maintaining separate copies.
Filter wiring
The bar has two modes:
| Mode | State change |
|---|---|
Link mode with path | Changes patch a URL encoded from the filter state. handle_params reads the URL and runs the query. |
Event mode with on_change | Apply, remove, and clear actions push operation-shaped events that the state handler can process. |
The application registers each filterable field through the field slot. The field type limits the operators and chooses the value editor. This registry is the contract between what the user can select and what the query state can execute.
References: lib/…/filters.ex• L65-81 lib/…/filters.ex• L113-185 lib/…/filters.ex• L427-463
Filter value editors
| Field type | Value contract |
|---|---|
text | One text value with text operators such as contains or starts with. |
select | One value from the supplied options. |
multi | A list of selected option values sent as values[]. |
number_range | One number or two bounds for between. |
date_range | One date or two dates for between. |
boolean | true or false with the equality operator fixed. |
Operators such as is_empty and is_not_empty have no value field. A between operator has a second bound. The bar renders active filters as chips, and reopening a chip restores its operator and value editor. Removing a chip or clearing all filters also moves focus to a still-present control and announces the new state.
The filter editors use simple browser inputs. Their main validation boundary is the shared state parser and the query layer, so the server must treat all submitted field names, operators, dates, numbers, and option values as untrusted input.
References: lib/…/filters.ex• L187-225 lib/…/filter_editor.ex• L1-25 lib/…/filter_editor.ex• L78-228
Slider inputs
The slider in lib/petal_components/slider.ex presents a bounded native range input with a styled track, fill, optional marks, and an optional value readout. The browser supplies the slider role, keyboard map, pointer behavior, and form submission; the component adds visual treatment and keeps it aligned with the current value.
Single and dual values
Single-thumb mode posts one value under name or a form field name. Dual-thumb mode posts two values: either min_field and max_field, or min_name and max_name with values={ [low, high] }. If a pair is reversed or outside the declared bounds, the server renders it in ordered, clamped form so the fill never points backward.
min, max, and step define the numeric contract. marks adds stops on the same scale. A mark with an empty label is still a tick, while a mark with a label adds text below the track. show_value can place a readout inline or above the thumb. Prefixes and suffixes affect the readout only, not the posted number.
References: lib/…/slider.ex• L18-32 lib/…/slider.ex• L34-76 lib/…/slider.ex• L95-147
Browser changes
The PetalSlider hook recalculates the fill, readout, and filled marks as the native input emits input. In dual mode it prevents the thumbs from crossing. When the thumbs meet, the thumb being moved is placed above the other one so the person can drag it back out.
Forms are the normal change path. Without a form, the wrapper emits a bubbling petal:slider-change event. Single mode sends detail: {value}; dual mode sends detail: {values: [min, max]}. The control also supports horizontal and vertical orientation, three sizes, and native disabled behavior. Form-field errors use the normal field error surface.
References: assets/…/petal_components.js• L556-659 lib/…/slider.ex• L149-159 lib/…/slider.ex• L325-423
Change checklist
When these files or sections are changed, remember to consider these:
- Keep calendar selection shapes and date-picker hidden field names aligned with the handler or changeset that reads them.
- Keep the date-picker display parser and formatter aligned when adding a supported date format or changing localized month and day labels.
- Preserve the
phx-changeupload binding and the upload entry reference used by cancel actions. A visual upload change without these server bindings leaves the entry out of the LiveView state. - Keep filter field types, operator subsets, editor names, and state parsing aligned. In particular,
betweenneedsvalue2, andmultineeds repeatedvalues[]parameters. - Keep slider dual names and ordering rules aligned across form fields, rendered thumbs, and the browser change event.
- Treat number and filter bounds as server contracts even when the browser hook clamps or formats them.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A date-picker LiveView ignores the month query parameter in link mode | Month arrows and typed dates outside the visible month appear to do nothing | Handle the parameter in handle_params/3, or use event mode and assign the new month. |
| A date-picker handler expects the formatted display text | A valid picker value fails changeset parsing when the display format is not ISO | Read the hidden ISO field or the date-picker field value. |
A file upload form has no phx-change binding | Selected entries never reach the LiveView and submit has nothing to consume | Bind phx-change on the upload form. |
| A custom upload entry row omits the cancel or progress contract | Users cannot cancel an entry or see its transfer state | Re-add the needed controls, or keep the default entry slot. |
| A number field relies only on client bounds | Crafted or non-browser input can bypass the hook's clamp | Validate numeric form data and bounds on the server. |
| A filter type offers an operator the query state does not support | The UI can submit a filter that the query layer cannot apply | Keep the registered operator subset within the shared state vocabulary. |
| Dual slider values cross without server normalization | The rendered fill and submitted lower and upper values disagree | Order and clamp the pair before using it as application state. |
Key files
| File | Why to read it |
|---|---|
lib/petal_components/calendar.ex | Defines date selection modes, date limits, month wiring, and the calendar value contract. |
lib/petal_components/date_picker.ex | Defines the display-versus-posted date contract and field error integration. |
| assets/…/petal_components.js | Defines browser behavior for calendar focus, date parsing, number stepping, and slider updates. |
lib/petal_components/number_field.ex | Defines numeric bounds, precision behavior, keyboard actions, and form-field states. |
lib/petal_components/file_upload.ex | Defines the LiveView upload surface, entry states, variants, and existing-file boundary. |
lib/petal_components/filters.ex | Defines filter registration, link and event wiring, active filter chips, and editor selection. |
lib/petal_components/data_table/filter_editor.ex | Defines the shared number, date, option, boolean, and text filter value contracts. |
lib/petal_components/slider.ex | Defines single and dual slider values, bounds, marks, readouts, and form names. |