Skip to content

Commit ac3fe35

Browse files
committed
Provide a way to clear belongs_to selection without keyboard
Closes #2090
1 parent 3bf5065 commit ac3fe35

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

app/assets/javascripts/rails_admin/ra.filtering-select.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,20 @@
8585
}
8686
};
8787

88-
return $.map(
88+
if (request.term.length > 0 || this.input.attr('required')) {
89+
var additionalOptions = [];
90+
} else {
91+
var additionalOptions = [{
92+
html: $('<span style="color: #888"></span>').append(
93+
'<i class="icon-remove"></i> ' +
94+
$('<span></span>').text(RailsAdmin.I18n.t("clear")).html()
95+
),
96+
value: null,
97+
id: null
98+
}];
99+
}
100+
101+
return additionalOptions.concat($.map(
89102
data,
90103
function(el) {
91104
var id = el.id || el.value;
@@ -99,7 +112,7 @@
99112
id: id
100113
};
101114
}
102-
});
115+
}));
103116
},
104117

105118
_getSourceFunction: function(source) {

config/locales/rails_admin.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ en:
1919
ends_with: Ends with
2020
too_many_objects: "Too many objects, use search box above"
2121
no_objects: "No objects found"
22+
clear: Clear
2223
loading: "Loading..."
2324
toggle_navigation: Toggle navigation
2425
home:

spec/integration/widgets/filtering_select_spec.rb

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@
4444
end
4545

4646
context 'on update' do
47-
before { visit edit_path(model_name: 'player', id: player.id) }
48-
4947
it 'changes the selected value' do
48+
visit edit_path(model_name: 'player', id: player.id)
5049
expect(find('#player_team_id', visible: false).value).to eq teams[0].id.to_s
5150
find('input.ra-filtering-select-input').set('Tex')
5251
page.execute_script("$('input.ra-filtering-select-input').trigger('focus').trigger('keydown')")
@@ -56,11 +55,33 @@
5655
expect(find('#player_team_id', visible: false).value).to eq teams[1].id.to_s
5756
end
5857

59-
it 'clears the current selection' do
58+
it 'clears the current selection with making the search box empty' do
59+
visit edit_path(model_name: 'player', id: player.id)
6060
find('input.ra-filtering-select-input').set('')
6161
page.execute_script("$('input.ra-filtering-select-input').trigger('keyup')")
6262
expect(find('#player_team_id', visible: false).value).to be_empty
6363
end
64+
65+
it 'clears the current selection with selecting the clear option' do
66+
visit edit_path(model_name: 'player', id: player.id)
67+
within('.filtering-select') { find('.dropdown-toggle').click }
68+
find('a.ui-menu-item-wrapper', text: /Clear/).click
69+
expect(find('#player_team_id', visible: false).value).to be_empty
70+
end
71+
72+
context 'when the field is required' do
73+
before do
74+
RailsAdmin.config Player do
75+
field(:team) { required true }
76+
end
77+
visit edit_path(model_name: 'player', id: player.id)
78+
end
79+
80+
it 'does not show the clear option' do
81+
within('.filtering-select') { find('.dropdown-toggle').click }
82+
is_expected.not_to have_css('a.ui-menu-item-wrapper', text: /Clear/)
83+
end
84+
end
6485
end
6586

6687
it 'prevents duplication when using browser back and forward' do

0 commit comments

Comments
 (0)