use fission::prelude::*;
#[derive(Debug)]
pub struct OrderState {
quantity: u16,
quantity_draft: String,
}
impl Default for OrderState {
fn default() -> Self {
Self { quantity: 1, quantity_draft: "1".into() }
}
}
impl GlobalState for OrderState {}
fission_component! {
pub OrderQuantity<OrderState> {}
view(_field, state, cx) {
let invalid = state.quantity_draft.parse::<u16>()
.map_or(true, |value| !(1..=10).contains(&value));
} =>
FormControl(
label: Some(cx.tr("order.quantity")),
error: if invalid { Some(cx.tr("order.quantity_range")) } else { None },
child: NumberInput(
value: state.quantity as f32,
display_text: Some(state.quantity_draft.clone()),
min: 1.0,
max: 10.0,
step: 1.0,
label: Some(cx.tr("order.quantity")),
on_input: reduce {
if let Some(change) = cx.input.text_change() {
state.quantity_draft = change.new_text.clone();
if let Ok(value) = state.quantity_draft.parse::<u16>() {
if (1..=10).contains(&value) {
state.quantity = value;
}
}
}
},
on_increment: reduce {
state.quantity = state.quantity.saturating_add(1).min(10);
state.quantity_draft = state.quantity.to_string();
},
on_decrement: reduce {
state.quantity = state.quantity.saturating_sub(1).max(1);
state.quantity_draft = state.quantity.to_string();
},
),
)
}
Field | Type | Meaning | Notes / default behavior |
|---|---|---|---|
id | Option<WidgetId> | Stable identity for the inner text field. | Defaults to None. |
value | f32 | Current numeric value. | Defaults to 0.0. |
display_text | Option<String> | Exact text shown in the field. | If None, the widget formats value with format!("{}", value). |
min | Option<f32> | Intended minimum value. | Exposed in range semantics; enforce it in the reducer. |
max | Option<f32> | Intended maximum value. | Exposed in range semantics; enforce it in the reducer. |
step | f32 | Intended adjustment step. | Defaults to 1.0, but the current widget does not apply it automatically. Your reducer does. |
field_width | Option<f32> | Width of the central text field. | If None, width is estimated from display_text. |
button_size | Option<f32> | Size of the plus and minus buttons. | Uses tokens.sizing.control_md, bounded below by min_pointer_target. |
gap | Option<f32> | Spacing between the buttons and field. | Uses the number-input recipe gap or tokens.spacing.xs. |
on_increment | Option<ActionEnvelope> | Action fired by the plus button. | Defaults to None. |
on_decrement | Option<ActionEnvelope> | Action fired by the minus button. | Defaults to None. |
label | Option<String> | Accessible name for the numeric adjustment. | Supply the same operation name as the visible form label. |
on_input | Option<ActionEnvelope> | Action fired for typed edits in the central field. | Preserves the bound action; parse ctx.input.text_change().new_text in the reducer. |