Select RS

Edit Component

Y Select RS Yew Usage

Adding Select RS to your project is simple:

  1. Make sure your project is set up with Yew. Follow their Getting Started Guide for setup instructions.

  2. Add the select-rs crate to your dependencies by including it in your Cargo.toml file:

    cargo add selectrs --features=yew
    
  3. Import the Select, Option, and Group components into your Yew component and start using them in your app.

🛠️ Usage

Incorporating Select RS into your application is easy. Follow these steps:

  1. Import the Select, Option, and Group components into your Yew project:

    use yew::prelude::*;
    use selectrs::yew::{Select, Option, Group};
  2. Use the Select component in your Yew application:

    use yew::prelude::*;
    use selectrs::yew::{Select, Option, Group};
    
    #[function_component(App)]
    pub fn app() -> Html {
        let onchange = Callback::from(|selected_values: Vec<String>| {
            log::info!("Selected: {:?}", selected_values);
        });
    
        html! {
            <Select
                class="custom-select"
                style="width: 250px"
                placeholder="Select an option..."
                onchange={onchange}
            >
                <Group>
                    <Option value="Option1" label="Option 1" />
                    <Option value="Option2" label="Option 2" />
                    <Option value="Option3" label="Option 3" />
                </Group>
            </Select>
        }
    }

🔧 Props

Select Component

Main Props

PropertyTypeDescriptionDefault
name&'static strThe name attribute of the select component, important for form submission.""
id&'static strThe unique ID for the select element.""
placeholder&'static strPlaceholder text displayed when no option is selected.""
multipleboolWhether multiple options can be selected.false
requiredboolMarks the field as required for form submission.false
sizeu64Number of visible options in the dropdown (applies only for multiple=true).0
form&'static strAssociates the select element with a specific form by its ID.""
autocomplete&'static strProvides an autocomplete hint.""
autofocusboolAutomatically focuses the select element on page load.false
childrenChildrenWithProps<Group>Child Group components containing options to render within the select box.""

Styling Props

+--------------------------------------------------+
|                  [Select Container]              |  <-- `class` & `style`
|                                                  |
|   +------------------------------------------+   |
|   |             [Selected Labels]            |   |  <-- Rendered only for `multiple=true`
|   |                                          |   |
|   |   +----------------------------------+   |   |
|   |   |         [Label (Chip)]           |   |   |  <-- `label_class` & `label_style`
|   |   |    +--------------------------+  |   |   |
|   |   |    |    Close Button ("x")    |  |   |   |  <-- `close_class` & `close_style`
|   |   |    +--------------------------+  |   |   |
|   |   +----------------------------------+   |   |
|   +------------------------------------------+   |
|                                                  |
|   +------------------------------------------+   |
|   |              [Select Element]            |   |  <-- `<select>` element
|   |                                          |   |
|   |   +----------------------------------+   |   |
|   |   |      [Option (Placeholder)]      |   |   |  <-- Rendered if `placeholder` is set
|   |   +----------------------------------+   |   |
|   |                                          |   |
|   |   +----------------------------------+   |   |
|   |   |        [Options Group]           |   |   |  <-- Rendered dynamically via children
|   |   +----------------------------------+   |   |
|   +------------------------------------------+   |
|                                                  |
+--------------------------------------------------+
PropertyTypeDescriptionDefault
class&'static strCSS class for the outer select container.""
style&'static strInline styles for the outer select container.""
labels_class&'static strCSS class for the label container.""
labels_style&'static strInline styles for the label container.""
label_class&'static strCSS class for individual labels.""
label_style&'static strInline styles for individual labels.""
close_class&'static strCSS class for the close button (multi-select).""
close_style&'static strInline styles for the close button.""
select_class&'static strCSS class for the dropdown select box.""
select_style&'static strInline styles for the dropdown select box.""

Behavioral Props

PropertyTypeDescriptionDefault
onchangeCallback<Vec<String>>Callback triggered when the selected values change.No-op

Group Component

Main Props

PropertyTypeDescriptionDefault
label&'static strText label for the group, useful for describing a set of options.""
groupboolIndicates whether this is a group of options.false
selectedStringThe currently selected option within the group.""
childrenChildrenWithProps<Option>Child Option components to display within this group.""

Styling Props

+--------------------------------------------------+
|               [OptGroup Container]               |  <-- Rendered if `group=true`
|              (`<optgroup>` element)              |
|   +------------------------------------------+   |
|   |         [Group Label/Text Header]        |   |  <-- `label` attribute (not styled)
|   +------------------------------------------+   |
|                                                  |
|   +------------------------------------------+   |
|   |              [Group Options]             |   |  <-- Rendered via child `Option` components
|   |   +----------------------------------+   |   |
|   |   |          [Option Label]          |   |   |
|   |   +----------------------------------+   |   |
|   +------------------------------------------+   |
|                                                  |
+--------------------------------------------------+
PropertyTypeDescriptionDefault
class&'static strCSS class for the group container.""
style&'static strInline styles for the group container.""

Behavioral Props

PropertyTypeDescriptionDefault
onchangeCallback<String>Callback triggered when the selected option changes.No-op

Option Component

Main Props

PropertyTypeDescriptionDefault
value&'static strThe underlying value associated with the option.""
labelChildrenContent displayed for the option, such as text or custom elements.None
selectedboolIndicates if the option is currently selected.false
disabledboolDisables the option, making it unselectable by the user.false

Styling Props

+--------------------------------------+
|         [Option Container]           |  <-- `<option>` element
|                                      |
|   +------------------------------+   |
|   |          [Label]             |   |  <-- Rendered dynamically via `label` prop
|   +------------------------------+   |
|                                      |
|      [Selected State Styling]        |  <-- Applies `selected_class` & `selected_style` if selected
+--------------------------------------+
PropertyTypeDescriptionDefault
class&'static strCSS class for the option container.""
style&'static strInline styles for the option container.""
selected_class&'static strCSS class applied when the option is selected.""
selected_style&'static strInline styles applied when the option is selected.""

Behavioral Props

PropertyTypeDescriptionDefault
on_clickCallback<()>Callback triggered when the option is clicked.No-op

💡 Notes

  • Use the Group component to organize related Option components within a Select component.
  • The onchange callback is triggered with a list of selected values.
  • If using the multiple prop, the size prop determines the number of visible options in the dropdown.
  • Use class and style props to fully customize the appearance of the Select component, either with your own CSS or css libraries like Tailwind, Bootstrap, etc.

Usage

CLI Usage

You can import Select RS directly into your project to customize and modify it using the opensass cli:

os add select-rs yew