Skip to content

Commit

Permalink
fix input type month
Browse files Browse the repository at this point in the history
  • Loading branch information
madflow committed Aug 21, 2023
1 parent e806ead commit 98393f4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
23 changes: 21 additions & 2 deletions element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"image/color"
"image/png"
"os"
"path/filepath"
"testing"
"time"

"github.com/ysmood/gson"

"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/cdp"
"github.com/go-rod/rod/lib/devices"
"github.com/go-rod/rod/lib/input"
"github.com/go-rod/rod/lib/proto"
"github.com/go-rod/rod/lib/utils"
"github.com/ysmood/gson"
)

func TestGetElementPage(t *testing.T) {
Expand Down Expand Up @@ -304,7 +306,8 @@ func TestShadowDOM(t *testing.T) {
func TestInputTime(t *testing.T) {
g := setup(t)

now := time.Now()
now := time.Date(2006, 1, 2, 3, 4, 5, 0, time.Local)
fmt.Println(now)

p := g.page.MustNavigate(g.srcFile("fixtures/input.html"))

Expand All @@ -325,6 +328,22 @@ func TestInputTime(t *testing.T) {
g.True(p.MustHas("[event=input-datetime-local-change]"))
}

{
el = p.MustElement("[type=time]")
el.MustInputTime(now)

g.Eq(el.MustText(), fmt.Sprintf("%02d:%02d", now.Hour(), now.Minute()))
g.True(p.MustHas("[event=input-time-change]"))
}

{
el = p.MustElement("[type=month]")
el.MustInputTime(now)

g.Eq(el.MustText(), now.Format("2006-01"))
g.True(p.MustHas("[event=input-month-change]"))
}

g.Panic(func() {
g.mc.stubErr(1, proto.RuntimeCallFunctionOn{})
el.MustInputTime(now)
Expand Down
14 changes: 14 additions & 0 deletions fixtures/input.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@

<hr />

<input
type="time"
onchange="this.setAttribute('event', 'input-time-change')"
/>

<hr />

<input
type="month"
onchange="this.setAttribute('event', 'input-month-change')"
/>

<hr />

<select multiple>
<option value="a">A</option>
<option value="b">B</option>
Expand Down
2 changes: 1 addition & 1 deletion lib/js/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ var InputEvent = &Function{
// InputTime ...
var InputTime = &Function{
Name: "inputTime",
Definition: `function(e){var e=new Date(e),t=e=>e.toString().padStart(2,"0"),n=e.getFullYear(),r=t(e.getMonth()+1),i=t(e.getDate()),o=t(e.getHours()),s=t(e.getMinutes());switch(this.type){case"date":this.value=n+` + "`" + `-${r}-` + "`" + `+i;break;case"datetime-local":this.value=n+` + "`" + `-${r}-${i}T${o}:` + "`" + `+s;break;case"month":this.value=r;break;case"time":this.value=o+":"+s}functions.inputEvent.call(this)}`,
Definition: `function(e){var e=new Date(e),t=e=>e.toString().padStart(2,"0"),n=e.getFullYear(),r=t(e.getMonth()+1),i=t(e.getDate()),o=t(e.getHours()),s=t(e.getMinutes());switch(this.type){case"date":this.value=n+` + "`" + `-${r}-` + "`" + `+i;break;case"datetime-local":this.value=n+` + "`" + `-${r}-${i}T${o}:` + "`" + `+s;break;case"month":this.value=n+"-"+r;break;case"time":this.value=o+":"+s}functions.inputEvent.call(this)}`,
Dependencies: []*Function{InputEvent},
}

Expand Down
2 changes: 1 addition & 1 deletion lib/js/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ const functions = {
this.value = `${y}-${mon}-${d}T${h}:${min}`
break
case 'month':
this.value = mon
this.value = `${y}-${mon}`
break
case 'time':
this.value = `${h}:${min}`
Expand Down

0 comments on commit 98393f4

Please sign in to comment.