Skip to content

Commit 6ad770a

Browse files
committed
update
1 parent c8a401f commit 6ad770a

10 files changed

+112
-0
lines changed

config/element.go

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ type Element struct {
2121
Data map[string]interface{} `json:"data,omitempty"`
2222
}
2323

24+
func (e *Element) Cols() int {
25+
return GetCols(e.LabelCols, e.FieldCols)
26+
}
27+
2428
func (e *Element) Clone() *Element {
2529
elements := make([]*Element, len(e.Elements))
2630
languages := make([]*Language, len(e.Languages))

config/element_interface.go

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import "html/template"
66
type FormElement interface {
77
Render() template.HTML
88
Name() string
9+
Cols() int
910
OriginalName() string
1011
SetName(string)
1112
String() string

config/element_list_group.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package config
2+
3+
type Grouped struct {
4+
Elements []FormElement
5+
}
6+
7+
type Groups []Grouped
8+
9+
func SplitGroup(elements []FormElement) Groups {
10+
result := Groups{}
11+
t := 0
12+
g := Grouped{}
13+
for idx, ele := range elements {
14+
if idx == 0 {
15+
g.Elements = append(g.Elements, ele)
16+
t += ele.Cols()
17+
} else {
18+
cols := ele.Cols()
19+
if cols == 0 || t+cols > 12 {
20+
result = append(result, g)
21+
g = Grouped{}
22+
t = 0
23+
}
24+
g.Elements = append(g.Elements, ele)
25+
t += ele.Cols()
26+
}
27+
}
28+
if len(g.Elements) > 0 {
29+
result = append(result, g)
30+
}
31+
return result
32+
}

config/element_list_group_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package config_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/coscms/forms/config"
7+
"github.com/coscms/forms/fields"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/webx-top/com"
10+
)
11+
12+
func TestSplitGroup(t *testing.T) {
13+
r := config.SplitGroup([]config.FormElement{
14+
&fields.Field{
15+
OrigName: `1`,
16+
LabelCols: 0,
17+
FieldCols: 4,
18+
},
19+
&fields.Field{
20+
OrigName: `2`,
21+
LabelCols: 0,
22+
FieldCols: 4,
23+
},
24+
&fields.Field{
25+
OrigName: `3`,
26+
LabelCols: 0,
27+
FieldCols: 8,
28+
},
29+
&fields.Field{
30+
OrigName: `4`,
31+
LabelCols: 0,
32+
FieldCols: 4,
33+
},
34+
&fields.Field{
35+
OrigName: `5`,
36+
LabelCols: 0,
37+
FieldCols: 4,
38+
},
39+
})
40+
assert.Equal(t, 3, len(r))
41+
com.Dump(r)
42+
}

config/utils.go

+18
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,21 @@ func getValue(elements []*Element, languages []*Language, fieldValue func(string
103103
}
104104
return
105105
}
106+
107+
func GetCols(labelCols int, fieldCols int) int {
108+
return GetLabelCols(labelCols) + GetFieldCols(fieldCols)
109+
}
110+
111+
func GetLabelCols(labelCols int) int {
112+
if labelCols == 0 {
113+
labelCols = 2
114+
}
115+
return labelCols
116+
}
117+
118+
func GetFieldCols(fieldCols int) int {
119+
if fieldCols == 0 {
120+
fieldCols = 8
121+
}
122+
return fieldCols
123+
}

fields/field.go

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ func FieldWithType(name, t string) *Field {
8585
}
8686
}
8787

88+
func (f *Field) Cols() int {
89+
return config.GetCols(f.LabelCols, f.FieldCols)
90+
}
91+
8892
func (f *Field) SetTemplate(tmpl string, theme ...string) FieldInterface {
8993
f.Template = tmpl
9094
if len(f.Template) > 0 && f.widget != nil && f.Template != tmpl {

fields/field_interface.go

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
type FieldInterface interface {
1212
Name() string
1313
OriginalName() string
14+
Cols() int
1415
SetName(string)
1516
SetLabelCols(cols int)
1617
SetFieldCols(cols int)

fieldset.go

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ type FieldSetType struct {
4848
data map[string]interface{}
4949
}
5050

51+
func (f *FieldSetType) Cols() int {
52+
return config.GetCols(f.LabelCols, f.FieldCols)
53+
}
54+
5155
func (f *FieldSetType) SetData(key string, value interface{}) {
5256
f.AppendData[key] = value
5357
}
@@ -79,6 +83,7 @@ func (f *FieldSetType) Data() map[string]interface{} {
7983
"labelCols": f.LabelCols,
8084
"fieldCols": f.FieldCols,
8185
"fields": f.FieldList,
86+
"groups": config.SplitGroup(f.FieldList),
8287
"classes": f.Classes,
8388
"tags": f.Tags,
8489
}

forms.go

+1
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ func (f *Form) Data() map[string]interface{} {
610610
f.data = map[string]interface{}{
611611
"container": "",
612612
"fields": f.FieldList,
613+
"groups": config.SplitGroup(f.FieldList),
613614
"classes": f.Class,
614615
"id": f.ID,
615616
"params": safeParams,

langset.go

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ type LangSetType struct {
4545
data map[string]interface{}
4646
}
4747

48+
func (f *LangSetType) Cols() int {
49+
return 0
50+
}
51+
4852
func (f *LangSetType) SetName(name string) {
4953
f.CurrName = name
5054
}

0 commit comments

Comments
 (0)