diff --git a/widgets/component/action.go b/widgets/component/action.go index b720c7aa..9441764d 100644 --- a/widgets/component/action.go +++ b/widgets/component/action.go @@ -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 { diff --git a/widgets/component/action_test.go b/widgets/component/action_test.go index aab6a1ca..49343ed0 100644 --- a/widgets/component/action_test.go +++ b/widgets/component/action_test.go @@ -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" } + }`), } } diff --git a/widgets/component/types.go b/widgets/component/types.go index 44939107..7636a159 100644 --- a/widgets/component/types.go +++ b/widgets/component/types.go @@ -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 Syntactic sugar eq -> value + Equal interface{} `json:"equal,omitempty"` // string | array Syntactic sugar equal -> value Value interface{} `json:"value,omitempty"` // string | array }