From f1f2cd62db3bb6ff2e41d01c38835dacca207c57 Mon Sep 17 00:00:00 2001 From: levchenko-ivan Date: Sun, 6 Oct 2024 00:29:54 +0300 Subject: [PATCH 1/2] fix(date-range): fix step param type --- src/UI/src/Fields/DateRange.php | 2 +- tests/Unit/Fields/DateRangeFieldTest.php | 26 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/UI/src/Fields/DateRange.php b/src/UI/src/Fields/DateRange.php index 20a18740b..99281de9d 100644 --- a/src/UI/src/Fields/DateRange.php +++ b/src/UI/src/Fields/DateRange.php @@ -56,7 +56,7 @@ public function max(string $max): static public function step(int|float|string $step): static { $this->step = $step; - $this->getAttributes()->set('step', $this->step); + $this->getAttributes()->set('step', (string) $this->step); return $this; } diff --git a/tests/Unit/Fields/DateRangeFieldTest.php b/tests/Unit/Fields/DateRangeFieldTest.php index ca4faa108..9af960ea8 100644 --- a/tests/Unit/Fields/DateRangeFieldTest.php +++ b/tests/Unit/Fields/DateRangeFieldTest.php @@ -221,4 +221,30 @@ $field->toField => $to . 'T00:00', ]); }); + + it('step', function (): void { + $from = now(); + $to = now()->addMonth(); + + $this->field + ->fill(['start' => $from, 'end' => $to]) + ->step(5); + + expect($this->field->getAttributes()->get('step')) + ->toBe('5'); + + $this->field + ->fill(['start' => $from, 'end' => $to]) + ->step(5.5); + + expect($this->field->getAttributes()->get('step')) + ->toBe('5.5'); + + $this->field + ->fill(['start' => $from, 'end' => $to]) + ->step('5'); + + expect($this->field->getAttributes()->get('step')) + ->toBe('5'); + }); }); From 1f8d3da906c671d6b5e77d8c91fecc87c2746386 Mon Sep 17 00:00:00 2001 From: levchenko-ivan Date: Mon, 7 Oct 2024 11:54:34 +0300 Subject: [PATCH 2/2] fix(range): fix from/to attributes --- src/UI/src/Traits/Fields/RangeTrait.php | 18 +++++----- tests/Unit/Fields/DateRangeFieldTest.php | 43 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/UI/src/Traits/Fields/RangeTrait.php b/src/UI/src/Traits/Fields/RangeTrait.php index 6d5b58beb..c0ab18325 100644 --- a/src/UI/src/Traits/Fields/RangeTrait.php +++ b/src/UI/src/Traits/Fields/RangeTrait.php @@ -23,11 +23,12 @@ trait RangeTrait public function fromAttributes(array $attributes): static { - $this->fromAttributes = $this->fromAttributes - ? $this->fromAttributes->merge($attributes) - : $this->getAttributes() + $this->fromAttributes = $this->getAttributes() ->except(array_keys($attributes)) ->merge($attributes) + ->when($this->fromAttributes, + fn(ComponentAttributesBagContract $attributes) => $attributes->merge($this->fromAttributes->all()) + ) ; return $this; @@ -56,11 +57,12 @@ public function getFromAttributes(): ComponentAttributesBagContract public function toAttributes(array $attributes): static { - $this->toAttributes = $this->toAttributes - ? $this->toAttributes->merge($attributes) - : $this->getAttributes() - ->except(array_keys($attributes)) - ->merge($attributes) + $this->toAttributes = $this->getAttributes() + ->except(array_keys($attributes)) + ->merge($attributes) + ->when($this->toAttributes, + fn(ComponentAttributesBagContract $attributes) => $attributes->merge($this->toAttributes->all()) + ) ; return $this; diff --git a/tests/Unit/Fields/DateRangeFieldTest.php b/tests/Unit/Fields/DateRangeFieldTest.php index 9af960ea8..107a55f54 100644 --- a/tests/Unit/Fields/DateRangeFieldTest.php +++ b/tests/Unit/Fields/DateRangeFieldTest.php @@ -247,4 +247,47 @@ expect($this->field->getAttributes()->get('step')) ->toBe('5'); }); + + it('attribute order', function (): void { + $from = now(); + $to = now()->addMonth(); + + $fieldOne = clone $this->field; + + $fieldOne + ->fill(['start' => $from, 'end' => $to]) + ->fromAttributes(['class'=> 'bg-lime-500']) + ->toAttributes(['class'=> 'bg-lime-500']) + ->step(10); + + // because prepareBeforeRender fill and merge from/to attributes + $fieldOne->render(); + + $fromAttributes = $fieldOne->getFromAttributes(); + $toAttributes = $fieldOne->getToAttributes(); + + expect($fromAttributes->get('step')) + ->toBe('10') + ->and($toAttributes->get('step')) + ->toBe('10'); + + $fieldTwo = clone $this->field; + + $fieldTwo + ->fill(['start' => $from, 'end' => $to]) + ->step('11') + ->fromAttributes(['class'=> 'bg-lime-500']) + ->toAttributes(['class'=> 'bg-lime-500']) + ; + + $fieldTwo->render(); + + $fromAttributes = $fieldTwo->getFromAttributes(); + $toAttributes = $fieldTwo->getToAttributes(); + + expect($fromAttributes->get('step')) + ->toBe('11') + ->and($toAttributes->get('step')) + ->toBe('11'); + }); });