Skip to content

Commit 25f9e34

Browse files
authored
Merge pull request #187 from yajra/editor-datetime
feat: add datetime field options
2 parents 51a6dbf + b64cbf4 commit 25f9e34

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/Html/Editor/Fields/DateTime.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,58 @@ public function minutesAvailable(array $minutes): static
109109
{
110110
return $this->opts(['minutesAvailable' => $minutes]);
111111
}
112+
113+
/**
114+
* The format of the date string loaded from the server for the field's
115+
* value and also for sending to the server on form submission.
116+
* The formatting options are defined by Moment.js.
117+
*
118+
* @param string $format
119+
* @return $this
120+
* @see https://editor.datatables.net/reference/field/datetime#Options
121+
* @see https://momentjs.com/docs/#/displaying/format/
122+
*/
123+
public function wireFormat(string $format = 'YYYY-MM-DDTHH:mm:ss.000000Z'): static
124+
{
125+
$this->attributes['wireFormat'] = $format;
126+
127+
return $this;
128+
}
129+
130+
/**
131+
* Allow (default), or disallow, the end user to type into the date / time input element.
132+
* If disallowed, they must use the calendar picker to enter data. This can be useful
133+
* if you are using a more complex date format and wish to disallow the user from
134+
* potentially making typing mistakes, although note that it does also disallow
135+
* pasting of data.
136+
*
137+
* @param bool $state
138+
* @return $this
139+
* @see https://editor.datatables.net/reference/field/datetime#Options
140+
*/
141+
public function keyInput(bool $state = true): static
142+
{
143+
$this->attributes['keyInput'] = $state;
144+
145+
return $this;
146+
}
147+
148+
/**
149+
* The format of the date string that will be shown to the end user in the input element.
150+
* The formatting options are defined by Moment.js. If a format is used that is not
151+
* ISO8061 (i.e. YYYY-MM-DD) and Moment.js has not been included, Editor will
152+
* throw an error stating that Moment.js must be included for custom
153+
* formatting to be used.
154+
*
155+
* @param string $format
156+
* @return $this
157+
* @see https://editor.datatables.net/reference/field/datetime#Options
158+
* @see https://momentjs.com/docs/#/displaying/format/
159+
*/
160+
public function displayFormat(string $format = 'YYYY-MM-DD hh:mm a'): static
161+
{
162+
$this->attributes['displayFormat'] = $format;
163+
164+
return $this;
165+
}
112166
}

tests/FieldTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ public function it_can_create_datetime_field()
107107

108108
$field->minutesAvailable([1, 2]);
109109
$this->assertEquals([1, 2], $field->opts['minutesAvailable']);
110+
111+
$field->keyInput(false);
112+
$this->assertEquals(false, $field->getAttributes()['keyInput']);
113+
114+
$field->displayFormat('LLL');
115+
$this->assertEquals('LLL', $field->getAttributes()['displayFormat']);
116+
117+
$field->wireFormat('LLL');
118+
$this->assertEquals('LLL', $field->getAttributes()['wireFormat']);
110119
}
111120

112121
/** @test */

0 commit comments

Comments
 (0)