Skip to content

Commit 93d10ee

Browse files
authored
DateRange add methods format/serverFormat (#2975)
Adding methods format/serverFormat for DateRange
1 parent 96de1d5 commit 93d10ee

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

src/Screen/Fields/DateRange.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Orchid\Screen\Fields;
66

7+
use Illuminate\Support\Carbon;
78
use Orchid\Screen\Concerns\ComplexFieldConcern;
89
use Orchid\Screen\Field;
910

@@ -24,6 +25,15 @@ class DateRange extends Field implements ComplexFieldConcern
2425
*/
2526
protected $view = 'platform::fields.dataRange';
2627

28+
/**
29+
* Default attributes value.
30+
*
31+
* @var array
32+
*/
33+
protected $attributes = [
34+
'data-datetime-date-format' => 'Y-m-d H:i:S',
35+
];
36+
2737
/**
2838
* Attributes available for a particular tag.
2939
*
@@ -32,6 +42,7 @@ class DateRange extends Field implements ComplexFieldConcern
3242
protected $inlineAttributes = [
3343
'form',
3444
'name',
45+
'data-datetime-date-format',
3546
];
3647

3748
/**
@@ -47,4 +58,49 @@ public function disableMobile(bool $disable = true): static
4758

4859
return $this;
4960
}
61+
62+
/**
63+
* A string of characters which are used
64+
* to define how the date will be displayed in the input box.
65+
*/
66+
public function format(string $format): self
67+
{
68+
$this->set('data-datetime-date-format', $format);
69+
70+
return $this;
71+
}
72+
73+
/**
74+
* Sets format for transmission to the front values
75+
* If the argument is not passed, then the value specified
76+
* in the 'format' method will be taken
77+
*
78+
*
79+
* @return $this
80+
*/
81+
public function serverFormat(?string $format = null): self
82+
{
83+
return $this->addBeforeRender(function () use ($format) {
84+
$values = $this->get('value');
85+
86+
if ($values === null || (is_array($values) && count($values) === 0)) {
87+
return;
88+
}
89+
90+
if ($format === null) {
91+
$format = $this->get('data-datetime-date-format');
92+
}
93+
94+
foreach ($values as $key => $value) {
95+
96+
if ($value === null) {
97+
continue;
98+
}
99+
100+
$values[$key] = Carbon::parse($value)->format($format);
101+
}
102+
103+
$this->set('value', $values);
104+
});
105+
}
50106
}

0 commit comments

Comments
 (0)