ratatui_kit/components/
view.rs

1//! View 组件:基础布局容器,支持 flex 布局、嵌套、间距、对齐等属性。
2//!
3//! 常用于包裹和组织多个子组件,是构建 UI 结构的基础。
4//!
5//! ## 示例
6//! ```rust
7//! element!(View(flex_direction: Direction::Vertical, gap: 1) {
8//!     element!(Child1()),
9//!     element!(Child2()),
10//! })
11//! ```
12//! 可通过 `flex_direction`、`gap`、`margin` 等属性灵活控制布局。
13
14use ratatui_kit_macros::{Props, with_layout_style};
15
16use crate::{AnyElement, Component};
17
18#[with_layout_style]
19#[derive(Default, Props)]
20/// View 组件属性。
21pub struct ViewProps<'a> {
22    /// 子元素列表。
23    pub children: Vec<AnyElement<'a>>,
24}
25
26/// View 组件实现。
27pub struct View;
28
29impl Component for View {
30    type Props<'a> = ViewProps<'a>;
31
32    fn new(_props: &Self::Props<'_>) -> Self {
33        Self
34    }
35
36    fn update(
37        &mut self,
38        props: &mut Self::Props<'_>,
39        _hooks: crate::Hooks,
40        updater: &mut crate::ComponentUpdater,
41    ) {
42        updater.set_layout_style(props.layout_style());
43        updater.update_children(&mut props.children, None);
44    }
45}