Skip to content

Commit 9d058a4

Browse files
committed
New feature: addStyle action
1 parent bd77971 commit 9d058a4

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Actions:
3939
+ **slide**: hides elements (using sliding)
4040
+ **fade**: hides elements (using fading)
4141
+ **addClass**: adds classes (ex. `"data-then": "addClass red"`)
42+
+ **addStyle**: adds some styles (ex. `"data-then": "addStyle color: #fb1; font-size: 12px"`)
4243
+ **setText**: set the text of an element (ex. `"data-then": "setText A sample text"`)
4344
+ **setValue**: set the value of an input element (ex. `"data-then": "setValue A sample value"`)
4445
+ **callback**: call a function (with arguments: **data-args**) (ex. `"data-then": "callback a_fun"`)

app/assets/javascripts/activeadmin/dynamic_fields.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
const ACTIONS = {
55
addClass: (el, name) => el.addClass(name),
6+
addStyle: (el, extra_style) => {
7+
let style = (el.attr('style') || '').trim()
8+
if (!style.includes(extra_style)) {
9+
if (style) style = style.replace(/;$/, '') + '; ' // ensure style ends with ;
10+
el.attr('style', `${style}${extra_style}`)
11+
}
12+
},
613
callback: (el, name) => {
714
if (window[name]) window[name](el.data('args'))
815
else {
@@ -33,6 +40,9 @@
3340

3441
const REVERSE_ACTIONS = {
3542
addClass: (el, name) => el.removeClass(name),
43+
addStyle: (el, extra_style) => {
44+
if(el.attr('style')) el.attr('style', el.attr('style').replace(extra_style, ''))
45+
},
3646
fade: el => el.fadeIn(),
3747
hide: el => el.show(),
3848
slide: el => el.slideDown()

spec/dummy/app/admin/posts.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
end
3939

4040
form do |f|
41-
def add_field(form, name, type, data, override_options = {})
42-
options = { as: type, input_html: { data: data }, hint: data.inspect }
41+
def add_field(form, name, type, data, override_options = {}, extra_attrs = {})
42+
options = { as: type, input_html: { data: data }.merge(extra_attrs), hint: data.inspect }
4343
options.merge! override_options
4444
form.input name, options
4545
end
@@ -139,6 +139,10 @@ def add_field(form, name, type, data, override_options = {})
139139
df271 = { if: 'checked', then: 'setText data test', target: '#post_data_field_271_input .inline-hints' }
140140
add_field(f, :data_field_271, :boolean, df271)
141141

142+
# --- addStyle
143+
df281 = { if: 'checked', then: 'addStyle font-size: 10px; color: red', target: '#post_data_field_281' }
144+
add_field(f, :data_field_281, :boolean, df281, {}, { 'style': 'margin-right: 20px' })
145+
142146
# --- gtarget
143147
df301 = { if: 'checked', then: 'addClass red', gtarget: 'body.active_admin' }
144148
add_field(f, :data_field_301, :boolean, df301)

spec/system/dynamic_fields_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@
139139
find('#post_data_field_271').click
140140
expect(find('#post_data_field_271_input .inline-hints').text).to eq 'data test'
141141

142+
# --- addStyle
143+
expect(find('#post_data_field_281')[:style]).to eq 'margin-right: 20px;'
144+
find('#post_data_field_281').click
145+
expect(find('#post_data_field_281')[:style]).to eq 'margin-right: 20px; font-size: 10px; color: red;'
146+
find('#post_data_field_281').click
147+
expect(find('#post_data_field_281')[:style]).to eq 'margin-right: 20px;'
148+
142149
# --- gtarget
143150
expect(page).not_to have_css('body.active_admin.red')
144151
find('#post_data_field_301').click

0 commit comments

Comments
 (0)