summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-09-19 06:18:00 +0200
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-09-19 06:18:00 +0200
commite0c55cbb19a6362b6a86bf71031ecaa26d673e1b (patch)
treecb9cf1732c30535b0b459cf85429bb7714a48147
parent184ebebfe12c9ea68b91a0a5db59f8b599b5bc5d (diff)
downloadiced-e0c55cbb19a6362b6a86bf71031ecaa26d673e1b.tar.gz
iced-e0c55cbb19a6362b6a86bf71031ecaa26d673e1b.tar.bz2
iced-e0c55cbb19a6362b6a86bf71031ecaa26d673e1b.zip
Show `text_input` doc example in multiple places
-rw-r--r--widget/src/helpers.rs33
-rw-r--r--widget/src/text_input.rs64
2 files changed, 84 insertions, 13 deletions
diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs
index ca401a89..cf8c0520 100644
--- a/widget/src/helpers.rs
+++ b/widget/src/helpers.rs
@@ -960,7 +960,38 @@ where
/// Creates a new [`TextInput`].
///
-/// [`TextInput`]: crate::TextInput
+/// Text inputs display fields that can be filled with text.
+///
+/// # 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::text_input;
+///
+/// struct State {
+/// content: String,
+/// }
+///
+/// #[derive(Debug, Clone)]
+/// enum Message {
+/// ContentChanged(String)
+/// }
+///
+/// fn view(state: &State) -> Element<'_, Message> {
+/// text_input("Type something here...", &state.content)
+/// .on_input(Message::ContentChanged)
+/// .into()
+/// }
+///
+/// fn update(state: &mut State, message: Message) {
+/// match message {
+/// Message::ContentChanged(content) => {
+/// state.content = content;
+/// }
+/// }
+/// }
+/// ```
pub fn text_input<'a, Message, Theme, Renderer>(
placeholder: &str,
value: &str,
diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs
index 3032dd13..5bbf76f5 100644
--- a/widget/src/text_input.rs
+++ b/widget/src/text_input.rs
@@ -1,6 +1,35 @@
-//! Display fields that can be filled with text.
+//! Text inputs display fields that can be filled with text.
//!
-//! A [`TextInput`] has some local [`State`].
+//! # 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::text_input;
+//!
+//! struct State {
+//! content: String,
+//! }
+//!
+//! #[derive(Debug, Clone)]
+//! enum Message {
+//! ContentChanged(String)
+//! }
+//!
+//! fn view(state: &State) -> Element<'_, Message> {
+//! text_input("Type something here...", &state.content)
+//! .on_input(Message::ContentChanged)
+//! .into()
+//! }
+//!
+//! fn update(state: &mut State, message: Message) {
+//! match message {
+//! Message::ContentChanged(content) => {
+//! state.content = content;
+//! }
+//! }
+//! }
+//! ```
mod editor;
mod value;
@@ -38,23 +67,34 @@ use crate::runtime::Action;
///
/// # Example
/// ```no_run
-/// # pub type TextInput<'a, Message> = iced_widget::TextInput<'a, Message>;
+/// # 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::text_input;
+///
+/// struct State {
+/// content: String,
+/// }
+///
/// #[derive(Debug, Clone)]
/// enum Message {
-/// TextInputChanged(String),
+/// ContentChanged(String)
/// }
///
-/// let value = "Some text";
+/// fn view(state: &State) -> Element<'_, Message> {
+/// text_input("Type something here...", &state.content)
+/// .on_input(Message::ContentChanged)
+/// .into()
+/// }
///
-/// let input = TextInput::new(
-/// "This is the placeholder...",
-/// value,
-/// )
-/// .on_input(Message::TextInputChanged)
-/// .padding(10);
+/// fn update(state: &mut State, message: Message) {
+/// match message {
+/// Message::ContentChanged(content) => {
+/// state.content = content;
+/// }
+/// }
+/// }
/// ```
-/// ![Text input drawn by `iced_wgpu`](https://github.com/iced-rs/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/text_input.png?raw=true)
#[allow(missing_debug_implementations)]
pub struct TextInput<
'a,