Skip to content

Commit d70fb2b

Browse files
authored
Merge pull request #30 from blocknotes/feat/new-args-syntax
feat: New args syntax
2 parents b41c0d7 + 3853e24 commit d70fb2b

File tree

4 files changed

+56
-47
lines changed

4 files changed

+56
-47
lines changed

README.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,14 @@ Actions:
6363
+ **hide**: hides elements (ex. `"data-then": "hide", "data-target": ".errors"`)
6464
+ **slide**: hides elements (using sliding)
6565
+ **fade**: hides elements (using fading)
66-
+ **addClass**: adds classes (ex. `"data-then": "addClass red"`)
67-
+ **addStyle**: adds some styles (ex. `"data-then": "addStyle color: #fb1; font-size: 12px"`)
68-
+ **setText**: set the text of an element (ex. `"data-then": "setText A sample text"`)
69-
+ **setValue**: set the value of an input element (ex. `"data-then": "setValue A sample value"`)
66+
+ **addClass**: adds classes (ex. `"data-then": "addClass", "data-args": "red"`)
67+
+ **addStyle**: adds some styles (ex. `"data-then": "addStyle", "data-args": "color: #fb1; font-size: 12px"`)
68+
+ **setText**: set the text of an element (ex. `"data-then": "setText", "data-args": "A sample text"`)
69+
+ **setValue**: set the value of an input element (ex. `"data-then": "setValue", "data-args": "A sample value"`)
7070
+ **callback**: call a function (with arguments: **data-args**) (ex. `"data-then": "callback a_fun"`)
71+
- **data-args**: arguments passed to the triggered action (or to the callback function)
7172
- **data-else**: action to trigger when the condition check is not true
72-
- **data-args**: arguments passed to the callback function
73+
- **data-else-args**: arguments passed to the triggered else action
7374

7475
Targets:
7576

@@ -98,14 +99,14 @@ end
9899
- Add 3 classes (*first*, *second*, *third*) if a checkbox is not checked, else add "forth" class:
99100

100101
```rb
101-
data = { if: 'not_checked', then: 'addClass first second third', target: '.grp1', else: 'addClass forth' }
102+
data = { if: 'not_checked', then: 'addClass', args: 'first second third', target: '.grp1', else: 'addClass', 'else-args': 'forth' }
102103
f.input :published, input_html: { data: data }
103104
```
104105

105106
- Set another field value if a string field is blank:
106107

107108
```rb
108-
f.input :title, input_html: { data: { if: 'blank', then: 'setValue 10', target: '#article_position' } }
109+
f.input :title, input_html: { data: { if: 'blank', then: 'setValue', args: '10', target: '#article_position' } }
109110
```
110111

111112
- Use a custom function for conditional check (*title_not_empty()* must be available on global scope) (with alternative syntax for data attributes):

app/assets/javascripts/activeadmin/dynamic_fields.js

+19-11
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,27 @@
7777
}
7878

7979
evaluateAction() {
80-
const action = this.el.data('then') || this.el.data('action') || ''
81-
const action_name = action.split(' ', 1)[0]
82-
const else_action = this.el.data('else') || ''
83-
const else_action_name = else_action.split(' ', 1)[0]
80+
const dataAction = this.el.data('then') || this.el.data('action') || ''
81+
const actionName = dataAction.split(' ', 1)[0]
82+
const dataElse = this.el.data('else') || ''
83+
const elseActionName = dataElse.split(' ', 1)[0]
8484

85-
this.action = ACTIONS[action_name]
86-
this.action_arg = action.substring(action.indexOf(' ') + 1)
87-
this.reverse_action = REVERSE_ACTIONS[action_name]
88-
this.else_action = ACTIONS[else_action_name]
89-
this.else_action_arg = else_action.substring(else_action.indexOf(' ') + 1)
90-
this.else_reverse_action = REVERSE_ACTIONS[else_action_name]
85+
this.action = ACTIONS[actionName]
86+
if (actionName == 'callback') {
87+
this.action_arg = dataAction.substring(dataAction.indexOf(' ') + 1)
88+
} else {
89+
this.action_arg = this.el.data('args') || dataAction.substring(dataAction.indexOf(' ') + 1)
90+
}
91+
this.reverse_action = REVERSE_ACTIONS[actionName]
92+
this.else_action = ACTIONS[elseActionName]
93+
if (elseActionName == 'callback') {
94+
this.else_action_arg = dataElse.substring(dataElse.indexOf(' ') + 1)
95+
} else {
96+
this.else_action_arg = this.el.data('else-args') || dataElse.substring(dataElse.indexOf(' ') + 1)
97+
}
98+
this.else_reverse_action = REVERSE_ACTIONS[elseActionName]
9199

92-
return action_name
100+
return actionName
93101
}
94102

95103
evaluateCondition() {

spec/dummy/app/admin/posts.rb

+28-28
Original file line numberDiff line numberDiff line change
@@ -53,79 +53,79 @@ def add_field(form, name, type, data, override_options = {}, extra_attrs = {})
5353
f.input :data_test, as: :string
5454

5555
# --- if
56-
df111 = { if: 'checked', then: 'addClass red', target: '#post_data_field_111_input label' }
56+
df111 = { if: 'checked', then: 'addClass', args: 'red', target: '#post_data_field_111_input label' }
5757
add_field(f, :data_field_111, :boolean, df111)
5858

59-
df112 = { if: '!checked', then: 'addClass red', target: '#post_data_field_112_input label' }
59+
df112 = { if: '!checked', then: 'addClass', args: 'red', target: '#post_data_field_112_input label' }
6060
add_field(f, :data_field_112, :boolean, df112)
6161

62-
df121 = { if: 'not_checked', then: 'addClass red', target: '#post_data_field_121_input label' }
62+
df121 = { if: 'not_checked', then: 'addClass', args: 'red', target: '#post_data_field_121_input label' }
6363
add_field(f, :data_field_121, :boolean, df121)
6464

65-
df131 = { if: 'blank', then: 'addClass red', target: '#post_data_field_131_input label' }
65+
df131 = { if: 'blank', then: 'addClass', args: 'red', target: '#post_data_field_131_input label' }
6666
add_field(f, :data_field_131, :string, df131)
6767

68-
df132 = { if: 'blank', then: 'addClass red', target: '#post_data_field_132_input label' }
68+
df132 = { if: 'blank', then: 'addClass', args: 'red', target: '#post_data_field_132_input label' }
6969
add_field(f, :data_field_132, :text, df132)
7070

71-
df141 = { if: 'not_blank', then: 'addClass red', target: '#post_data_field_141_input label' }
71+
df141 = { if: 'not_blank', then: 'addClass', args: 'red', target: '#post_data_field_141_input label' }
7272
add_field(f, :data_field_141, :string, df141)
7373

74-
df142 = { if: 'not_blank', then: 'addClass red', target: '#post_data_field_142_input label' }
74+
df142 = { if: 'not_blank', then: 'addClass', args: 'red', target: '#post_data_field_142_input label' }
7575
add_field(f, :data_field_142, :text, df142)
7676

77-
df151 = { if: 'changed', then: 'addClass red', target: '#post_data_field_151_input label' }
77+
df151 = { if: 'changed', then: 'addClass', args: 'red', target: '#post_data_field_151_input label' }
7878
add_field(f, :data_field_151, :boolean, df151)
7979

80-
df152 = { if: 'changed', then: 'addClass red', target: '#post_data_field_152_input label' }
80+
df152 = { if: 'changed', then: 'addClass', args: 'red', target: '#post_data_field_152_input label' }
8181
add_field(f, :data_field_152, :string, df152)
8282

83-
df153 = { if: 'changed', then: 'addClass red', target: '#post_data_field_153_input label' }
83+
df153 = { if: 'changed', then: 'addClass', args: 'red', target: '#post_data_field_153_input label' }
8484
add_field(f, :data_field_153, :text, df153)
8585

8686
# --- eq
87-
df161 = { eq: '161', then: 'addClass red', target: '#post_data_field_161_input label' }
87+
df161 = { eq: '161', then: 'addClass', args: 'red', target: '#post_data_field_161_input label' }
8888
add_field(f, :data_field_161, :string, df161)
8989

90-
df162 = { eq: '162', then: 'addClass red', target: '#post_data_field_162_input label' }
90+
df162 = { eq: '162', then: 'addClass', args: 'red', target: '#post_data_field_162_input label' }
9191
add_field(f, :data_field_162, :select, df162, collection: [161, 162, 163])
9292

93-
df163 = { eq: '163', then: 'addClass red', target: '#post_data_field_163_input label' }
93+
df163 = { eq: '163', then: 'addClass', args: 'red', target: '#post_data_field_163_input label' }
9494
add_field(f, :data_field_163, :text, df163)
9595

96-
df164 = { eq: '!164', then: 'addClass red', target: '#post_data_field_164_input label' }
96+
df164 = { eq: '!164', then: 'addClass', args: 'red', target: '#post_data_field_164_input label' }
9797
add_field(f, :data_field_164, :string, df164)
9898

9999
# --- not
100-
df171 = { not: '171', then: 'addClass red', target: '#post_data_field_171_input label' }
100+
df171 = { not: '171', then: 'addClass', args: 'red', target: '#post_data_field_171_input label' }
101101
add_field(f, :data_field_171, :string, df171)
102102

103-
df172 = { not: '172', then: 'addClass red', target: '#post_data_field_172_input label' }
103+
df172 = { not: '172', then: 'addClass', args: 'red', target: '#post_data_field_172_input label' }
104104
add_field(f, :data_field_172, :select, df172, collection: [171, 172, 173])
105105

106-
df173 = { not: '173', then: 'addClass red', target: '#post_data_field_173_input label' }
106+
df173 = { not: '173', then: 'addClass', args: 'red', target: '#post_data_field_173_input label' }
107107
add_field(f, :data_field_173, :text, df173)
108108

109109
# --- match
110-
df181 = { match: 'Something\s', then: 'addClass red', target: '#post_data_field_181_input label' }
110+
df181 = { match: 'Something\s', then: 'addClass', args: 'red', target: '#post_data_field_181_input label' }
111111
add_field(f, :data_field_181, :string, df181)
112112

113113
# --- mismatch
114-
df191 = { mismatch: '^\d+$', then: 'addClass red', target: '#post_data_field_191_input label' }
114+
df191 = { mismatch: '^\d+$', then: 'addClass', args: 'red', target: '#post_data_field_191_input label' }
115115
add_field(f, :data_field_191, :string, df191)
116116

117117
# --- function
118-
df201 = { function: 'test_fun', then: 'addClass red', target: '#post_data_field_201_input label' }
118+
df201 = { function: 'test_fun', then: 'addClass', args: 'red', target: '#post_data_field_201_input label' }
119119
add_field(f, :data_field_201, :string, df201)
120120

121-
df202 = { function: 'missing_fun', then: 'addClass red', target: '#post_data_field_202_input label' }
121+
df202 = { function: 'missing_fun', then: 'addClass', args: 'red', target: '#post_data_field_202_input label' }
122122
add_field(f, :data_field_202, :string, df202)
123123

124124
df203 = { function: 'test_fun2' }
125125
add_field(f, :data_field_203, :boolean, df203)
126126

127127
# --- addClass
128-
df211 = { if: 'checked', then: 'addClass red', target: '#post_data_field_211_input label' }
128+
df211 = { if: 'checked', then: 'addClass', args: 'red', target: '#post_data_field_211_input label' }
129129
add_field(f, :data_field_211, :boolean, df211)
130130

131131
# --- callback
@@ -136,7 +136,7 @@ def add_field(form, name, type, data, override_options = {}, extra_attrs = {})
136136
add_field(f, :data_field_222, :boolean, df222)
137137

138138
# --- setValue
139-
df231 = { if: 'checked', then: 'setValue data test', target: '#post_data_test' }
139+
df231 = { if: 'checked', then: 'setValue', args: 'data test', target: '#post_data_test' }
140140
add_field(f, :data_field_231, :boolean, df231)
141141

142142
# --- hide
@@ -152,23 +152,23 @@ def add_field(form, name, type, data, override_options = {}, extra_attrs = {})
152152
add_field(f, :data_field_261, :boolean, df261)
153153

154154
# --- setText
155-
df271 = { if: 'checked', then: 'setText data test', target: '#post_data_field_271_input .inline-hints' }
155+
df271 = { if: 'checked', then: 'setText', args: 'data test', target: '#post_data_field_271_input .inline-hints' }
156156
add_field(f, :data_field_271, :boolean, df271)
157157

158158
# --- addStyle
159-
df281 = { if: 'checked', then: 'addStyle font-size: 10px; padding: 3px', target: '#post_data_field_281' }
159+
df281 = { if: 'checked', then: 'addStyle', args: 'font-size: 10px; padding: 3px', target: '#post_data_field_281' }
160160
add_field(f, :data_field_281, :boolean, df281, {}, { 'style': 'margin-right: 20px' })
161161

162162
# --- gtarget
163-
df301 = { if: 'checked', then: 'addClass red', gtarget: 'body.active_admin' }
163+
df301 = { if: 'checked', then: 'addClass', args: 'red', gtarget: 'body.active_admin' }
164164
add_field(f, :data_field_301, :boolean, df301)
165165

166166
# This will not work - here only for testing:
167-
df302 = { if: 'checked', then: 'addClass red', target: 'body.active_admin' }
167+
df302 = { if: 'checked', then: 'addClass', args: 'red', target: 'body.active_admin' }
168168
add_field(f, :data_field_302, :boolean, df302)
169169

170170
# --- else
171-
df321 = { if: 'checked', then: 'addClass red', target: '#post_data_field_321_input label', else: 'addClass green' }
171+
df321 = { if: 'checked', then: 'addClass', args: 'red', target: '#post_data_field_321_input label', else: 'addClass', "else-args": "green" }
172172
add_field(f, :data_field_321, :boolean, df321)
173173
end
174174

spec/system/dynamic_fields_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def test_change_css(target, options = {})
6969
it 'checks the conditions and actions', retry: 3 do # rubocop:disable RSpec/ExampleLength, RSpec/MultipleExpectations
7070
visit "/admin/posts/#{post.id}/edit"
7171

72-
expect(page).to have_css('#post_data_field_111[data-if="checked"][data-then="addClass red"][data-target="#post_data_field_111_input label"]')
72+
expect(page).to have_css('#post_data_field_111[data-if="checked"][data-then="addClass"][data-args="red"][data-target="#post_data_field_111_input label"]')
7373

7474
# --- if
7575
spec_message('check data-if condition')

0 commit comments

Comments
 (0)