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 20, 2023
1 parent ed86763 commit ffe6d9d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
20 changes: 19 additions & 1 deletion 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 @@ -325,6 +327,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("%d:%d", 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 ffe6d9d

Please sign in to comment.