docs(tabs): add documentation to Tabs (#535)

This commit is contained in:
Valentin271 2023-09-26 08:22:14 +02:00 committed by GitHub
parent 082cbcbc50
commit 3bda372847
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 12 deletions

View File

@ -98,12 +98,8 @@ fn ui(f: &mut Frame, app: &App) {
let tabs = Tabs::new(titles) let tabs = Tabs::new(titles)
.block(Block::default().borders(Borders::ALL).title("Tabs")) .block(Block::default().borders(Borders::ALL).title("Tabs"))
.select(app.index) .select(app.index)
.style(Style::default().fg(Color::Cyan)) .style(Style::default().cyan().on_gray())
.highlight_style( .highlight_style(Style::default().bold().on_black());
Style::default()
.add_modifier(Modifier::BOLD)
.bg(Color::Black),
);
f.render_widget(tabs, chunks[0]); f.render_widget(tabs, chunks[0]);
let inner = match app.index { let inner = match app.index {
0 => Block::default().title("Inner 0").borders(Borders::ALL), 0 => Block::default().title("Inner 0").borders(Borders::ALL),

View File

@ -1,3 +1,4 @@
#![deny(missing_docs)]
use crate::{ use crate::{
buffer::Buffer, buffer::Buffer,
layout::Rect, layout::Rect,
@ -7,18 +8,22 @@ use crate::{
widgets::{Block, Widget}, widgets::{Block, Widget},
}; };
/// A widget to display available tabs in a multiple panels context. /// A widget that displays a horizontal set of Tabs with a single tab selected.
/// ///
/// # Examples /// Each tab title is stored as a [`Line`] which can be individually styled. The selected tab is set
/// using [`Tabs::select`] and styled using [`Tabs::highlight_style`]. The divider can be customized
/// with [`Tabs::divider`].
///
/// # Example
/// ///
/// ``` /// ```
/// use ratatui::{prelude::*, widgets::*}; /// use ratatui::{prelude::*, widgets::*};
/// ///
/// let titles = ["Tab1", "Tab2", "Tab3", "Tab4"].iter().cloned().map(Line::from).collect(); /// Tabs::new(vec!["Tab1", "Tab2", "Tab3", "Tab4"])
/// Tabs::new(titles)
/// .block(Block::default().title("Tabs").borders(Borders::ALL)) /// .block(Block::default().title("Tabs").borders(Borders::ALL))
/// .style(Style::default().fg(Color::White)) /// .style(Style::default().white())
/// .highlight_style(Style::default().fg(Color::Yellow)) /// .highlight_style(Style::default().yellow())
/// .select(2)
/// .divider(symbols::DOT); /// .divider(symbols::DOT);
/// ``` /// ```
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)] #[derive(Debug, Default, Clone, Eq, PartialEq, Hash)]
@ -38,6 +43,24 @@ pub struct Tabs<'a> {
} }
impl<'a> Tabs<'a> { impl<'a> Tabs<'a> {
/// Creates new `Tabs` from their titles.
///
/// `titles` can be a [`Vec`] of [`&str`], [`String`] or anything that can be converted into
/// [`Line`]. As such, titles can be styled independently.
///
/// # Examples
///
/// Basic titles.
/// ```
/// # use ratatui::{prelude::*, widgets::Tabs};
/// let tabs = Tabs::new(vec!["Tab 1", "Tab 2"]);
/// ```
///
/// Styled titles
/// ```
/// # use ratatui::{prelude::*, widgets::Tabs};
/// let tabs = Tabs::new(vec!["Tab 1".red(), "Tab 2".blue()]);
/// ```
pub fn new<T>(titles: Vec<T>) -> Tabs<'a> pub fn new<T>(titles: Vec<T>) -> Tabs<'a>
where where
T: Into<Line<'a>>, T: Into<Line<'a>>,
@ -52,26 +75,55 @@ impl<'a> Tabs<'a> {
} }
} }
/// Surrounds the `Tabs` with a [`Block`].
pub fn block(mut self, block: Block<'a>) -> Tabs<'a> { pub fn block(mut self, block: Block<'a>) -> Tabs<'a> {
self.block = Some(block); self.block = Some(block);
self self
} }
/// Sets the selected tab.
///
/// The first tab has index 0 (this is also the default index).
/// The selected tab can have a different style with [`Tabs::highlight_style`].
pub fn select(mut self, selected: usize) -> Tabs<'a> { pub fn select(mut self, selected: usize) -> Tabs<'a> {
self.selected = selected; self.selected = selected;
self self
} }
/// Sets the style of the tabs.
///
/// This will set the given style on the entire render area.
/// More precise style can be applied to the titles by styling the ones given to [`Tabs::new`].
/// The selected tab can be styled differently using [`Tabs::highlight_style`].
pub fn style(mut self, style: Style) -> Tabs<'a> { pub fn style(mut self, style: Style) -> Tabs<'a> {
self.style = style; self.style = style;
self self
} }
/// Sets the style for the highlighted tab.
///
/// Highlighted tab can be selected with [`Tabs::select`].
pub fn highlight_style(mut self, style: Style) -> Tabs<'a> { pub fn highlight_style(mut self, style: Style) -> Tabs<'a> {
self.highlight_style = style; self.highlight_style = style;
self self
} }
/// Sets the string to use as tab divider.
///
/// By default, the divider is a pipe (`|`).
///
/// # Examples
///
/// Use a dot (`•`) as separator.
/// ```
/// # use ratatui::{prelude::*, widgets::Tabs, symbols};
/// let tabs = Tabs::new(vec!["Tab 1", "Tab 2"]).divider(symbols::DOT);
/// ```
/// Use dash (`-`) as separator.
/// ```
/// # use ratatui::{prelude::*, widgets::Tabs, symbols};
/// let tabs = Tabs::new(vec!["Tab 1", "Tab 2"]).divider("-");
/// ```
pub fn divider<T>(mut self, divider: T) -> Tabs<'a> pub fn divider<T>(mut self, divider: T) -> Tabs<'a>
where where
T: Into<Span<'a>>, T: Into<Span<'a>>,