Skip to content

Commit b95a40b

Browse files
authored
Merge pull request #188 from yajra/editor-hiddenOn
feat: hide/show fields based on editor action
2 parents 25fe850 + ff2819f commit b95a40b

File tree

2 files changed

+81
-13
lines changed

2 files changed

+81
-13
lines changed

src/Html/Editor/Editor.php

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ class Editor extends Fluent
2424
use HasEvents;
2525
use HasAuthorizations;
2626

27-
public array $events = [];
28-
2927
const DISPLAY_LIGHTBOX = 'lightbox';
3028
const DISPLAY_ENVELOPE = 'envelope';
3129
const DISPLAY_BOOTSTRAP = 'bootstrap';
3230
const DISPLAY_FOUNDATION = 'foundation';
3331
const DISPLAY_JQUERYUI = 'jqueryui';
3432

33+
public array $events = [];
34+
3535
/**
3636
* Editor constructor.
3737
*
@@ -156,30 +156,30 @@ public function fields(array $fields): static
156156
}
157157

158158
/**
159-
* Set Editor's formOptions.
159+
* Set Editor's bubble formOptions.
160160
*
161161
* @param array $formOptions
162162
* @return $this
163-
* @see https://editor.datatables.net/reference/option/formOptions
164-
* @see https://editor.datatables.net/reference/type/form-options
163+
* @see https://editor.datatables.net/reference/option/formOptions.bubble
165164
*/
166-
public function formOptions(array $formOptions): static
165+
public function formOptionsBubble(array $formOptions): static
167166
{
168-
$this->attributes['formOptions'] = $formOptions;
169-
170-
return $this;
167+
return $this->formOptions(['bubble' => Helper::castToArray($formOptions)]);
171168
}
172169

173170
/**
174-
* Set Editor's bubble formOptions.
171+
* Set Editor's formOptions.
175172
*
176173
* @param array $formOptions
177174
* @return $this
178-
* @see https://editor.datatables.net/reference/option/formOptions.bubble
175+
* @see https://editor.datatables.net/reference/option/formOptions
176+
* @see https://editor.datatables.net/reference/type/form-options
179177
*/
180-
public function formOptionsBubble(array $formOptions): static
178+
public function formOptions(array $formOptions): static
181179
{
182-
return $this->formOptions(['bubble' => Helper::castToArray($formOptions)]);
180+
$this->attributes['formOptions'] = $formOptions;
181+
182+
return $this;
183183
}
184184

185185
/**
@@ -272,4 +272,53 @@ public function toJson($options = 0): string
272272

273273
return Helper::toJsonScript($parameters, $options);
274274
}
275+
276+
/**
277+
* Hide fields on create action.
278+
*
279+
* @param array $fields
280+
* @return $this
281+
*/
282+
public function hiddenOnCreate(array $fields): static
283+
{
284+
return $this->hiddenOn('create', $fields);
285+
}
286+
287+
/**
288+
* Hide fields on specific action.
289+
*
290+
* @param string $action
291+
* @param array $fields
292+
* @return $this
293+
*/
294+
public function hiddenOn(string $action, array $fields): static
295+
{
296+
$script = 'function(e, mode, action) {';
297+
$script .= "if (action === '$action') {";
298+
foreach ($fields as $field) {
299+
$script .= "this.hide('$field');";
300+
}
301+
$script .= '} else {';
302+
foreach ($fields as $field) {
303+
$script .= "this.show('$field');";
304+
}
305+
$script .= '}';
306+
$script .= 'return true;';
307+
$script .= '}';
308+
309+
$this->onPreOpen($script);
310+
311+
return $this;
312+
}
313+
314+
/**
315+
* Hide fields on edit action.
316+
*
317+
* @param array $fields
318+
* @return $this
319+
*/
320+
public function hiddenOnEdit(array $fields): static
321+
{
322+
return $this->hiddenOn('edit', $fields);
323+
}
275324
}

tests/EditorTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,25 @@ public function it_can_have_events()
5959
$this->assertEquals($event, $editor->events[0]);
6060
}
6161

62+
/** @test */
63+
public function it_can_show_hide_fields()
64+
{
65+
$editor = $this->getEditor();
66+
67+
$editor->hiddenOnCreate(['name']);
68+
$editor->hiddenOnEdit(['email']);
69+
70+
$this->assertCount(2, $editor->events);
71+
72+
$this->assertEquals('preOpen', $editor->events[0]['event']);
73+
$this->assertStringContainsString("action === 'create'", $editor->events[0]['script']);
74+
$this->assertStringContainsString("this.hide('name')", $editor->events[0]['script']);
75+
76+
$this->assertEquals('preOpen', $editor->events[1]['event']);
77+
$this->assertStringContainsString("action === 'edit'", $editor->events[1]['script']);
78+
$this->assertStringContainsString("this.hide('email')", $editor->events[1]['script']);
79+
}
80+
6281
/** @test */
6382
public function it_has_authorizations()
6483
{

0 commit comments

Comments
 (0)