enable/disable all html-tags corresponding to a radio button checked state (#1046)

This commit is contained in:
Thomas Rupprecht 2022-12-23 18:31:26 +01:00 committed by GitHub
parent b1b08afd23
commit 2bab370221
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 10 deletions

View File

@ -422,7 +422,11 @@ function admin_shifts()
. form_spinner( . form_spinner(
'angeltype_count_' . $type->id, 'angeltype_count_' . $type->id,
$type->name, $type->name,
$needed_angel_types[$type->id] $needed_angel_types[$type->id],
[
'radio-name' => 'angelmode',
'radio-value' => 'manually'
]
) )
. '</div>'; . '</div>';
} }
@ -465,7 +469,15 @@ function admin_shifts()
__('Length'), __('Length'),
$request->has('length') $request->has('length')
? $request->input('length') ? $request->input('length')
: '120' : '120',
false,
null,
null,
'',
[
'radio-name' => 'mode',
'radio-value' => 'multi'
]
), ),
form_radio( form_radio(
'mode', 'mode',
@ -478,7 +490,15 @@ function admin_shifts()
__('Shift change hours'), __('Shift change hours'),
$request->has('change_hours') $request->has('change_hours')
? $request->input('change_hours') ? $request->input('change_hours')
: '00, 04, 08, 10, 12, 14, 16, 18, 20, 22' : '00, 04, 08, 10, 12, 14, 16, 18, 20, 22',
false,
null,
null,
'',
[
'radio-name' => 'mode',
'radio-value' => 'variable'
]
), ),
form_checkbox( form_checkbox(
'shift_over_midnight', 'shift_over_midnight',

View File

@ -21,19 +21,24 @@ function form_hidden($name, $value)
* @param string $name * @param string $name
* @param string $label * @param string $label
* @param int $value * @param int $value
* @param array $data_attributes
* @return string * @return string
*/ */
function form_spinner(string $name, string $label, int $value) function form_spinner(string $name, string $label, int $value, array $data_attributes = [])
{ {
$id = 'spinner-' . $name; $id = 'spinner-' . $name;
$attr = '';
foreach ($data_attributes as $attr_key => $attr_value) {
$attr .= ' data-' . $attr_key . '="' . $attr_value . '"';
}
return form_element($label, ' return form_element($label, '
<div class="input-group"> <div class="input-group">
<input id="' . $id . '" class="form-control" type="number" min="0" step="1" name="' . $name . '" value="' . $value . '" /> <input id="' . $id . '" class="form-control" type="number" min="0" step="1" name="' . $name . '" value="' . $value . '"' . $attr . ' />
<button class="btn btn-secondary spinner-down" type="button" data-input-id="' . $id . '"> <button class="btn btn-secondary spinner-down" type="button" data-input-id="' . $id . '"' . $attr . '>
' . icon('dash-lg') . ' ' . icon('dash-lg') . '
</button> </button>
<button class="btn btn-secondary spinner-up" type="button" data-input-id="' . $id . '"> <button class="btn btn-secondary spinner-up" type="button" data-input-id="' . $id . '"' . $attr . '>
' . icon('plus-lg') . ' ' . icon('plus-lg') . '
</button> </button>
</div> </div>
@ -205,19 +210,23 @@ function form_submit($name, $label, $class = '', $wrapForm = true, $buttonType =
* @param int|null $maxlength * @param int|null $maxlength
* @param string|null $autocomplete * @param string|null $autocomplete
* @param string|null $class * @param string|null $class
* * @param array $data_attributes
* @return string * @return string
*/ */
function form_text($name, $label, $value, $disabled = false, $maxlength = null, $autocomplete = null, $class = '') function form_text($name, $label, $value, $disabled = false, $maxlength = null, $autocomplete = null, $class = '', $data_attributes = [])
{ {
$disabled = $disabled ? ' disabled="disabled"' : ''; $disabled = $disabled ? ' disabled="disabled"' : '';
$maxlength = $maxlength ? ' maxlength=' . (int)$maxlength : ''; $maxlength = $maxlength ? ' maxlength=' . (int)$maxlength : '';
$autocomplete = $autocomplete ? ' autocomplete="' . $autocomplete . '"' : ''; $autocomplete = $autocomplete ? ' autocomplete="' . $autocomplete . '"' : '';
$attr = '';
foreach ($data_attributes as $attr_key => $attr_value) {
$attr .= ' data-' . $attr_key . '="' . $attr_value . '"';
}
return form_element( return form_element(
$label, $label,
'<input class="form-control" id="form_' . $name . '" type="text" name="' . $name '<input class="form-control" id="form_' . $name . '" type="text" name="' . $name
. '" value="' . htmlspecialchars((string)$value) . '"' . $maxlength . $disabled . $autocomplete . '/>', . '" value="' . htmlspecialchars((string)$value) . '"' . $maxlength . $disabled . $autocomplete . $attr . '/>',
'form_' . $name, 'form_' . $name,
$class $class
); );

View File

@ -111,6 +111,38 @@ ready(() => {
}); });
}); });
/**
* {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled#overview}
*/
const DISABLE_ELEMENTS = [
'button', 'command', 'fieldset', 'input', 'keygen', 'optgroup', 'option', 'select', 'textarea'
];
ready(() => {
// get all input-radio's and add for each an onChange event listener
document.querySelectorAll('input[type="radio"]').forEach((radioElement) => {
// build selector and get all corrsponding elements for this input-radio
const selector = DISABLE_ELEMENTS.map((tagName) => (
`${tagName}[data-radio-name="${radioElement.name}"][data-radio-value]`
)).join(',');
const elements = Array.from(document.querySelectorAll(selector));
// set all states one time on init for each of the corresponding elements
elements.forEach((element) => {
// each radio button updates only his elements
if (element.dataset.radioValue === radioElement.value) {
element.disabled = !radioElement.checked;
}
});
// add an onChange event listener that update the disabled state for all corresponding elements
radioElement.addEventListener('change', () => {
elements.forEach((element) => {
element.disabled = element.dataset.radioValue !== radioElement.value;
});
});
});
});
ready(() => { ready(() => {
document.querySelectorAll('.spinner-down').forEach((element) => { document.querySelectorAll('.spinner-down').forEach((element) => {
const inputElement = document.getElementById(element.dataset.inputId); const inputElement = document.getElementById(element.dataset.inputId);