Skip to content

Commit 7b25902

Browse files
committed
Add functionality to deselect radio buttons for usage_by_lms
1 parent 9ccdec1 commit 7b25902

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

app/assets/javascripts/tasks_form.coffee

+19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ready = ->
22
initializeFileTypeSelection()
33
initializeVisibilityWarning()
4+
initializeRadioButtonDeselection()
45

56
initializeLoadSelect2 = ->
67
$('#task_programming_language_id').select2
@@ -30,6 +31,24 @@ initializeVisibilityWarning = ->
3031
$('#task_access_level_public').on 'change', ->
3132
warning_message.addClass('d-none')
3233

34+
initializeRadioButtonDeselection = ->
35+
radios = $('.radio-switch input[type="radio"][name*="[usage_by_lms]"]')
36+
hidden_field = $('input[name="file[usage_by_lms]"][type="hidden"]')
37+
38+
radios.each ->
39+
$radio = $(this)
40+
$radio.data('was-checked', $radio.prop('checked'))
41+
42+
radios.on 'click', ->
43+
$radio = $(this)
44+
45+
if $radio.prop('checked') and $radio.data('was-checked')
46+
$radio.prop('checked', false).data('was-checked', false)
47+
hidden_field.val(null) # Reset the value to `nil`
48+
else
49+
radios.data('was-checked', false) # Reset all other radios
50+
$radio.data('was-checked', true)
51+
hidden_field.val('') # Clear hidden field since something is selected
3352

3453
$(document).on('turbolinks:load', ready)
3554
$(document).on('select2:locales:loaded', initializeLoadSelect2)

app/views/tasks/_file_config.html.slim

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
.form-control.placeholder
3232
= file.label :usage_by_lms, TaskFile.human_attribute_name('usage_by_lms'), class: 'form-label w-auto me-2'
3333
.radio-switch
34+
= file.hidden_field :usage_by_lms, value: nil, class: 'fallback'
3435
= file.radio_button :usage_by_lms, 'edit', value: 'edit'
3536
= file.label :usage_by_lms_edit, data: {toggle: 'tooltip', placement: 'bottom'}, title: t('common.button.edit'), class: 'radio-left small-radio radio-third' do
3637
span.fa-stack

0 commit comments

Comments
 (0)