Skip to content

Commit c68b0e3

Browse files
author
Diyor Khaydarov
committed
feat: drawers support in scaffolded tables
1 parent 6efa698 commit c68b0e3

File tree

6 files changed

+242
-32
lines changed

6 files changed

+242
-32
lines changed

components/scaffold/drawers.templ

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package scaffold
2+
3+
import (
4+
"fmt"
5+
icons "github.com/iota-uz/icons/phosphor"
6+
"github.com/iota-uz/iota-sdk/components/base/dialog"
7+
)
8+
9+
type DefaultDrawerProps struct {
10+
Title string
11+
CallbackURL string
12+
}
13+
14+
templ DefaultDrawer(props DefaultDrawerProps) {
15+
@dialog.Drawer(dialog.DrawerProps{
16+
Direction: dialog.RTL,
17+
Action: "open-default-drawer",
18+
Open: true,
19+
Classes: templ.Classes("flex items-stretch"),
20+
Attrs: templ.Attributes{
21+
"@closing": fmt.Sprintf("window.history.pushState({}, '', '%s')", props.CallbackURL),
22+
"@closed": "document.getElementById('view-drawer').innerHTML = ''",
23+
},
24+
}) {
25+
<div class="bg-white w-full md:w-[450px] ml-auto">
26+
<div class="flex flex-col h-full">
27+
<form method="dialog">
28+
<div class="flex justify-between px-4 py-3 border-b border-primary">
29+
<h3 class="text-lg font-medium">
30+
{ props.Title }
31+
</h3>
32+
<div>
33+
<button class="cursor-pointer">
34+
@icons.XCircle(icons.Props{Size: "20"})
35+
</button>
36+
</div>
37+
</div>
38+
</form>
39+
<div class="flex-1 min-h-0">
40+
{ children... }
41+
</div>
42+
</div>
43+
</div>
44+
}
45+
}

components/scaffold/drawers_templ.go

+107
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/scaffold/helpers.go

+42-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66

77
// --- Interfaces ---
88

9+
type RowOpt func(r *tableRowImpl)
10+
type ColumnOpt func(c *tableColumnImpl)
11+
912
type TableColumn interface {
1013
Key() string
1114
Label() string
@@ -15,6 +18,8 @@ type TableColumn interface {
1518

1619
type TableRow interface {
1720
Cells() []templ.Component
21+
Atrrs() templ.Attributes
22+
ApplyOpts(opts ...RowOpt) TableRow
1823
}
1924

2025
// --- Private Implementations ---
@@ -33,15 +38,36 @@ func (c *tableColumnImpl) Width() string { return c.width }
3338

3439
type tableRowImpl struct {
3540
cells []templ.Component
41+
attrs templ.Attributes
3642
}
3743

3844
func (r *tableRowImpl) Cells() []templ.Component {
3945
return r.cells
4046
}
4147

42-
// --- Column Option Type ---
48+
func (r *tableRowImpl) Atrrs() templ.Attributes {
49+
return r.attrs
50+
}
4351

44-
type ColumnOpt func(c *tableColumnImpl)
52+
func (r *tableRowImpl) ApplyOpts(opts ...RowOpt) TableRow {
53+
for _, opt := range opts {
54+
opt(r)
55+
}
56+
return r
57+
}
58+
59+
// --- Row Options ---
60+
61+
func WithDrawer(fetchURL string) RowOpt {
62+
return func(r *tableRowImpl) {
63+
r.attrs["class"] = r.attrs["class"].(string) + " cursor-pointer"
64+
r.attrs["hx-get"] = fetchURL
65+
r.attrs["hx-target"] = "#view-drawer"
66+
r.attrs["hx-swap"] = "innerHTML"
67+
}
68+
}
69+
70+
// --- Column Options ---
4571

4672
func WithClass(classes string) ColumnOpt {
4773
return func(c *tableColumnImpl) {
@@ -51,6 +77,8 @@ func WithClass(classes string) ColumnOpt {
5177

5278
// --- Table Configuration ---
5379

80+
type TableConfigOpt func(c *TableConfig)
81+
5482
type TableConfig struct {
5583
Title string
5684
DataURL string
@@ -60,14 +88,18 @@ type TableConfig struct {
6088
SideFilter templ.Component
6189
}
6290

63-
func NewTableConfig(title, dataURL string) *TableConfig {
64-
return &TableConfig{
91+
func NewTableConfig(title, dataURL string, opts ...TableConfigOpt) *TableConfig {
92+
t := &TableConfig{
6593
Title: title,
6694
DataURL: dataURL,
6795
Columns: []TableColumn{},
6896
Filters: []templ.Component{},
6997
Rows: []TableRow{},
7098
}
99+
for _, o := range opts {
100+
o(t)
101+
}
102+
return t
71103
}
72104

73105
func Column(key, label string, opts ...ColumnOpt) TableColumn {
@@ -82,7 +114,12 @@ func Column(key, label string, opts ...ColumnOpt) TableColumn {
82114
}
83115

84116
func Row(cells ...templ.Component) TableRow {
85-
return &tableRowImpl{cells: cells}
117+
return &tableRowImpl{
118+
cells: cells,
119+
attrs: templ.Attributes{
120+
"class": "hide-on-load",
121+
},
122+
}
86123
}
87124

88125
func (c *TableConfig) AddCols(cols ...TableColumn) *TableConfig {

components/scaffold/scaffold.templ

+12-8
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,36 @@ templ DateTime(ts time.Time) {
2828
}
2929

3030
// Rows renders the table rows for a scaffold table
31-
templ Rows(config *TableConfig) {
31+
templ Rows(cfg *TableConfig) {
3232
{{ pageCtx := composables.UsePageCtx(ctx) }}
3333
<tr class="hidden">
34-
<td colspan={ fmt.Sprintf("%d", len(config.Columns)) }>
34+
<td colspan={ fmt.Sprintf("%d", len(cfg.Columns)) }>
3535
@loaders.Spinner(loaders.SpinnerProps{
3636
ContainerClass: templ.Classes("flex justify-center items-center py-4"),
3737
})
3838
</td>
3939
</tr>
40-
if len(config.Rows) == 0 {
40+
if len(cfg.Rows) == 0 {
4141
@base.TableRow(base.TableRowProps{
42-
Attrs: templ.Attributes{"class": "hide-on-load"},
42+
Attrs: templ.Attributes{
43+
"class": "hide-on-load",
44+
},
4345
}) {
4446
@base.TableCell(base.TableCellProps{
4547
Classes: templ.Classes("text-center"),
4648
Attrs: templ.Attributes{
47-
"colspan": fmt.Sprintf("%d", len(config.Columns)),
49+
"colspan": fmt.Sprintf("%d", len(cfg.Columns)),
4850
},
4951
}) {
5052
{ pageCtx.T("Scaffold.Table.NothingFound") }
5153
}
5254
}
5355
} else {
54-
for _, item := range config.Rows {
56+
for _, row := range cfg.Rows {
5557
@base.TableRow(base.TableRowProps{
56-
Attrs: templ.Attributes{"class": "hide-on-load"},
58+
Attrs: row.Atrrs(),
5759
}) {
58-
for _, cell := range item.Cells() {
60+
for _, cell := range row.Cells() {
5961
@base.TableCell(base.TableCellProps{}) {
6062
@cell
6163
}
@@ -165,6 +167,8 @@ templ Content(config *TableConfig) {
165167
<div class="mt-5">
166168
@TableSection(config)
167169
</div>
170+
<!-- Placeholder for the view drawer -->
171+
<div id="view-drawer"></div>
168172
</div>
169173
}
170174

0 commit comments

Comments
 (0)