Show combo_box doc example in multiple places
This commit is contained in:
parent
51f7ce7324
commit
3e6e669c4c
2 changed files with 166 additions and 5 deletions
|
|
@ -1,4 +1,59 @@
|
||||||
//! Display a dropdown list of searchable and selectable options.
|
//! Combo boxes display a dropdown list of searchable and selectable options.
|
||||||
|
//!
|
||||||
|
//! # Example
|
||||||
|
//! ```no_run
|
||||||
|
//! # mod iced { pub mod widget { pub use iced_widget::*; } pub use iced_widget::Renderer; pub use iced_widget::core::*; }
|
||||||
|
//! # pub type Element<'a, Message> = iced_widget::core::Element<'a, Message, iced_widget::Theme, iced_widget::Renderer>;
|
||||||
|
//! #
|
||||||
|
//! use iced::widget::combo_box;
|
||||||
|
//!
|
||||||
|
//! struct State {
|
||||||
|
//! fruits: combo_box::State<Fruit>,
|
||||||
|
//! favorite: Option<Fruit>,
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! #[derive(Debug, Clone)]
|
||||||
|
//! enum Fruit {
|
||||||
|
//! Apple,
|
||||||
|
//! Orange,
|
||||||
|
//! Strawberry,
|
||||||
|
//! Tomato,
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! #[derive(Debug, Clone)]
|
||||||
|
//! enum Message {
|
||||||
|
//! FruitSelected(Fruit),
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! fn view(state: &State) -> Element<'_, Message> {
|
||||||
|
//! combo_box(
|
||||||
|
//! &state.fruits,
|
||||||
|
//! "Select your favorite fruit...",
|
||||||
|
//! state.favorite.as_ref(),
|
||||||
|
//! Message::FruitSelected
|
||||||
|
//! )
|
||||||
|
//! .into()
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! fn update(state: &mut State, message: Message) {
|
||||||
|
//! match message {
|
||||||
|
//! Message::FruitSelected(fruit) => {
|
||||||
|
//! state.favorite = Some(fruit);
|
||||||
|
//! }
|
||||||
|
//! }
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! impl std::fmt::Display for Fruit {
|
||||||
|
//! fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
//! f.write_str(match self {
|
||||||
|
//! Self::Apple => "Apple",
|
||||||
|
//! Self::Orange => "Orange",
|
||||||
|
//! Self::Strawberry => "Strawberry",
|
||||||
|
//! Self::Tomato => "Tomato",
|
||||||
|
//! })
|
||||||
|
//! }
|
||||||
|
//! }
|
||||||
|
//! ```
|
||||||
use crate::core::event::{self, Event};
|
use crate::core::event::{self, Event};
|
||||||
use crate::core::keyboard;
|
use crate::core::keyboard;
|
||||||
use crate::core::keyboard::key;
|
use crate::core::keyboard::key;
|
||||||
|
|
@ -21,9 +76,60 @@ use std::fmt::Display;
|
||||||
|
|
||||||
/// A widget for searching and selecting a single value from a list of options.
|
/// A widget for searching and selecting a single value from a list of options.
|
||||||
///
|
///
|
||||||
/// This widget is composed by a [`TextInput`] that can be filled with the text
|
/// # Example
|
||||||
/// to search for corresponding values from the list of options that are displayed
|
/// ```no_run
|
||||||
/// as a Menu.
|
/// # mod iced { pub mod widget { pub use iced_widget::*; } pub use iced_widget::Renderer; pub use iced_widget::core::*; }
|
||||||
|
/// # pub type Element<'a, Message> = iced_widget::core::Element<'a, Message, iced_widget::Theme, iced_widget::Renderer>;
|
||||||
|
/// #
|
||||||
|
/// use iced::widget::combo_box;
|
||||||
|
///
|
||||||
|
/// struct State {
|
||||||
|
/// fruits: combo_box::State<Fruit>,
|
||||||
|
/// favorite: Option<Fruit>,
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[derive(Debug, Clone)]
|
||||||
|
/// enum Fruit {
|
||||||
|
/// Apple,
|
||||||
|
/// Orange,
|
||||||
|
/// Strawberry,
|
||||||
|
/// Tomato,
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[derive(Debug, Clone)]
|
||||||
|
/// enum Message {
|
||||||
|
/// FruitSelected(Fruit),
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// fn view(state: &State) -> Element<'_, Message> {
|
||||||
|
/// combo_box(
|
||||||
|
/// &state.fruits,
|
||||||
|
/// "Select your favorite fruit...",
|
||||||
|
/// state.favorite.as_ref(),
|
||||||
|
/// Message::FruitSelected
|
||||||
|
/// )
|
||||||
|
/// .into()
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// fn update(state: &mut State, message: Message) {
|
||||||
|
/// match message {
|
||||||
|
/// Message::FruitSelected(fruit) => {
|
||||||
|
/// state.favorite = Some(fruit);
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// impl std::fmt::Display for Fruit {
|
||||||
|
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
/// f.write_str(match self {
|
||||||
|
/// Self::Apple => "Apple",
|
||||||
|
/// Self::Orange => "Orange",
|
||||||
|
/// Self::Strawberry => "Strawberry",
|
||||||
|
/// Self::Tomato => "Tomato",
|
||||||
|
/// })
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct ComboBox<
|
pub struct ComboBox<
|
||||||
'a,
|
'a,
|
||||||
|
|
|
||||||
|
|
@ -902,7 +902,62 @@ where
|
||||||
|
|
||||||
/// Creates a new [`ComboBox`].
|
/// Creates a new [`ComboBox`].
|
||||||
///
|
///
|
||||||
/// [`ComboBox`]: crate::ComboBox
|
/// Combo boxes display a dropdown list of searchable and selectable options.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```no_run
|
||||||
|
/// # mod iced { pub mod widget { pub use iced_widget::*; } pub use iced_widget::Renderer; pub use iced_widget::core::*; }
|
||||||
|
/// # pub type Element<'a, Message> = iced_widget::core::Element<'a, Message, iced_widget::Theme, iced_widget::Renderer>;
|
||||||
|
/// #
|
||||||
|
/// use iced::widget::combo_box;
|
||||||
|
///
|
||||||
|
/// struct State {
|
||||||
|
/// fruits: combo_box::State<Fruit>,
|
||||||
|
/// favorite: Option<Fruit>,
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[derive(Debug, Clone)]
|
||||||
|
/// enum Fruit {
|
||||||
|
/// Apple,
|
||||||
|
/// Orange,
|
||||||
|
/// Strawberry,
|
||||||
|
/// Tomato,
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[derive(Debug, Clone)]
|
||||||
|
/// enum Message {
|
||||||
|
/// FruitSelected(Fruit),
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// fn view(state: &State) -> Element<'_, Message> {
|
||||||
|
/// combo_box(
|
||||||
|
/// &state.fruits,
|
||||||
|
/// "Select your favorite fruit...",
|
||||||
|
/// state.favorite.as_ref(),
|
||||||
|
/// Message::FruitSelected
|
||||||
|
/// )
|
||||||
|
/// .into()
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// fn update(state: &mut State, message: Message) {
|
||||||
|
/// match message {
|
||||||
|
/// Message::FruitSelected(fruit) => {
|
||||||
|
/// state.favorite = Some(fruit);
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// impl std::fmt::Display for Fruit {
|
||||||
|
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
/// f.write_str(match self {
|
||||||
|
/// Self::Apple => "Apple",
|
||||||
|
/// Self::Orange => "Orange",
|
||||||
|
/// Self::Strawberry => "Strawberry",
|
||||||
|
/// Self::Tomato => "Tomato",
|
||||||
|
/// })
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn combo_box<'a, T, Message, Theme, Renderer>(
|
pub fn combo_box<'a, T, Message, Theme, Renderer>(
|
||||||
state: &'a combo_box::State<T>,
|
state: &'a combo_box::State<T>,
|
||||||
placeholder: &str,
|
placeholder: &str,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue