Skip to content

Commit

Permalink
Update docs, clean up tests. (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw authored Nov 25, 2023
1 parent 0088f7a commit ed361f9
Show file tree
Hide file tree
Showing 8 changed files with 471 additions and 409 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ to the require-dev section of your `composer.json` file.

## Usage

### View
### Name

```php
use Yii2\Extensions\Filepond\FilePond;

echo FilePond::widget(['name' => 'image_file']);
```

### Active Field

```php
use Yii2\Extensions\Filepond\FilePond;
Expand Down
57 changes: 0 additions & 57 deletions src/File.php

This file was deleted.

54 changes: 36 additions & 18 deletions src/FilePond.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use JsonException;
use PHPForge\Html\Helper\CssClass;
use PHPForge\Html\Helper\Utils;
use RuntimeException;
use PHPForge\Html\Input;
use Yii2\Extensions\FilePond\Asset\FilePondAsset;
use Yii2\Extensions\FilePond\Asset\FilePondCdnAsset;
use Yii;
Expand Down Expand Up @@ -57,7 +57,7 @@ final class FilePond extends InputWidget
*/
public bool $allowPdfPreview = false;
/**
* @var string The CSS class for the filepond container.
* @var string The CSS class to add to the root element.
*/
public string $cssClass = '';
/**
Expand Down Expand Up @@ -273,8 +273,12 @@ final class FilePond extends InputWidget
*/
public bool $required = false;

private string $id = '';

public function init(): void
{
parent::init();

$this->config = array_merge(
[
'acceptedFileTypes' => $this->acceptedFileTypes,
Expand Down Expand Up @@ -329,18 +333,14 @@ public function init(): void
],
$this->config,
);

$this->id = $this->hasModel()
? Utils::generateInputId($this->model->formName(), $this->attribute)
: $this->getId() . '-filepond';
}

public function run(): string
{
if ($this->model === null) {
throw new RuntimeException('The model is not set.');
}

if ($this->attribute === null) {
throw new RuntimeException('The attribute is not set.');
}

$this->registerClientScript();

return $this->renderInputFile();
Expand All @@ -357,7 +357,6 @@ private function getScript(): string
$closure = "{$this->fileValidateTypeDetectType} {$closure}";
}

$id = Utils::generateInputId($this->model->formName(), $this->attribute);
$loadFileDefault = $this->loadFileDefault;
$pluginConfig = implode(', ', $this->pluginDefault);
$setOptions = json_encode($this->config, JSON_THROW_ON_ERROR);
Expand All @@ -367,7 +366,7 @@ private function getScript(): string
FilePond.setOptions($setOptions)
const loadFileDefault = "$loadFileDefault"
const pond = FilePond.create(document.querySelector('input[type="file"][id="$id"]'), {$closure})
const pond = FilePond.create(document.querySelector('input[type="file"][id="$this->id"]'), {$closure})
if (loadFileDefault !== '') {
pond.addFiles(loadFileDefault)
Expand Down Expand Up @@ -396,22 +395,20 @@ private function registerClientScript(): void
*/
private function renderInputFile(): string
{
$name = $this->name;
$options = $this->options;

if (isset($options['class']) && str_contains($options['class'], 'form-control')) {
$options['class'] = str_replace('form-control', '', $options['class']);
}

if (isset($options['placeholder']) && $options['placeholder'] === true) {
unset($options['placeholder']);
}

if (array_key_exists('allowMultiple', $this->config) && $this->config['allowMultiple']) {
$options['multiple'] = true;
}

if (array_key_exists('className', $this->config) && is_string($this->config['className'])) {
CssClass::add($options, $this->config['className']);
$class = str_replace('form-control', '', $this->config['className']);
CssClass::add($options, $class);
}

if (array_key_exists('required', $this->config) && $this->config['required']) {
Expand All @@ -420,6 +417,27 @@ private function renderInputFile(): string

CssClass::add($options, 'filepond');

return File::widget($this->model, $this->attribute)->attributes($options)->render();
$name = match ($this->hasModel()) {
true => Utils::generateArrayableName(Utils::generateInputName($this->model->formName(), $this->attribute)),
default => Utils::generateArrayableName($name),
};

// input type="file" not supported value attribute.
unset($options['id'], $options['placeholder'], $options['value']);

return match ($this->hasModel()) {
true => Input::widget()
->attributes($options)
->id($this->id)
->name($name)
->type('file')
->render(),
default => Input::widget()
->attributes($options)
->id($this->id)
->name($name)
->type('file')
->render(),
};
}
}
Loading

0 comments on commit ed361f9

Please sign in to comment.