This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1305 from GuessWhoSamFoo/modal-component
Add modal component
- Loading branch information
Showing
34 changed files
with
839 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added modal component and opening modals through buttons |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package component | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
type ModalSize string | ||
|
||
const ( | ||
// ModalSizeSmall is the smallest modal | ||
ModalSizeSmall ModalSize = "sm" | ||
// ModalSizeLarge is a large modal | ||
ModalSizeLarge ModalSize = "lg" | ||
// ModalSizeExtraLarge is the largest modal | ||
ModalSizeExtraLarge ModalSize = "xl" | ||
) | ||
|
||
// ModalConfig is a configuration for the modal component. | ||
type ModalConfig struct { | ||
Body Component `json:"body,omitempty"` | ||
Form *Form `json:"form,omitempty"` | ||
Opened bool `json:"opened"` | ||
ModalSize ModalSize `json:"size,omitempty"` | ||
Buttons []Button `json:"buttons,omitempty"` | ||
} | ||
|
||
// UnmarshalJSON unmarshals a modal config from JSON. | ||
func (m *ModalConfig) UnmarshalJSON(data []byte) error { | ||
x := struct { | ||
Body *TypedObject `json:"body,omitempty"` | ||
Form *Form `json:"form,omitempty"` | ||
Opened bool `json:"opened"` | ||
ModalSize ModalSize `json:"size,omitempty"` | ||
Buttons []Button `json:"buttons,omitempty"` | ||
}{} | ||
|
||
if err := json.Unmarshal(data, &x); err != nil { | ||
return err | ||
} | ||
|
||
if x.Body != nil { | ||
var err error | ||
m.Body, err = x.Body.ToComponent() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
m.Form = x.Form | ||
m.Opened = x.Opened | ||
m.ModalSize = x.ModalSize | ||
m.Buttons = x.Buttons | ||
return nil | ||
} | ||
|
||
// Modal is a modal component. | ||
// | ||
// +octant:component | ||
type Modal struct { | ||
Base | ||
Config ModalConfig `json:"config"` | ||
} | ||
|
||
// NewModal creates a new modal. | ||
func NewModal(title []TitleComponent) *Modal { | ||
return &Modal{ | ||
Base: newBase(TypeModal, title), | ||
} | ||
} | ||
|
||
var _ Component = (*Modal)(nil) | ||
|
||
// SetBody sets the body of a modal. | ||
func (m *Modal) SetBody(body Component) { | ||
m.Config.Body = body | ||
} | ||
|
||
// AddForm adds a form to a modal. It is added after the body. | ||
func (m *Modal) AddForm(form Form) { | ||
m.Config.Form = &form | ||
} | ||
|
||
// SetSize sets the size of a modal. Size is medium by default. | ||
func (m *Modal) SetSize(size ModalSize) { | ||
m.Config.ModalSize = size | ||
} | ||
|
||
// AddButton is a helper to add a custom button | ||
func (m *Modal) AddButton(button Button) { | ||
m.Config.Buttons = append(m.Config.Buttons, button) | ||
} | ||
|
||
// Open opens a modal. A modal is closed by default. | ||
func (m *Modal) Open() { | ||
m.Config.Opened = true | ||
} | ||
|
||
// Close closes a modal. | ||
func (m *Modal) Close() { | ||
m.Config.Opened = false | ||
} | ||
|
||
type modalMarshal Modal | ||
|
||
// MarshalJSON marshal a modal to JSON. | ||
func (m *Modal) MarshalJSON() ([]byte, error) { | ||
k := modalMarshal(*m) | ||
k.Metadata.Type = TypeModal | ||
return json.Marshal(&k) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package component | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestModal_SetBody(t *testing.T) { | ||
modal := NewModal(TitleFromString("modal")) | ||
body := NewText("body") | ||
modal.SetBody(body) | ||
|
||
expected := NewModal(TitleFromString("modal")) | ||
expected.Config.Body = body | ||
|
||
AssertEqual(t, expected, modal) | ||
} | ||
|
||
func TestModal_SetSize(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
size ModalSize | ||
expected *Modal | ||
}{ | ||
{ | ||
name: "small", | ||
size: ModalSizeSmall, | ||
expected: &Modal{ | ||
Base: newBase(TypeModal, TitleFromString("modal")), | ||
Config: ModalConfig{ | ||
ModalSize: ModalSizeSmall, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "large", | ||
size: ModalSizeLarge, | ||
expected: &Modal{ | ||
Base: newBase(TypeModal, TitleFromString("modal")), | ||
Config: ModalConfig{ | ||
ModalSize: ModalSizeLarge, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "extra large", | ||
size: ModalSizeExtraLarge, | ||
expected: &Modal{ | ||
Base: newBase(TypeModal, TitleFromString("modal")), | ||
Config: ModalConfig{ | ||
ModalSize: ModalSizeExtraLarge, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
modal := NewModal(TitleFromString("modal")) | ||
modal.SetSize(test.size) | ||
|
||
AssertEqual(t, test.expected, modal) | ||
}) | ||
} | ||
} | ||
|
||
func TestModal_Open(t *testing.T) { | ||
modal := NewModal(TitleFromString("modal")) | ||
modal.Open() | ||
|
||
expected := NewModal(TitleFromString("modal")) | ||
expected.Config.Opened = true | ||
AssertEqual(t, expected, modal) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"body":{ | ||
"metadata":{ | ||
"type":"text" | ||
}, | ||
"config":{ | ||
"value":"test" | ||
} | ||
}, | ||
"opened":true, | ||
"size":"sm" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.