Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #101 #102

Merged
merged 2 commits into from
Jul 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions form/bootstrap/form_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,15 @@ func Test_Field_TagOnly(t *testing.T) {
output: `<input class="" id="-Name" name="Name" type="checkbox" value="true" />`,
},

{
f: f.SelectTag,
name: "Name",
opts: tags.Options{
"tag_only": true,
},
output: `<select class="" id="-Name" name="Name"></select>`,
},

{
f: f.FileTag,
name: "Name",
Expand Down
2 changes: 1 addition & 1 deletion form/form_for.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (f FormFor) RadioButtonTag(field string, opts tags.Options) *tags.Tag {
}

//SelectTag creates a select tag for a specified struct field and loads options from the options opject
func (f FormFor) SelectTag(field string, opts tags.Options) *SelectTag {
func (f FormFor) SelectTag(field string, opts tags.Options) *tags.Tag {
f.buildOptions(field, opts)
return f.Form.SelectTag(opts)
}
Expand Down
22 changes: 13 additions & 9 deletions form/select_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ type SelectTag struct {
}

func (s SelectTag) String() string {
for _, x := range s.SelectOptions {
if _, ok := s.selectedValueCache[template.HTMLEscaper(x.Value)]; ok {
x.Selected = true
}
s.Append(x.String())
}
return s.Tag.String()
}

Expand All @@ -31,7 +25,7 @@ func (s SelectTag) HTML() template.HTML {
}

// NewSelectTag constructs a new `<select>` tag.
func NewSelectTag(opts tags.Options) *SelectTag {
func NewSelectTag(opts tags.Options) *tags.Tag {
so := parseSelectOptions(opts)
selected := opts["value"]
delete(opts, "value")
Expand Down Expand Up @@ -70,17 +64,27 @@ func NewSelectTag(opts tags.Options) *SelectTag {
selectedMap[template.HTMLEscaper(selected)] = struct{}{}
}

delete(opts, "tag_only")

st := &SelectTag{
Tag: tags.New("select", opts),
SelectOptions: so,
SelectedValue: selected,
selectedValueCache: selectedMap,
}
return st

for _, x := range st.SelectOptions {
if _, ok := st.selectedValueCache[template.HTMLEscaper(x.Value)]; ok {
x.Selected = true
}
st.Append(x.String())
}

return st.Tag
}

// SelectTag constructs a new `<select>` tag from a form.
func (f Form) SelectTag(opts tags.Options) *SelectTag {
func (f Form) SelectTag(opts tags.Options) *tags.Tag {
return NewSelectTag(opts)
}

Expand Down
15 changes: 15 additions & 0 deletions form/select_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ func Test_SelectTagWithOptions(t *testing.T) {
r.Equal(`<select><option value="1">1/2 day</option><option value="2">1-2 days</option><option value="7">1 week</option><option value="14">1-2 weeks</option></select>`, s.String())
}

func Test_SelectTagWithOptionsSelected(t *testing.T) {
r := require.New(t)
f := form.New(tags.Options{})
s := f.SelectTag(tags.Options{
"options": []map[string]interface{}{
{"1/2 day": 1},
{"1-2 days": 2},
{"1 week": 7},
{"1-2 weeks": 14},
},
"value": 1,
})
r.Equal(`<select><option value="1" selected>1/2 day</option><option value="2">1-2 days</option><option value="7">1 week</option><option value="14">1-2 weeks</option></select>`, s.String())
}

func Test_SelectTag_WithSelectOptions(t *testing.T) {
r := require.New(t)
f := form.New(tags.Options{})
Expand Down