Skip to content

Commit 3f18734

Browse files
authored
Merge pull request #1 from codebar-ag/feature-updates
Handle formatting
2 parents 5b51545 + fc64bc9 commit 3f18734

File tree

5 files changed

+53
-14
lines changed

5 files changed

+53
-14
lines changed

resources/views/forms/components/json-input.blade.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
:field="$field"
44

55
>
6-
@php
7-
ray($getStatePath())
8-
@endphp
96
<div
107
x-data="{ state: $wire.{{ $applyStateBindingModifiers("\$entangle('{$getStatePath()}')") }} }"
118
style="position: relative; border-radius: 0.375rem;"
@@ -14,7 +11,7 @@
1411
<div
1512
wire:ignore
1613
x-init="
17-
codeMirrorEditor = CodeMirror($refs.input, {
14+
{{ str_replace('.', '', $getId()) }} = CodeMirror($refs.{{ str_replace('.', '', $getId()) }}, {
1815
mode: 'application/json',
1916
lineNumbers: {{ json_encode($getHasLineNumbers()) }},
2017
lineWrapping: {{ json_encode($getHasLineWrapping()) }},
@@ -57,19 +54,25 @@
5754
}
5855
});
5956
60-
codeMirrorEditor.setSize('100%', '100%');
61-
codeMirrorEditor.setValue(state ?? '{}');
57+
{{ str_replace('.', '', $getId()) }}.setSize('100%', '100%');
58+
{{ str_replace('.', '', $getId()) }}.setValue({{ json_encode(json_encode($getState(), JSON_PRETTY_PRINT), JSON_UNESCAPED_SLASHES) }} ?? '{}');
6259
6360
setTimeout(function() {
64-
codeMirrorEditor.refresh();
61+
{{ str_replace('.', '', $getId()) }}.refresh();
6562
}, 1);
6663
67-
codeMirrorEditor.on('change', () => state = codeMirrorEditor.getValue())
64+
{{ str_replace('.', '', $getId()) }}.on('change', function () {
65+
try {
66+
state = JSON.parse({{ str_replace('.', '', $getId()) }}.getValue())
67+
} catch (e) {
68+
state = {{ str_replace('.', '', $getId()) }}.getValue();
69+
}
70+
});
6871
"
6972
>
7073
<div
7174
wire:ignore
72-
x-ref="input"
75+
x-ref="{{ str_replace('.', '', $getId()) }}"
7376
></div>
7477
</div>
7578
</div>

resources/views/infolists/components/json-entry.blade.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
<div
77
wire:ignore
88
x-init="
9-
codeMirrorEditor = CodeMirror($refs.input, {
9+
{{ str_replace('.', '', $getId()) }} = CodeMirror($refs.{{ str_replace('.', '', $getId()) }}, {
1010
mode: 'application/json',
1111
readOnly: true,
1212
lineNumbers: {{ json_encode($getHasLineNumbers()) }},
1313
lineWrapping: {{ json_encode($getHasLineWrapping()) }},
1414
autoCloseBrackets: {{ json_encode($getHasAutoCloseBrackets()) }},
15+
lineNumbers: {{ json_encode($getHasLineNumbers()) }},
1516
viewportMargin: Infinity,
1617
theme: '{{ $getHasDarkTheme() ? 'darcula' : 'default' }}',
1718
foldGutter: {{ json_encode($getHasFoldingCode()) }},
@@ -50,17 +51,17 @@
5051
}
5152
});
5253
53-
codeMirrorEditor.setSize(null, '100%');
54-
codeMirrorEditor.setValue({{ json_encode($getState()) }} ?? '{}');
54+
{{ str_replace('.', '', $getId()) }}.setSize(null, '100%');
55+
{{ str_replace('.', '', $getId()) }}.setValue({{ json_encode(json_encode($getState(), JSON_PRETTY_PRINT), JSON_UNESCAPED_SLASHES) }} ?? '{}');
5556
5657
setTimeout(function() {
57-
codeMirrorEditor.refresh();
58+
{{ str_replace('.', '', $getId()) }}.refresh();
5859
}, 1);
5960
"
6061
>
6162
<div
6263
wire:ignore
63-
x-ref="input"
64+
x-ref="{{ str_replace('.', '', $getId()) }}"
6465
></div>
6566
</div>
6667
</div>

src/Forms/Components/JsonInput.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,15 @@ class JsonInput extends Field
2020
use HasLineWrapping;
2121

2222
protected string $view = 'filament-json-field::forms.components.json-input';
23+
24+
public function setUp(): void
25+
{
26+
parent::setUp();
27+
28+
$this
29+
->rules(['array'])
30+
->validationMessages([
31+
'array' => __('The :attribute must be valid JSON.'),
32+
]);
33+
}
2334
}

tests/JsonEntryFieldTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@
3838
expect($field->getHasLineNumbers())->toBe(true);
3939
});
4040

41+
it('can have line wrapping code ', function () {
42+
$field = JsonEntry::make('json');
43+
44+
expect($field->getHasLineWrapping())->toBe(true);
45+
46+
$field->lineWrapping(false);
47+
expect($field->getHasLineWrapping())->toBe(false);
48+
49+
$field->lineWrapping(true);
50+
expect($field->getHasLineWrapping())->toBe(true);
51+
});
52+
4153
it('can have code ', function () {
4254
$field = JsonEntry::make('json');
4355

tests/JsonInputFieldTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@
3838
expect($field->getHasLineNumbers())->toBe(true);
3939
});
4040

41+
it('can have line wrapping code ', function () {
42+
$field = JsonInput::make('json');
43+
44+
expect($field->getHasLineWrapping())->toBe(true);
45+
46+
$field->lineWrapping(false);
47+
expect($field->getHasLineWrapping())->toBe(false);
48+
49+
$field->lineWrapping(true);
50+
expect($field->getHasLineWrapping())->toBe(true);
51+
});
52+
4153
it('can have code ', function () {
4254
$field = JsonInput::make('json');
4355

0 commit comments

Comments
 (0)