This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
password_modal.go
109 lines (98 loc) · 2.57 KB
/
password_modal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package components
import (
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
// PasswordModal is a modal which allows to user to enter a password
type PasswordModal struct {
app.Compo
Title string // Title of the modal
WrongPassword bool // Whether the previously entered password was wrong
ClearWrongPassword func() // Handler to call when clearing the password
OnSubmit func(password string) // Handler to call to submit the password
OnCancel func() // Handler to call when closing/cancelling the modal
password string
}
func (c *PasswordModal) Render() app.UI {
return &Modal{
ID: "password-modal",
Title: c.Title,
DisableFocus: true,
Body: []app.UI{
app.Form().
Class("pf-c-form").
ID("password-modal-form").
OnSubmit(func(ctx app.Context, e app.Event) {
e.PreventDefault()
// Submit the form
c.OnSubmit(
c.password,
)
c.clear()
}).
Body(
app.Div().
Class("pf-c-form__group").
Body(
app.Div().
Class("pf-c-form__group-control").
Body(
&Autofocused{
Component: app.Input().
Class("pf-c-form-control").
Required(true).
Type("password").
Placeholder("Password").
Aria("label", "Password").
Aria("invalid", c.WrongPassword).
Aria("describedby", func() string {
if c.WrongPassword {
return "password-helper"
}
return ""
}).
OnInput(func(ctx app.Context, e app.Event) {
c.password = ctx.JSSrc().Get("value").String()
if c.ClearWrongPassword != nil {
c.ClearWrongPassword()
}
}).
Value(c.password),
},
app.If(
c.WrongPassword,
app.P().
Class("pf-c-form__helper-text pf-m-error").
ID("password-helper").
Aria("live", "polite").
Body(
app.Text("The password is incorrect. Please try again."),
),
),
),
),
),
},
Footer: []app.UI{
app.Button().
Class("pf-c-button pf-m-primary").
Type("submit").
Form("password-modal-form").
Text("Continue"),
app.Button().
Class("pf-c-button pf-m-link").
Type("button").
Text("Cancel").
OnClick(func(ctx app.Context, e app.Event) {
c.clear()
c.OnCancel()
}),
},
OnClose: func() {
c.clear()
c.OnCancel()
},
}
}
func (c *PasswordModal) clear() {
c.password = ""
}