From 1ce6ddd8cb227329e424a6094fb93c4ba3482828 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 24 Dec 2022 12:06:39 +0800 Subject: [PATCH] feat(web): UOJForm::addCheckboxes() --- web/app/models/UOJForm.php | 79 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/web/app/models/UOJForm.php b/web/app/models/UOJForm.php index b022fef..5b0adca 100644 --- a/web/app/models/UOJForm.php +++ b/web/app/models/UOJForm.php @@ -259,6 +259,85 @@ class UOJForm { ); } + public function addCheckboxes($name, $config) { + $config += [ + 'div_class' => '', + 'select_class' => '', + 'label' => '', + 'label_class' => 'form-check-label', + 'options' => [], + 'default_value' => '', + 'option_div_class' => 'form-check', + 'option_class' => 'form-check-input', + 'option_label_class' => 'form-check-label', + 'help' => '', + 'help_class' => 'form-text', + 'disabled' => false, + ]; + + $html = ''; + $html .= HTML::tag_begin('div', ['id' => "div-$name", 'class' => $config['div_class']]); + + // Label + if ($config['label']) { + $html .= HTML::tag('label', [ + 'class' => $config['label_class'], + 'for' => "input-$name", + ], $config['label']); + } + + // Select + $html .= HTML::tag_begin('div', ['class' => $config['select_class']]); + + foreach ($config['options'] as $opt_name => $opt_label) { + $html .= HTML::tag_begin('div', ['class' => $config['option_div_class']]); + + if ($opt_name == $config['default_value']) { + $html .= HTML::empty_tag('input', [ + 'name' => $name, + 'id' => "input-$name-$opt_name", + 'class' => $config['option_class'], + 'type' => 'radio', + 'value' => $opt_name, + 'checked' => 'checked', + ]); + } else { + $html .= HTML::empty_tag('input', [ + 'name' => $name, + 'id' => "input-$name-$opt_name", + 'class' => $config['option_class'], + 'type' => 'radio', + 'value' => $opt_name, + ]); + } + + $html .= HTML::tag('label', [ + 'class' => $config['option_label_class'], + 'for' => "input-$name-$opt_name", + ], $opt_label); + + $html .= HTML::tag_end('div'); + } + + $html .= HTML::tag_end('div'); + + // Help text + if ($config['help']) { + $html .= HTML::tag('div', ['class' => $config['help_class']], $config['help']); + } + + $html .= HTML::tag_end('div'); + + $this->add( + $name, + $html, + function ($opt) use ($config) { + return isset($config['options'][$opt]) ? '' : "ๆ— ๆ•ˆ้€‰้กน"; + }, + null + ); + } + public function printHTML() { echo HTML::tag_begin('form', [ 'action' => UOJContext::requestURI(),