[add] syntactic sugar action.disabled

This commit is contained in:
Max 2023-01-19 09:41:53 +08:00
parent b81eb750a3
commit 827b4cc3d3
3 changed files with 77 additions and 0 deletions

View file

@ -23,6 +23,21 @@ func (action *ActionDSL) UnmarshalJSON(data []byte) error {
return err
}
// Syntactic sugar Disabled
if action.Disabled != nil {
if action.Disabled.Eq != nil {
action.Disabled.Value = action.Disabled.Eq
}
if action.Disabled.Equal != nil {
action.Disabled.Value = action.Disabled.Equal
}
if action.Disabled.Field != "" {
action.Disabled.Bind = fmt.Sprintf("{{%s}}", action.Disabled.Field)
}
}
// Syntactic sugar { "hide": ["add", "edit", "view"] }
// ["add", "edit", "view"]
if action.Hide != nil {

View file

@ -74,6 +74,30 @@ func TestActionUnmarshalJSON(t *testing.T) {
assert.Equal(t, "Actions.test.back", action.Action[0]["type"])
assert.Equal(t, "7daf632016d4d8ea77066bc54afd525e", action.ID)
action = ActionDSL{}
err = jsoniter.Unmarshal(data["sugar-hide"], &action)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, false, action.ShowWhenAdd)
assert.Equal(t, false, action.ShowWhenView)
assert.Equal(t, false, action.HideWhenEdit)
action = ActionDSL{}
err = jsoniter.Unmarshal(data["sugar-disabled-eq"], &action)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "{{data}}", action.Disabled.Bind)
assert.Equal(t, "1", action.Disabled.Value)
action = ActionDSL{}
err = jsoniter.Unmarshal(data["sugar-disabled-equal"], &action)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "{{data}}", action.Disabled.Bind)
assert.Equal(t, "1", action.Disabled.Value)
}
// testActionData
@ -159,5 +183,40 @@ func testActionData() map[string][]byte {
},
"confirm": { "title": "Tips", "desc": "Delete Confirm" }
}`),
"sugar-hide": []byte(`{
"title": "Delete",
"icon": "icon-trash-2",
"style": "danger",
"hide": ["view", "add"],
"action": {
"Actions.test.back": { "pathname": "/x/Table/env" }
},
"confirm": { "title": "Tips", "desc": "Delete Confirm" }
}`),
"sugar-disabled-eq": []byte(`{
"title": "Delete",
"icon": "icon-trash-2",
"style": "danger",
"hide": ["view", "add"],
"action": {
"Actions.test.back": { "pathname": "/x/Table/env" }
},
"confirm": { "title": "Tips", "desc": "Delete Confirm" },
"disabled": { "field":"data", "eq": "1" }
}`),
"sugar-disabled-equal": []byte(`{
"title": "Delete",
"icon": "icon-trash-2",
"style": "danger",
"hide": ["view", "add"],
"action": {
"Actions.test.back": { "pathname": "/x/Table/env" }
},
"confirm": { "title": "Tips", "desc": "Delete Confirm" },
"disabled": {"field":"data", "equal": "1" }
}`),
}
}

View file

@ -52,7 +52,10 @@ type ActionDSL struct {
// DisabledDSL the action disabled
type DisabledDSL struct {
Field string `json:"Field,omitempty"` // Syntactic sugar -> bind
Bind string `json:"bind,omitempty"`
Eq interface{} `json:"eq,omitempty"` // string | array<string> Syntactic sugar eq -> value
Equal interface{} `json:"equal,omitempty"` // string | array<string> Syntactic sugar equal -> value
Value interface{} `json:"value,omitempty"` // string | array<string>
}