Skip to main content

euv_ui/component/select/view/
struct.rs

1use super::*;
2
3/// One entry of the [`euv_select`] option list.
4///
5/// Carries the machine value written into the selection signal, the label
6/// the user reads, the caption of the group the option belongs to, and
7/// whether the option can be picked at all.
8#[derive(Clone, Copy, CustomDebug, Data, Default, New)]
9pub struct EuvSelectOption {
10    /// The machine value written into the selection signal when picked.
11    #[get(type(copy))]
12    pub value: &'static str,
13    /// The display label shown for this option.
14    #[get(type(copy))]
15    pub label: &'static str,
16    /// The caption of the group this option belongs to.
17    ///
18    /// An empty caption places the option in the ungrouped block at the
19    /// top of the panel, which is rendered without a caption row.
20    #[get(type(copy))]
21    pub group: &'static str,
22    /// Whether a separator appears immediately before this option.
23    #[get(type(copy))]
24    pub separator_before: bool,
25    /// Whether the option is disabled.
26    ///
27    /// A disabled option is drawn with the muted treatment and cannot be
28    /// picked, so it never writes into the selection signal.
29    #[get(type(copy))]
30    pub disabled: bool,
31}
32
33/// Props for the [`euv_select`] component.
34///
35/// The selection lives in caller-owned signals: `value` for single mode and
36/// `values` when `multiple` is enabled. The panel open state is component-owned.
37#[derive(Clone, CustomDebug)]
38pub struct EuvSelectProps {
39    /// The signal holding the currently selected option value.
40    pub value: Signal<String>,
41    /// Whether the Select permits more than one selected option.
42    pub multiple: bool,
43    /// The selected option values used when `multiple` is enabled.
44    pub values: Signal<Vec<String>>,
45    /// The size of the trigger.
46    pub size: EuvSelectSize,
47    /// The options rendered in the panel, in display order.
48    pub options: Vec<EuvSelectOption>,
49    /// The text shown in the trigger while no option is selected.
50    pub placeholder: &'static str,
51    /// The group label above the trigger; an empty label renders no label.
52    pub label: &'static str,
53    /// Accessible name for the trigger when no visible label is rendered.
54    pub aria_label: String,
55    /// Space-separated ids for the trigger's descriptive text and error.
56    pub aria_describedby: &'static str,
57    /// The trigger id used to connect an external label to the control.
58    /// An empty id uses the generated Select id.
59    pub id: &'static str,
60    /// The form field name submitted by the hidden native control.
61    pub name: &'static str,
62    /// The id of the form that owns the hidden native control.
63    /// An empty value uses the nearest ancestor form.
64    pub form: &'static str,
65    /// The browser autofill hint applied to the single-value native control.
66    /// An empty value leaves the browser's default behavior enabled.
67    pub auto_complete: &'static str,
68    /// Whether the native form control is required.
69    pub required: bool,
70    /// Whether the control is invalid; the root and trigger expose that state
71    /// for field styling and assistive technology.
72    pub invalid: bool,
73    /// Whether the whole control is disabled.
74    pub disabled: bool,
75    /// Whether the user can inspect options without changing the value.
76    pub read_only: bool,
77    /// The caller-owned open signal; `None` leaves open state uncontrolled.
78    pub open: Option<Signal<bool>>,
79    /// The initial open state used when `open` is not provided.
80    pub default_open: bool,
81    /// Whether the selected item aligns with the trigger while the panel is open.
82    ///
83    /// Defaults to `true` with the shadcn scheme and `false` with other schemes.
84    pub align_item_with_trigger: bool,
85    /// Optional callback receiving the value of the picked option.
86    #[debug(skip)]
87    pub on_change: Option<Rc<dyn Fn(&'static str)>>,
88    /// Optional callback receiving the full multiple-selection value list.
89    #[debug(skip)]
90    pub on_change_values: Option<Rc<dyn Fn(Vec<String>)>>,
91    /// Optional callback invoked when the Select requests an open-state change.
92    #[debug(skip)]
93    pub on_open_change: Option<Rc<dyn Fn(bool)>>,
94}
95
96/// The native form-control values and behavior consumed by Select rendering.
97pub(crate) struct EuvSelectFormInputProps {
98    /// The form field name submitted by the native input.
99    pub(crate) name: &'static str,
100    /// The single selected value.
101    pub(crate) value: String,
102    /// The selected values for a multiple Select.
103    pub(crate) values: Vec<String>,
104    /// Whether the Select submits multiple values.
105    pub(crate) multiple: bool,
106    /// Whether the form control participates in required validation.
107    pub(crate) required: bool,
108    /// Whether the native control is disabled.
109    pub(crate) disabled: bool,
110    /// The external form id; empty means ancestor association.
111    pub(crate) form: &'static str,
112    /// The browser autofill hint; empty means default behavior.
113    pub(crate) auto_complete: &'static str,
114}
115
116/// The reactive state shared by each rendered Select option row.
117///
118/// Keeping the row's handlers together avoids duplicating the selection and
119/// panel signals across the helper's argument list.
120#[derive(Clone, CustomDebug)]
121pub(crate) struct EuvSelectOptionContext {
122    /// The selection signal written when a row is picked.
123    pub value: Signal<String>,
124    /// The signal holding all selected values in multiple mode.
125    pub values: Signal<Vec<String>>,
126    /// Whether option activation toggles membership instead of replacing a value.
127    pub multiple: bool,
128    /// The available options used for keyboard text navigation.
129    pub options: Rc<Vec<EuvSelectOption>>,
130    /// The open-state signal used to close the option panel.
131    pub open: Signal<bool>,
132    /// The callback invoked when the panel requests an open-state change.
133    #[debug(skip)]
134    pub on_open_change: Option<Rc<dyn Fn(bool)>>,
135    /// Whether activating an option leaves the selected value unchanged.
136    pub read_only: bool,
137    /// Whether the selected option should align with the trigger.
138    pub align_item_with_trigger: bool,
139    /// The CSS translation signal used by item alignment.
140    pub align_offset: Signal<String>,
141    /// Whether the panel can scroll toward earlier options.
142    pub scroll_up_visible: Signal<bool>,
143    /// Whether the panel can scroll down.
144    pub scroll_down_visible: Signal<bool>,
145    /// The current keyboard typeahead prefix.
146    pub typeahead_buffer: Signal<String>,
147    /// The generation that invalidates older typeahead reset timers.
148    pub typeahead_generation: Signal<u32>,
149    /// The optional callback invoked when a row is picked.
150    #[debug(skip)]
151    pub on_change: Option<Rc<dyn Fn(&'static str)>>,
152    /// The optional callback invoked after a multiple-selection change.
153    #[debug(skip)]
154    pub on_change_values: Option<Rc<dyn Fn(Vec<String>)>>,
155}
156
157/// Copyable signal handles used to align and focus the panel after it opens.
158#[cfg(target_arch = "wasm32")]
159#[derive(Clone, Copy)]
160pub(crate) struct EuvSelectOpenEffect {
161    /// The signal containing the trigger id.
162    pub(crate) trigger_id: Signal<String>,
163    /// The inline style that positions the portaled panel against its trigger.
164    pub(crate) panel_position_style: Signal<String>,
165    /// The inline maximum height for the panel's scrollable list.
166    pub(crate) viewport_position_style: Signal<String>,
167    /// The trigger-to-panel gap measured when the panel first opens.
168    pub(crate) panel_gap: Signal<f64>,
169    /// The controlled or component-owned open state.
170    pub(crate) open: Signal<bool>,
171    /// The single-selection value.
172    pub(crate) value: Signal<String>,
173    /// The multiple-selection values.
174    pub(crate) values: Signal<Vec<String>>,
175    /// Whether the Select accepts multiple values.
176    pub(crate) multiple: bool,
177    /// Whether the selected item aligns with the trigger.
178    pub(crate) align_item_with_trigger: bool,
179    /// The computed panel alignment offset.
180    pub(crate) align_offset: Signal<String>,
181    /// Whether the viewport can scroll toward earlier options.
182    pub(crate) scroll_up_visible: Signal<bool>,
183    /// Whether the viewport can scroll toward later options.
184    pub(crate) scroll_down_visible: Signal<bool>,
185}
186
187#[cfg(not(target_arch = "wasm32"))]
188#[derive(Clone, Copy)]
189pub(crate) struct EuvSelectOpenEffect;
190
191/// Props for the [`euv_select_scroll_up_button`] component.
192///
193/// Displays a scroll-up indicator for a long option list. The Select panel's
194/// native viewport remains responsible for scrolling.
195#[derive(Clone, CustomDebug, Default)]
196pub struct EuvSelectScrollUpButtonProps {}
197
198/// Props for the [`euv_select_scroll_down_button`] component.
199///
200/// Displays a scroll-down indicator for a long option list. The Select panel's
201/// native viewport remains responsible for scrolling.
202#[derive(Clone, CustomDebug, Default)]
203pub struct EuvSelectScrollDownButtonProps {}