Skip to content

Commit 0d29900

Browse files
committed
improve colunm, grid and hbox
1 parent 6a74d61 commit 0d29900

File tree

6 files changed

+88
-67
lines changed

6 files changed

+88
-67
lines changed

comp.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func (a *App) ChartAxis() comp.ChartAxis { return icomp.NewChartAxis()
232232
func (a *App) NewListItem() comp.ListItem { return icomp.NewListItem() }
233233
func (a *App) Api() comp.Api { return icomp.NewApi() }
234234
func (a *App) BreadcrumbItem() comp.BreadcrumbItem { return icomp.NewBreadcrumbItem() }
235-
func (a *App) Column() comp.Column { return icomp.NewColumn() }
235+
func (a *App) Column(component ...any) comp.Column { return icomp.NewColumn(component...) }
236236
func (a *App) Event() comp.Event { return icomp.NewEvent() }
237237
func (a *App) EventActions(actions ...comp.EventAction) icomp.EventActions {
238238
return icomp.NewEventActions(actions...)
@@ -246,16 +246,16 @@ func (a *App) EventActionDrawer(drawer ...comp.Drawer) icomp.EventAction {
246246
func (a *App) EventActionDialog(dialog ...comp.Dialog) icomp.EventAction {
247247
return icomp.NewEventActionDialog(dialog...)
248248
}
249-
func (a *App) EventActionArgs() comp.EventActionArgs { return icomp.NewEventActionArgs() }
250-
func (a *App) GridItem() comp.GridItem { return icomp.NewGridItem() }
251-
func (a *App) Horizontal() comp.Horizontal { return icomp.NewHorizontal() }
252-
func (a *App) ListBodyField() comp.ListBodyField { return icomp.NewListBodyField() }
253-
func (a *App) NavLink() comp.NavLink { return icomp.NewNavLink() }
254-
func (a *App) Option() comp.Option { return icomp.NewOption() }
255-
func (a *App) Options() comp.Options { return icomp.NewOptions() }
256-
func (a *App) PageItem() comp.PageItem { return icomp.NewPageItem() }
257-
func (a *App) PropertyItem() comp.PropertyItem { return icomp.NewPropertyItem() }
258-
func (a *App) PullRefresh() comp.PullRefresh { return icomp.NewPullRefresh() }
259-
func (a *App) Rule() comp.Rule { return icomp.NewRule() }
260-
func (a *App) Tab() comp.Tab { return icomp.NewTab() }
261-
func (a *App) CarouselOption() comp.CarouselOption { return icomp.NewCarouselOption() }
249+
func (a *App) EventActionArgs() comp.EventActionArgs { return icomp.NewEventActionArgs() }
250+
func (a *App) GridItem(component ...any) comp.GridItem { return icomp.NewGridItem(component...) }
251+
func (a *App) Horizontal() comp.Horizontal { return icomp.NewHorizontal() }
252+
func (a *App) ListBodyField() comp.ListBodyField { return icomp.NewListBodyField() }
253+
func (a *App) NavLink() comp.NavLink { return icomp.NewNavLink() }
254+
func (a *App) Option() comp.Option { return icomp.NewOption() }
255+
func (a *App) Options() comp.Options { return icomp.NewOptions() }
256+
func (a *App) PageItem() comp.PageItem { return icomp.NewPageItem() }
257+
func (a *App) PropertyItem() comp.PropertyItem { return icomp.NewPropertyItem() }
258+
func (a *App) PullRefresh() comp.PullRefresh { return icomp.NewPullRefresh() }
259+
func (a *App) Rule() comp.Rule { return icomp.NewRule() }
260+
func (a *App) Tab() comp.Tab { return icomp.NewTab() }
261+
func (a *App) CarouselOption() comp.CarouselOption { return icomp.NewCarouselOption() }

internal/comp/column.go

+39-40
Original file line numberDiff line numberDiff line change
@@ -4,167 +4,166 @@ import "github.com/zrcoder/amisgo/schema"
44

55
type Column schema.Schema
66

7-
func NewColumn() Column {
8-
return make(Column)
7+
func NewColumn(component ...any) Column {
8+
if len(component) == 0 {
9+
return Column{}
10+
}
11+
s := toSchema(component[0])
12+
return Column(s)
913
}
1014

11-
// Set sets a key-value pair
12-
func (c Column) Set(key string, value any) Column {
15+
// set sets a key-value pair
16+
func (c Column) set(key string, value any) Column {
1317
c[key] = value
1418
return c
1519
}
1620

1721
// Align sets the alignment
1822
func (c Column) Align(value string) Column {
19-
return c.Set("align", value)
23+
return c.set("align", value)
2024
}
2125

2226
// Body sets the body content
2327
func (c Column) Body(value ...any) Column {
24-
return c.Set("body", value)
28+
return c.set("body", value)
2529
}
2630

2731
// CanAccessSuperData sets whether the cell can access parent data
2832
func (c Column) CanAccessSuperData(value bool) Column {
29-
return c.Set("canAccessSuperData", value)
33+
return c.set("canAccessSuperData", value)
3034
}
3135

3236
// Children sets the header group
3337
func (c Column) Children(value string) Column {
34-
return c.Set("children", value)
38+
return c.set("children", value)
3539
}
3640

3741
// ClassName sets the column class name
3842
func (c Column) ClassName(value string) Column {
39-
return c.Set("className", value)
43+
return c.set("className", value)
4044
}
4145

4246
// ClassNameExpr sets the cell class name expression
4347
func (c Column) ClassNameExpr(value string) Column {
44-
return c.Set("classNameExpr", value)
48+
return c.set("classNameExpr", value)
4549
}
4650

4751
// ColSpanExpr sets the column span expression
4852
func (c Column) ColSpanExpr(value string) Column {
49-
return c.Set("colSpanExpr", value)
53+
return c.set("colSpanExpr", value)
5054
}
5155

5256
// Copyable sets whether the column is copyable
5357
func (c Column) Copyable(value bool) Column {
54-
return c.Set("copyable", value)
58+
return c.set("copyable", value)
5559
}
5660

5761
// Filterable sets whether the column is filterable
5862
func (c Column) Filterable(value bool) Column {
59-
return c.Set("filterable", value)
63+
return c.set("filterable", value)
6064
}
6165

6266
// Fixed sets whether the column is fixed
6367
func (c Column) Fixed(value string) Column {
64-
return c.Set("fixed", value)
65-
}
66-
67-
// Type sets the column type
68-
func (c Column) Type(value string) Column {
69-
return c.Set("type", value)
68+
return c.set("fixed", value)
7069
}
7170

7271
// Name sets the column unique identifier
7372
func (c Column) Name(value string) Column {
74-
return c.Set("name", value)
73+
return c.set("name", value)
7574
}
7675

7776
// Label sets the column label
7877
func (c Column) Label(value string) Column {
79-
return c.Set("label", value)
78+
return c.set("label", value)
8079
}
8180

8281
// QuickEdit sets the quick edit configuration
8382
func (c Column) QuickEdit(value string) Column {
84-
return c.Set("quickEdit", value)
83+
return c.set("quickEdit", value)
8584
}
8685

8786
// Remark sets the header remark
8887
func (c Column) Remark(value string) Column {
89-
return c.Set("remark", value)
88+
return c.set("remark", value)
9089
}
9190

9291
// RowSpanExpr sets the row span expression
9392
func (c Column) RowSpanExpr(value string) Column {
94-
return c.Set("rowSpanExpr", value)
93+
return c.set("rowSpanExpr", value)
9594
}
9695

9796
// Searchable sets whether the column is searchable
9897
func (c Column) Searchable(value any) Column {
99-
return c.Set("searchable", value)
98+
return c.set("searchable", value)
10099
}
101100

102101
// Sortable sets whether the column is sortable
103102
func (c Column) Sortable(value bool) Column {
104-
return c.Set("sortable", value)
103+
return c.set("sortable", value)
105104
}
106105

107106
// Sorter sets the sorter
108107
func (c Column) Sorter(value bool) Column {
109-
return c.Set("sorter", value)
108+
return c.set("sorter", value)
110109
}
111110

112111
// Title sets the column title
113112
func (c Column) Title(value any) Column {
114-
return c.Set("title", value)
113+
return c.set("title", value)
115114
}
116115

117116
// TitleClassName sets the header cell class name
118117
func (c Column) TitleClassName(value string) Column {
119-
return c.Set("titleClassName", value)
118+
return c.set("titleClassName", value)
120119
}
121120

122121
// Togged sets whether the column is toggled
123122
func (c Column) Togged(value bool) Column {
124-
return c.Set("toggled", value)
123+
return c.set("toggled", value)
125124
}
126125

127126
// Width sets the column width
128127
func (c Column) Width(value string) Column {
129-
return c.Set("width", value)
128+
return c.set("width", value)
130129
}
131130

132131
// ColumnClassName sets the column class name
133132
func (c Column) ColumnClassName(value string) Column {
134-
return c.Set("columnClassName", value)
133+
return c.set("columnClassName", value)
135134
}
136135

137136
// Xs sets the width ratio for extra small screens
138137
func (c Column) Xs(value any) Column {
139-
return c.Set("xs", value)
138+
return c.set("xs", value)
140139
}
141140

142141
// Sm sets the width ratio for small screens
143142
func (c Column) Sm(value any) Column {
144-
return c.Set("sm", value)
143+
return c.set("sm", value)
145144
}
146145

147146
// Md sets the width ratio for medium screens
148147
func (c Column) Md(value any) Column {
149-
return c.Set("md", value)
148+
return c.set("md", value)
150149
}
151150

152151
// Lg sets the width ratio for large screens
153152
func (c Column) Lg(value any) Column {
154-
return c.Set("lg", value)
153+
return c.set("lg", value)
155154
}
156155

157-
// Valign sets the vertical alignment
156+
// Valign sets the vertical alignment, 'top' | 'middle' | 'bottom' | 'between'
158157
func (c Column) Valign(value string) Column {
159-
return c.Set("valign", value)
158+
return c.set("valign", value)
160159
}
161160

162161
// Buttons sets the buttons
163162
func (c Column) Buttons(value ...any) Column {
164-
return c.Set("buttons", value)
163+
return c.set("buttons", value)
165164
}
166165

167166
// PopOver sets the pop-over
168167
func (c Column) PopOver(value any) Column {
169-
return c.Set("popOver", value)
168+
return c.set("popOver", value)
170169
}

internal/comp/grid2d.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (g Grid2d) ClassName(value string) Grid2d {
2020
}
2121

2222
// Cols sets the number of columns, default is 12.
23-
func (g Grid2d) Cols(value string) Grid2d {
23+
func (g Grid2d) Cols(value int) Grid2d {
2424
return g.set("cols", value)
2525
}
2626

@@ -39,14 +39,14 @@ func (g Grid2d) EditorSetting(value string) Grid2d {
3939
return g.set("editorSetting", value)
4040
}
4141

42-
// Gap sets the gap between grid items, default is 0.
43-
func (g Grid2d) Gap(value string) Grid2d {
42+
// Gap sets the gap between grid items, string | int, default is 0.
43+
func (g Grid2d) Gap(value any) Grid2d {
4444
return g.set("gap", value)
4545
}
4646

47-
// RowGap sets the vertical gap between grid items.
48-
func (g Grid2d) RowGap(value string) Grid2d {
49-
return g.set("rowGap", value)
47+
// GapRow sets the vertical gap between grid items, string | int.
48+
func (g Grid2d) GapRow(value any) Grid2d {
49+
return g.set("gapRow", value)
5050
}
5151

5252
// Grids sets the configuration for each grid item.

internal/comp/grid_model.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package comp
22

3-
import "github.com/zrcoder/amisgo/schema"
3+
import (
4+
"github.com/zrcoder/amisgo/schema"
5+
)
46

57
type GridItem schema.Schema
68

7-
func NewGridItem() GridItem {
8-
return GridItem{}
9+
func NewGridItem(component ...any) GridItem {
10+
if len(component) == 0 {
11+
return GridItem{}
12+
}
13+
s := toSchema(component[0])
14+
return GridItem(s)
915
}
1016

1117
func (g GridItem) set(key string, value any) GridItem {
@@ -52,3 +58,8 @@ func (g GridItem) Align(value string) GridItem {
5258
func (g GridItem) Valign(value string) GridItem {
5359
return g.set("valign", value)
5460
}
61+
62+
// GridClassName sets the classname
63+
func (g GridItem) GridClassName(value string) GridItem {
64+
return g.set("gridClassName", value)
65+
}

internal/comp/hbox.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func (h HBox) set(key string, value any) HBox {
1414
return h
1515
}
1616

17-
// Align sets the horizontal alignment: left, right, between, center
17+
// Align sets the horizontal alignment: 'left' | 'right' | 'between' | 'center'
1818
func (h HBox) Align(value string) HBox {
1919
return h.set("align", value)
2020
}
@@ -25,7 +25,7 @@ func (h HBox) ClassName(value string) HBox {
2525
}
2626

2727
// Columns sets the columns
28-
func (h HBox) Columns(value ...Column) HBox {
28+
func (h HBox) Columns(value ...any) HBox {
2929
return h.set("columns", value)
3030
}
3131

@@ -44,7 +44,7 @@ func (h HBox) EditorSetting(value string) HBox {
4444
return h.set("editorSetting", value)
4545
}
4646

47-
// Gap sets the horizontal gap: xs, sm, base, none, md, lg
47+
// Gap sets the horizontal gap: 'xs' | 'sm' | 'base' | 'none' | 'md' | 'lg'
4848
func (h HBox) Gap(value string) HBox {
4949
return h.set("gap", value)
5050
}
@@ -134,7 +134,7 @@ func (h HBox) UseMobileUI(value bool) HBox {
134134
return h.set("useMobileUI", value)
135135
}
136136

137-
// Valign sets the vertical alignment: top, middle, bottom, between
137+
// Valign sets the vertical alignment: 'top' | 'middle' | 'bottom' | 'between'
138138
func (h HBox) Valign(value string) HBox {
139139
return h.set("valign", value)
140140
}

internal/comp/util.go

+11
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package comp
22

33
import (
44
"encoding/json"
5+
"reflect"
56

67
"github.com/zrcoder/amisgo/internal/servemux"
8+
"github.com/zrcoder/amisgo/schema"
79
)
810

911
func pureAmis(key string, value any, m map[string]any) {
@@ -16,3 +18,12 @@ func pureAmis(key string, value any, m map[string]any) {
1618
func marshalMap(m map[string]any) ([]byte, error) {
1719
return json.Marshal(m)
1820
}
21+
22+
func toSchema(value any) schema.Schema {
23+
v := reflect.ValueOf(value)
24+
if v.Kind() == reflect.Map && v.Type().ConvertibleTo(reflect.TypeOf(schema.Schema{})) {
25+
converted := v.Convert(reflect.TypeOf(schema.Schema{}))
26+
return converted.Interface().(schema.Schema)
27+
}
28+
return schema.Schema{}
29+
}

0 commit comments

Comments
 (0)