diff --git a/app/Admin/Extensions/Form/Radio2.php b/app/Admin/Extensions/Form/Radio2.php new file mode 100644 index 0000000..4efe88f --- /dev/null +++ b/app/Admin/Extensions/Form/Radio2.php @@ -0,0 +1,143 @@ +options instanceof \Closure) { + $this->options( + $this->options->call($this->values(), $this->value(), $this) + ); + } + + $this->addCascadeScript(); + + $radio = WidgetRadio::make($this->getElementName(), $this->options, $this->style); + + if ($this->attributes['disabled'] ?? false) { + $radio->disable(); + } + + $radio + ->inline($this->inline) + ->check($this->value()) + ->class($this->getElementClassString()) + ->size($this->size); + + $this->addVariables([ + 'radio' => $radio, + ]); + + return parent::render(); + } + + /** + * Add cascade scripts to contents. + * + * @return string + */ + protected function getCascadeScript() + { + if (empty($this->conditions)) { + return; + } + + $cascadeGroups = collect($this->conditions)->map(function ($condition) { + return [ + 'class' => $this->getCascadeClass($condition['value'], $condition['operator']), + 'operator' => $condition['operator'], + 'value' => $condition['value'], + ]; + })->toJson(); + + return <<': function(a, b) { + return a > b; + }, + '<': function(a, b) { + return a < b; + }, + '>=': function(a, b) { return a >= b; }, + '<=': function(a, b) { return a <= b; }, + '!=': function(a, b) { + return ! operator_table['='](a, b); + }, + 'in': function(a, b) { return Dcat.helpers.inObject(a, String(b), true); }, + 'notIn': function(a, b) { return ! Dcat.helpers.inObject(a, String(b), true); }, + 'has': function(a, b) { return Dcat.helpers.inObject(b, String(b), true); }, + }; + var cascade_groups = {$cascadeGroups}, event = '{$this->cascadeEvent}'; + + \$this.on(event, function (e) { + {$this->getFormFrontValue()} + + cascade_groups.forEach(function (event) { + var group = $(e.target).closest('.form-group.form-field').next('div.cascade-group.'+event.class); + if (compare(checked, event.value, event.operator)) { + group.removeClass('d-none'); + } else { + group.addClass('d-none'); + } + }); + }).trigger(event); +})(); +JS; + } + + /** + * @return string + */ + protected function getFormFrontValue() + { + return <<getElementClassSelector()}:checked').val(); +JS; + } +} diff --git a/app/AdminSupplier/Controllers/DiyFormController.php b/app/AdminSupplier/Controllers/DiyFormController.php index 899947a..d28e12a 100644 --- a/app/AdminSupplier/Controllers/DiyFormController.php +++ b/app/AdminSupplier/Controllers/DiyFormController.php @@ -2,6 +2,7 @@ namespace App\AdminSupplier\Controllers; +use App\Admin\Extensions\Form\Radio2; use App\AdminSupplier\Repositories\DiyForm; use Dcat\Admin\Admin; use Dcat\Admin\Form; @@ -80,6 +81,7 @@ class DiyFormController extends AdminController */ protected function form() { + Form::extend('radio2', Radio2::class); return Form::make(new DiyForm(['fields']), function (Form $form) { $form->disableViewButton(); @@ -93,9 +95,10 @@ class DiyFormController extends AdminController $form->hasMany('fields', function (Form\NestedForm $form) { $form->text('field', '字段名称')->required(); $form->switch('required', '是否必填')->default(1)->required(); - $form->radio('type', '字段类型') + $form->radio2('type', '字段类型') ->required()->default('text') - ->options(admin_trans('diy-form.options'))->when(['radio', 'checkbox'], function (Form\NestedForm $form) { + ->options(admin_trans('diy-form.options')) + ->when(['radio', 'checkbox'], function (Form\NestedForm $form) { $form->list('options', '选项列表'); }); $form->number('sort', '排序')