Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
hdm authored Sep 18, 2019
2 parents 8cd423b + 0f314a0 commit 071732b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
7 changes: 6 additions & 1 deletion form/select_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ func parseSelectOptions(opts tags.Options) SelectOptions {
x := rv.Index(i).Interface()

if rv.Index(i).Type().Implements(selectableType) {
so = append(so, SelectOption{Value: x.(Selectable).SelectValue(), Label: x.(Selectable).SelectLabel()})
isSelected := false
selectableMultipleType := reflect.TypeOf((*SelectableMultiple)(nil)).Elem()
if rv.Index(i).Type().Implements(selectableMultipleType) {
isSelected = x.(SelectableMultiple).IsSelected()
}
so = append(so, SelectOption{Value: x.(Selectable).SelectValue(), Label: x.(Selectable).SelectLabel(), Selected: isSelected})
continue
}

Expand Down
39 changes: 39 additions & 0 deletions form/select_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ func Test_SelectTag_Multiple_SelectMultiple_Selectable_Interface(t *testing.T) {
r.Contains(s, `<option value="3">Mark</option>`)
}


func Test_SelectTagWithHTML(t *testing.T) {
r := require.New(t)
f := form.New(tags.Options{})
Expand All @@ -360,6 +361,24 @@ func Test_SelectTagWithHTML(t *testing.T) {
r.Equal(`<select><option value="&lt;u&gt;Not Underlined&lt;/u&gt;">&lt;b&gt;Not Bold&lt;/b&gt;</option></select>`, s.String())
}

func Test_SelectTag_Multiple_SelectMultiple_SelectableMultiple_Interface(t *testing.T) {
r := require.New(t)
f := form.New(tags.Options{})
st := f.SelectTag(tags.Options{
"multiple": true,
"options": []SelectableMultipleModel{
{"John", "1"},
{"Peter", "2"},
{"Mark", "3"},
},
})
s := st.String()
r.Contains(s, `<select multiple>`)
r.Contains(s, `<option value="1">John</option>`)
r.Contains(s, `<option value="2">Peter</option>`)
r.Contains(s, `<option value="3" selected>Mark</option>`)
}

type SelectableModel struct {
Name string
ID string
Expand All @@ -373,6 +392,26 @@ func (sm SelectableModel) SelectValue() interface{} {
return sm.ID
}

type SelectableMultipleModel struct {
Name string
ID string
}

func (sm SelectableMultipleModel) SelectLabel() string {
return sm.Name
}

func (sm SelectableMultipleModel) SelectValue() interface{} {
return sm.ID
}

func (sm SelectableMultipleModel) IsSelected() bool {
if sm.Name == "Mark" {
return true
}
return false
}

type SelectableUUIDModel struct {
Name string
ID uuid.UUID
Expand Down
9 changes: 9 additions & 0 deletions form/selectable_multiple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package form

//SelectableMultiple allows any struct to add Selected option in the select tag.
type SelectableMultiple interface {
IsSelected() bool
}

//SelectableMultiples is the plural for SelectableMultiple
type SelectableMultiples []SelectableMultiple

0 comments on commit 071732b

Please sign in to comment.