1
0
mirror of https://github.com/cjango/dcat-vue.git synced 2025-12-06 22:40:03 +08:00

新增组件

This commit is contained in:
weiwait
2022-09-16 14:54:27 +08:00
parent 7523cfbb6f
commit 839f7d6bf3
6 changed files with 669 additions and 386 deletions

View File

@@ -37,7 +37,7 @@
![示例图片](https://raw.githubusercontent.com/weiwait/images/main/dcat-filesystem-config.png)
### 已有表单组件
### 已有表单组件(采用的是Naive UI)
```php
$form->vFile('file') // 关联文件系统配置-直传
->accept('mime types');
@@ -59,7 +59,17 @@
->jpeg(0.8) // 裁剪为jpeg格式, 参数为图片质量0-1
->accept('mime types');
$form->vTags('tags');
$form->vTags('tags'); // 标签
$form->vList('list')
->sortable() // 开启排序
->max(8); // 限制最大添加数量
$form->vKeyValue('kvs')
->sortable() // 开启排序
->serial() // 开启固定有序索引 默认为字母A-Z
->keys(['一', '二', '三', '四']) // serial后自定义索引
->list(); // serial后只提交值保存为一维数组(索引仅作为显示)
```
[comment]: <> (### Donate)

File diff suppressed because one or more lines are too long

View File

@@ -9,6 +9,8 @@ use Illuminate\Support\Facades\Event;
use Weiwait\DcatVue\Field\DateRange;
use Weiwait\DcatVue\Field\File;
use Weiwait\DcatVue\Field\Image;
use Weiwait\DcatVue\Field\KeyValue;
use Weiwait\DcatVue\Field\ListField;
use Weiwait\DcatVue\Field\MultipleFile;
use Weiwait\DcatVue\Field\MultipleImage;
use Weiwait\DcatVue\Field\Tag;
@@ -41,6 +43,8 @@ class DcatVueServiceProvider extends ServiceProvider
Form::extend('vImage', Image::class);
Form::extend('vMultipleImage', MultipleImage::class);
Form::extend('vTags', Tag::class);
Form::extend('vList', ListField::class);
Form::extend('vKeyValue', KeyValue::class);
// Form::extend('dateRange', DateRange::class);
Admin::asset()->css(Admin::asset()->getAlias('@weiwait.dcat-vue')['css']);

177
src/Field/KeyValue.php Normal file
View File

@@ -0,0 +1,177 @@
<?php
namespace Weiwait\DcatVue\Field;
use Dcat\Admin\Form\Field;
use Dcat\Admin\Support\Helper;
use Illuminate\Support\Arr;
use Illuminate\Support\MessageBag;
use function Swoole\Coroutine\map;
class KeyValue extends Field\KeyValue
{
protected $view = 'weiwait.dcat-vue::common';
private bool $is_list = false;
private bool $is_serial = false;
private bool $is_sortable = false;
private array $keys = [];
public function getValidator(array $input)
{
if ($this->is_list) {
if ($this->validator) {
return $this->validator->call($this, $input);
}
if (! is_string($this->column)) {
return false;
}
$rules = $attributes = [];
if (! Arr::has($input, $this->column)) {
return false;
}
if ($fieldRules = $this->getRules()) {
$rules["{$this->column}.values.*"] = $fieldRules;
}
$attributes["{$this->column}.values.*"] = __('Value');
$rules["{$this->column}.values"][] = 'array';
$attributes["{$this->column}.values"] = $this->label;
$input = $this->prepareValidatorInput($input);
return validator($input, $rules, $this->getValidationMessages(), $attributes);
}
return parent::getValidator($input);
}
public function formatValidatorMessages($messageBag): MessageBag
{
if ($this->is_list) {
$messages = new MessageBag();
foreach ($messageBag->toArray() as $column => $message) {
$messages->add($this->column, $message);
}
return $messages;
}
return parent::formatValidatorMessages($messageBag);
}
protected function prepareValidatorInput(array $input): array
{
if ($this->is_list) {
Arr::forget($input, "{$this->column}.values.".static::DEFAULT_FLAG_NAME);
return $input;
}
return parent::prepareValidatorInput($input);
}
protected function prepareInputValue($value)
{
if ($this->is_list) {
unset($value['values'][self::DEFAULT_FLAG_NAME]);
if (empty($value['values'])) {
return [];
}
return array_values($value['values']);
}
return parent::prepareInputValue($value);
}
public function render()
{
/****************************** parent ************************************/
$value = $this->value();
$this->value(collect($value)->map(fn($value, $key) => ['key' => $key, 'value' => $value])->values());
$this->addVariables([
'count' => $value ? count($value) : 0,
'keyLabel' => $this->getKeyLabel(),
'valueLabel' => $this->getValueLabel(),
]);
/****************************** field ************************************/
if (!$this->shouldRender()) {
return '';
}
$this->setDefaultClass();
$this->callComposing();
$this->withScript();
/****************************** custom ************************************/
$this->addVariables([
'is_list' => $this->is_list,
'is_serial' => $this->is_serial,
'is_sortable' => $this->is_sortable,
'keys' => $this->keys,
]);
$this->withProvides();
$this->addVariables([
'provides' => $this->variables(),
]);
return view($this->view(), $this->variables());
}
protected function formatAttributes()
{
return $this->attributes;
}
protected function withProvides()
{
$this->addVariables([
'component' => 'KeyValue',
]);
}
public function sortable(bool $sortable = true): self
{
$this->is_sortable = $sortable;
return $this;
}
public function serial(bool $serial = true): self
{
$this->is_serial = $serial;
return $this;
}
public function keys(array $keys): self
{
$this->keys = $keys;
return $this;
}
public function list(bool $list = true): self
{
$this->is_list = $list;
return $this;
}
}

68
src/Field/ListField.php Normal file
View File

@@ -0,0 +1,68 @@
<?php
namespace Weiwait\DcatVue\Field;
use Dcat\Admin\Form\Field;
use Dcat\Admin\Support\Helper;
class ListField extends Field\ListField
{
protected $view = 'weiwait.dcat-vue::common';
public function render()
{
/****************************** parent ************************************/
$value = $this->value();
$this->addVariables(['count' => $value ? count($value) : 0]);
/****************************** field ************************************/
if (!$this->shouldRender()) {
return '';
}
$this->setDefaultClass();
$this->callComposing();
$this->withScript();
/****************************** custom ************************************/
$this->withProvides();
$this->addVariables([
'max' => $this->max ?? 999,
'min' => $this->min,
]);
$this->addVariables([
'provides' => $this->variables(),
]);
return view($this->view(), $this->variables());
}
protected function formatAttributes()
{
return $this->attributes;
}
protected function withProvides()
{
$this->addVariables([
'component' => 'List',
]);
}
public function sortable(bool $sortable = true): self
{
$this->addVariables([
'sortable' => $sortable,
]);
return $this;
}
}

View File

@@ -33,4 +33,8 @@ return [
'2.0.1' => [
'修复文件删除',
],
'2.1.0' => [
'新增list表单组件',
'新增keyValue表单组件',
],
];