Skip to content

Commit 541c409

Browse files
committed
fixed svg example
1 parent ea3e782 commit 541c409

File tree

7 files changed

+75
-46
lines changed

7 files changed

+75
-46
lines changed

.github/workflows/ci.yml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,23 @@ jobs:
2929
strategy:
3030
fail-fast: false
3131
matrix:
32-
example: [ bootstrap, custom_renderers_svg, custom_row_renderer, editable, generic, getter, paginated_rest_datasource, pagination, selectable, simple, tailwind ]
32+
example:
33+
- bootstrap
34+
- custom_renderers_svg
35+
- custom_row_renderer
36+
- editable
37+
- generic
38+
- getter
39+
- paginated_rest_datasource
40+
- pagination
41+
- selectable
42+
- serverfn_sqlx
43+
- simple
44+
- tailwind
3345
steps:
3446
- uses: actions/checkout@v3
3547
- uses: ./.github/actions/rust_toolchain/
36-
- name: Run example ${{ matrix.example }}
48+
- name: Build example ${{ matrix.example }}
3749
run: |
3850
cd ${{ github.workspace }}/examples/${{ matrix.example }}/
3951
cargo build

examples/custom_renderers_svg/src/main.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ fn main() {
2121

2222
mount_to_body(|| {
2323
let rows = vec![
24-
Form {
25-
name: "Heart".to_string(),
26-
path: "M12.82 5.58l-.82.822l-.824-.824a5.375 5.375 0 1 0-7.601 7.602l7.895 7.895a.75.75 0 0 0 1.06 0l7.902-7.897a5.376 5.376 0 0 0-.001-7.599a5.38 5.38 0 0 0-7.611 0zm6.548 6.54L12 19.485L4.635 12.12a3.875 3.875 0 1 1 5.48-5.48l1.358 1.357a.75.75 0 0 0 1.073-.012L13.88 6.64a3.88 3.88 0 0 1 5.487 5.48z".to_string(),
27-
},
28-
Form {
29-
name: "Bell".to_string(),
30-
path: "M12 1.996a7.49 7.49 0 0 1 7.496 7.25l.004.25v4.097l1.38 3.156a1.249 1.249 0 0 1-1.145 1.75L15 18.502a3 3 0 0 1-5.995.177L9 18.499H4.275a1.251 1.251 0 0 1-1.147-1.747L4.5 13.594V9.496c0-4.155 3.352-7.5 7.5-7.5zM13.5 18.5l-3 .002a1.5 1.5 0 0 0 2.993.145l.007-.147zM12 3.496c-3.32 0-6 2.674-6 6v4.41L4.656 17h14.697L18 13.907V9.509l-.003-.225A5.988 5.988 0 0 0 12 3.496z".to_string(),
31-
},
32-
Form {
33-
name: "Star".to_string(),
34-
path: "M10.788 3.102c.495-1.003 1.926-1.003 2.421 0l2.358 4.778l5.273.766c1.107.16 1.549 1.522.748 2.303l-3.816 3.719l.901 5.25c.19 1.104-.968 1.945-1.959 1.424l-4.716-2.48l-4.715 2.48c-.99.52-2.148-.32-1.96-1.423l.901-5.251l-3.815-3.72c-.801-.78-.359-2.141.748-2.302L8.43 7.88l2.358-4.778zm1.21.937L9.74 8.614a1.35 1.35 0 0 1-1.016.739l-5.05.734l3.654 3.562c.318.31.463.757.388 1.195l-.862 5.029l4.516-2.375a1.35 1.35 0 0 1 1.257 0l4.516 2.375l-.862-5.03a1.35 1.35 0 0 1 .388-1.194l3.654-3.562l-5.05-.734a1.35 1.35 0 0 1-1.016-.739L11.998 4.04z".to_string(),
35-
}
36-
];
24+
Form {
25+
name: "Heart".to_string(),
26+
path: "M12.82 5.58l-.82.822l-.824-.824a5.375 5.375 0 1 0-7.601 7.602l7.895 7.895a.75.75 0 0 0 1.06 0l7.902-7.897a5.376 5.376 0 0 0-.001-7.599a5.38 5.38 0 0 0-7.611 0zm6.548 6.54L12 19.485L4.635 12.12a3.875 3.875 0 1 1 5.48-5.48l1.358 1.357a.75.75 0 0 0 1.073-.012L13.88 6.64a3.88 3.88 0 0 1 5.487 5.48z".to_string(),
27+
},
28+
Form {
29+
name: "Bell".to_string(),
30+
path: "M12 1.996a7.49 7.49 0 0 1 7.496 7.25l.004.25v4.097l1.38 3.156a1.249 1.249 0 0 1-1.145 1.75L15 18.502a3 3 0 0 1-5.995.177L9 18.499H4.275a1.251 1.251 0 0 1-1.147-1.747L4.5 13.594V9.496c0-4.155 3.352-7.5 7.5-7.5zM13.5 18.5l-3 .002a1.5 1.5 0 0 0 2.993.145l.007-.147zM12 3.496c-3.32 0-6 2.674-6 6v4.41L4.656 17h14.697L18 13.907V9.509l-.003-.225A5.988 5.988 0 0 0 12 3.496z".to_string(),
31+
},
32+
Form {
33+
name: "Star".to_string(),
34+
path: "M10.788 3.102c.495-1.003 1.926-1.003 2.421 0l2.358 4.778l5.273.766c1.107.16 1.549 1.522.748 2.303l-3.816 3.719l.901 5.25c.19 1.104-.968 1.945-1.959 1.424l-4.716-2.48l-4.715 2.48c-.99.52-2.148-.32-1.96-1.423l.901-5.251l-3.815-3.72c-.801-.78-.359-2.141.748-2.302L8.43 7.88l2.358-4.778zm1.21.937L9.74 8.614a1.35 1.35 0 0 1-1.016.739l-5.05.734l3.654 3.562c.318.31.463.757.388 1.195l-.862 5.029l4.516-2.375a1.35 1.35 0 0 1 1.257 0l4.516 2.375l-.862-5.03a1.35 1.35 0 0 1 .388-1.194l3.654-3.562l-5.05-.734a1.35 1.35 0 0 1-1.016-.739L11.998 4.04z".to_string(),
35+
},
36+
];
3737

3838
view! {
3939
<svg style="font-family: sans-serif;">
@@ -44,7 +44,7 @@ fn main() {
4444
error_row_renderer=SvgErrorRowRenderer
4545
thead_row_renderer=GRenderer
4646
thead_renderer=GRenderer
47-
tbody_renderer=GRenderer
47+
tbody_renderer=SvgTbodyRenderer
4848
/>
4949
</svg>
5050
}

examples/custom_renderers_svg/src/renderers.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use leptos::html::AnyElement;
2+
use leptos::svg::G;
13
use leptos::*;
24
use leptos_struct_table::*;
35

@@ -10,6 +12,18 @@ wrapper_render_fn!(
1012
g,
1113
);
1214

15+
#[allow(non_snake_case)]
16+
pub fn SvgTbodyRenderer(
17+
content: Fragment,
18+
class: Signal<String>,
19+
node_ref: NodeRef<AnyElement>,
20+
) -> impl IntoView {
21+
let tbody_ref = create_node_ref::<G>();
22+
tbody_ref.on_load(move |e| node_ref.load(&e.into_any()));
23+
24+
view! { <g class=class node_ref=tbody_ref>{content}</g> }
25+
}
26+
1327
#[allow(unused_variables, non_snake_case)]
1428
pub fn SvgRowRenderer<Row>(
1529
class: Signal<String>,
@@ -19,10 +33,10 @@ pub fn SvgRowRenderer<Row>(
1933
on_select: EventHandler<web_sys::MouseEvent>,
2034
on_change: EventHandler<ChangeEvent<Row>>,
2135
) -> impl IntoView
22-
where
23-
Row: TableRow + Clone + 'static,
36+
where
37+
Row: TableRow + Clone + 'static,
2438
{
25-
let transform = format!("translate(0, {})", (index + 1) * ROW_HEIGHT);
39+
let transform = y_transform_from_index(index);
2640

2741
view! {
2842
<g
@@ -44,9 +58,13 @@ where
4458
}
4559
}
4660

61+
fn y_transform_from_index(index: usize) -> String {
62+
format!("translate(0, {})", (index + 1) * ROW_HEIGHT)
63+
}
64+
4765
#[allow(non_snake_case)]
4866
pub fn SvgErrorRowRenderer(err: String, index: usize, _col_count: usize) -> impl IntoView {
49-
let transform = transform_from_index(index, 0);
67+
let transform = y_transform_from_index(index);
5068

5169
view! {
5270
<g transform=transform>
@@ -65,7 +83,7 @@ pub fn SvgLoadingRowRenderer(
6583
index: usize,
6684
_col_count: usize,
6785
) -> impl IntoView {
68-
let transform = transform_from_index(index, 0);
86+
let transform = y_transform_from_index(index);
6987

7088
view! {
7189
<g class=class transform=transform>
@@ -96,8 +114,8 @@ pub fn SvgHeadCellRenderer<F>(
96114
on_click: F,
97115
children: Children,
98116
) -> impl IntoView
99-
where
100-
F: Fn(TableHeadEvent) + 'static,
117+
where
118+
F: Fn(TableHeadEvent) + 'static,
101119
{
102120
let style = move || {
103121
let sort = match sort_direction() {
@@ -142,9 +160,9 @@ pub fn SvgTextCellRenderer<T, F>(
142160
on_change: F,
143161
index: usize,
144162
) -> impl IntoView
145-
where
146-
T: IntoView + Clone + 'static,
147-
F: Fn(T) + 'static,
163+
where
164+
T: IntoView + Clone + 'static,
165+
F: Fn(T) + 'static,
148166
{
149167
let x = x_from_index(index);
150168

@@ -163,8 +181,8 @@ pub fn SvgPathCellRenderer<F>(
163181
on_change: F,
164182
index: usize,
165183
) -> impl IntoView
166-
where
167-
F: Fn(String) + 'static,
184+
where
185+
F: Fn(String) + 'static,
168186
{
169187
let transform = transform_from_index(index, 3);
170188

src/components/table_content.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ pub fn TableContent<Row, DataP, Err, ClsP>(
170170
loading_row_display_limit: Option<usize>,
171171
#[prop(optional)] _marker: PhantomData<Err>,
172172
) -> impl IntoView
173-
where
174-
Row: TableRow<ClassesProvider = ClsP> + Clone + 'static,
175-
DataP: TableDataProvider<Row, Err> + 'static,
176-
Err: Debug,
177-
ClsP: TableClassesProvider + Copy + 'static,
173+
where
174+
Row: TableRow<ClassesProvider=ClsP> + Clone + 'static,
175+
DataP: TableDataProvider<Row, Err> + 'static,
176+
Err: Debug,
177+
ClsP: TableClassesProvider + Copy + 'static,
178178
{
179179
let on_change = store_value(on_change);
180180
let rows = Rc::new(RefCell::new(rows));
@@ -299,11 +299,11 @@ where
299299
create_memo(move |_| {
300300
((height.get() / average_row_height.get()).ceil() as usize).max(20)
301301
})
302-
.into()
302+
.into()
303303
}
304304
};
305305

306-
let (display_range, set_display_range) = create_signal(0..5);
306+
let (display_range, set_display_range) = create_signal(0..0);
307307

308308
let placeholder_height_before =
309309
if matches!(display_strategy, DisplayStrategy::Pagination { .. }) {
@@ -325,7 +325,7 @@ where
325325

326326
row_count_after * average_row_height.get()
327327
})
328-
.into()
328+
.into()
329329
};
330330

331331
let tbody_ref = create_node_ref::<AnyElement>();
@@ -592,7 +592,7 @@ fn compute_average_row_height_from_loaded<Row, ClsP>(
592592
placeholder_height_before: Signal<f64>,
593593
loaded_rows: RwSignal<LoadedRows<Row>>,
594594
) where
595-
Row: TableRow<ClassesProvider = ClsP> + Clone + 'static,
595+
Row: TableRow<ClassesProvider=ClsP> + Clone + 'static,
596596
{
597597
if let Some(el) = tbody_ref.get_untracked() {
598598
let el: &web_sys::Element = &el;

src/components/tbody.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use leptos::html::{AnyElement, Tbody};
22
use leptos::*;
33

4-
/// Default tbody renderer. Please note that this is **NOT** a `#[component]` .
4+
/// Default tbody renderer. Please note that this is **NOT** a `#[component]`.
55
///
66
/// # Arguments
77
///
88
/// * `content` - The content of the renderer. It's like the children of this view.
99
/// * `class` - The class attribute that is passed to the root element
1010
/// * `node_ref` - The `NodeRef` referencing the root tbody element.
11-
#[doc = ""]
12-
#[doc = " This render function has to render exactly one root element."]
11+
///
12+
/// This render function has to render exactly one root element.
1313
#[allow(non_snake_case)]
1414
pub fn DefaultTableBodyRenderer(
1515
content: Fragment,

src/loaded_rows.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ pub struct LoadedRows<T: Clone> {
2727

2828
impl<T: Clone> LoadedRows<T> {
2929
pub fn new() -> Self {
30-
Self {
31-
rows: vec![RowState::Placeholder; 5],
32-
}
30+
Self { rows: vec![] }
3331
}
3432

3533
#[inline]

src/table_row.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ pub trait TableRow: Clone {
1818
///
1919
/// This render function has to render exactly one root element.
2020
fn render_row(&self, index: usize, on_change: EventHandler<ChangeEvent<Self>>)
21-
-> impl IntoView;
21+
-> impl IntoView;
2222

2323
/// Render the head row of the table.
2424
fn render_head_row<F>(
2525
sorting: Signal<VecDeque<(usize, ColumnSort)>>,
2626
on_head_click: F,
2727
) -> impl IntoView
28-
where
29-
F: Fn(TableHeadEvent) + Clone + 'static;
28+
where
29+
F: Fn(TableHeadEvent) + Clone + 'static;
3030

3131
/// The name of the column (= struct field name) at the given index. This can be used to implement
3232
/// sorting in a database. It takes the `#[table(skip)]` attributes into account. `col_index`
@@ -35,6 +35,7 @@ pub trait TableRow: Clone {
3535
/// For example:
3636
/// ```
3737
/// # use leptos_struct_table::*;
38+
/// # use leptos::*;
3839
/// #
3940
/// #[derive(TableRow, Clone)]
4041
/// struct Person {

0 commit comments

Comments
 (0)