Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/nodus-it/livewire-forms i…
Browse files Browse the repository at this point in the history
…nto refactorings
  • Loading branch information
dtvmedia committed Jan 27, 2024
2 parents 5ac80fa + da249cc commit 495fa25
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 33 deletions.
31 changes: 14 additions & 17 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="Unit">
<directory>./tests/Unit</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<directory>./src/resources</directory>
</exclude>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Unit">
<directory>./tests/Unit</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<directory>./src/resources</directory>
</exclude>
</source>
</phpunit>
2 changes: 1 addition & 1 deletion src/Livewire/FormView.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public function updated($propertyName)
$this->validateOnly($propertyName, null, [], $this->getCustomValidationAttributes());

// In case we have added array validations, we check them here separately
if (isset($this->rules[$propertyName . '.*'])) {
if (isset($this->rules[$propertyName . '.*']) && !empty($this->rules[$propertyName . '.*'])) {
$this->validateOnly($propertyName . '.*', null, [], $this->getCustomValidationAttributes());
}
}
Expand Down
36 changes: 36 additions & 0 deletions src/Services/FormBuilder/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,40 @@ public function preRenderMutator($date)
return $date;
}
}

/**
* Returns the max value of the input
*
* @return string|null
*/
public function getMax(): ?string
{
if ($this->max === null) {
return null;
}

if (!$this->max instanceof Carbon) {
$this->max = Carbon::parse($this->max);
}

return $this->max->toDateString();
}

/**
* Returns the min value of the input
*
* @return string|null
*/
public function getMin(): ?string
{
if ($this->min === null) {
return null;
}

if (!$this->min instanceof Carbon) {
$this->min = Carbon::parse($this->min);
}

return $this->min->toDateString();
}
}
7 changes: 4 additions & 3 deletions src/Services/FormBuilder/Decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function isCurrency(): bool
*
* @return false|NumberFormatter
*/
public function getNumberFormatter(string $locale = 'de_DE'): bool|NumberFormatter
protected function getNumberFormatter(string $locale = 'de_DE'): bool|NumberFormatter
{
$format = NumberFormatter::create($locale, ($this->isCurrency()) ? NumberFormatter::CURRENCY : NumberFormatter::DECIMAL);

Expand All @@ -145,15 +145,16 @@ public function preRenderMutator($value): ?string
}

$unit = '';
$value = $this->parseValue($value);
$formatter = $this->getNumberFormatter();

if ($this->isCurrency()) {
return $formatter->formatCurrency($this->parseValue($value), $this->getUnit()->value);
return $formatter->formatCurrency($value, $this->getUnit()->value);
} elseif (!empty($this->getUnit())) {
$unit = ' ' . $this->getUnit();
}

return $formatter->format($this->parseValue($value)) . $unit;
return $formatter->format($value) . $unit;
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/Services/FormBuilder/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,35 @@ class Textarea extends FormInput
use SupportsSize;
use SupportsHint;
use SupportsPlaceholder;

/**
* Rows count
*
* @var int|null
*/
protected ?int $rows = null;

/**
* Sets the display height of the textarea in rows
*
* @param int $rows
*
* @return $this
*/
public function setRows(int $rows): static
{
$this->rows = $rows;

return $this;
}

/**
* Returns the display height of the textarea in rows
*
* @return int|null
*/
public function getRows(): ?int
{
return $this->rows;
}
}
27 changes: 22 additions & 5 deletions src/resources/js/FormView.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ Nodus.FormView = class {

// Initial render form initializing
document.addEventListener('livewire:load', function() {
this.initInputs(true);
this.initInputs();
}.bind(this));

// On livewire update -> reinitializing the inputs
// ToDo: for dynamically added/removed forms we need to unbind the event listener
Livewire.hook('message.processed', function() {
this.initInputs(false);
this.initInputs();
}.bind(this));

// On livewire DOM update destroy bootstrap selects
Expand Down Expand Up @@ -56,16 +57,28 @@ Nodus.FormView = class {
return classes.join(', ');
}

initInputs(initialRender = true) {
initInputs() {
this.livewireId = this.form.getAttribute('wire:id');
this.livewire = window.livewire.find(this.livewireId);

try {
this.livewire = window.livewire.find(this.livewireId);
} catch (e) {
console.warn('Could not find livewire component', this.livewireId);
return;
}

this.form.querySelectorAll(this.getRelevantInputClasses()).forEach(function(container) {
if (container.hasAttribute('data-init') && container.getAttribute('data-init') === 'true') {
console.log('Already initialized', container);
return;
}

const staticContainer = container.querySelector('.nodus-form-container-static');
if (staticContainer !== null && staticContainer.hasAttribute('data-init') && staticContainer.getAttribute('data-init') === 'true') {
console.log('Already initialized', staticContainer);
return;
}

console.log('Init form control: ', container);

if (container.classList.contains('nodus-form-control-select')) {
Expand All @@ -74,11 +87,15 @@ Nodus.FormView = class {
this.initDecimal(container);
} else if (container.classList.contains('nodus-form-control-code')) {
this.initCode(container);
} else if (container.classList.contains('nodus-form-control-richtextarea') && initialRender === true) {
} else if (container.classList.contains('nodus-form-control-richtextarea')) {
this.initRichtextArea(container);
}

container.setAttribute('data-init', 'true');

if (staticContainer !== null) {
staticContainer.setAttribute('data-init', 'true');
}
}.bind(this));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
id="{{ $input->getId(true) }}_container"
data-id="{{ $input->getId(true) }}"
data-mode="{{ $input->getMode() }}">
<div wire:ignore>
<textarea class="d-none" id="{{ $input->getId(true) }}">{!! $this->values[$input->getId()] !!}</textarea>
<div wire:ignore class="nodus-form-container-static">
<textarea class="d-none" id="{{ $input->getId(true) }}">{!! $this->getValue($input->getId()) !!}</textarea>
</div>
<textarea name="{{ $input->getId() }}"
id="{{ $input->getId(true) }}_text"
class="d-none"
wire:model.defer="{{ $input->getViewId() }}"
>{!! $this->values[$input->getId()] !!}</textarea>
>{!! $this->getValue($input->getId()) !!}</textarea>
@include('nodus.packages.livewire-forms::livewire.'.config('livewire-forms.theme').'.components.validation')
</div>
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<div class="nodus-form-control nodus-form-control-richtextarea" id="{{ $input->getId(true) }}_container" data-id="{{ $input->getId(true) }}">
<div wire:ignore>
<div wire:ignore class="nodus-form-container-static">
<div id="{{ $input->getId(true) }}">
{!! $this->values[$input->getId()] !!}
{!! $this->getValue($input->getId()) !!}
</div>
</div>
<textarea name="{{ $input->getId() }}"
id="{{ $input->getId(true) }}_text"
class="d-none"
wire:model.defer="{{ $input->getViewId() }}"
>{!! $this->values[$input->getId()] !!}</textarea>
>{!! $this->getValue($input->getId()) !!}</textarea>
@include('nodus.packages.livewire-forms::livewire.'.config('livewire-forms.theme').'.components.validation')
</div>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div class="nodus-form-control nodus-form-control-textarea" id="{{ $input->getId(true) }}_container" data-id="{{ $input->getId(true) }}">
<textarea name="{{ $input->getName() }}"
@if($input->hasPlaceholder()) placeholder="{{ $input->getPlaceholder() }}" @endif
@if($input->getRows() !== null) rows="{{ $input->getRows() }}" @endif
class="form-control @if(isset($errors) && $errors->hasAny($input->getErrorKeys())) is-invalid @endif"
wire:model.{{config('livewire-forms.update_mode')}}="{{ $input->getViewId() }}">
</textarea>
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/InputTests/TraitsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function testSupportsValidations()
$this->assertSame('unique:mysql.test,id,1', $input->rewriteValidationRules(new TestModel(['id' => 1])));
}

public function validTranslations()
public static function validTranslations()
{
return [
['DeselectAllText', 'deselect_all'],
Expand Down

0 comments on commit 495fa25

Please sign in to comment.