Skip to content

Commit c352877

Browse files
committed
Updated form rendering
1 parent 73bee18 commit c352877

13 files changed

Lines changed: 164 additions & 148 deletions

File tree

config/ominity.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@
7171
'version' => env('OMINITY_FORMS_RECAPTCHA_VERSION', 'v2'),
7272
'score' => env('OMINITY_FORMS_RECAPTCHA_SCORE', 0.5), // Minimum score for reCAPTCHA v3
7373
],
74+
'fields' => [
75+
'phone' => [
76+
'country_enabled' => env('OMINITY_FORMS_PHONE_COUNTRY_ENABLED', false), // Enable custom country list
77+
'default_country' => env('OMINITY_FORMS_PHONE_DEFAULT_COUNTRY', null), // Default country for phone fields
78+
]
79+
]
7480
],
7581

7682
'routes' => [
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
@foreach($field->options as $option)
2-
@if($option == 'page_title')
1+
@foreach ($field->options as $option)
2+
@if ($option == 'page_title')
33
<input type="hidden" name="{{ $field->name }}[{{ $option }}]" value="" id="{{ $id }}-{{ $option }}">
44
<script>
55
document.getElementById('{{ $id }}-{{ $option }}').value = document.title;
66
</script>
7-
@elseif($option == 'page_url')
7+
@elseif($option == 'page_url')
88
<input type="hidden" name="{{ $field->name }}[{{ $option }}]" value="" id="{{ $id }}-{{ $option }}">
99
<script>
1010
document.getElementById('{{ $id }}-{{ $option }}').value = window.location.href;
1111
</script>
1212
@endif
13-
@endforeach
13+
@endforeach

resources/views/components/form.blade.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ class="ominity-form {{ $class ?? '' }}"
1616
{{ $above }}
1717
@endif
1818

19-
@foreach ($form->fields() as $field)
20-
<x-ominity::form-field :field="$field" />
19+
@foreach ($fieldRows as $row)
20+
<div class="form-row">
21+
@foreach ($row as $field)
22+
<x-ominity::form-field :field="$field" />
23+
@endforeach
24+
</div>
2125
@endforeach
2226

2327
@if (isset($below))

src/OminityServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
class OminityServiceProvider extends ServiceProvider
2424
{
25-
const PACKAGE_VERSION = '1.1.7';
25+
const PACKAGE_VERSION = '1.1.8';
2626

2727
/**
2828
* Boot the service provider.

src/Views/Components/Form.php

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,43 @@
44

55
use Illuminate\Support\Facades\Cache;
66
use Illuminate\View\Component;
7+
use Ominity\Api\Resources\Modules\Forms\Form as OminityForm;
78
use Ominity\Laravel\Facades\Ominity;
89

910
class Form extends Component
1011
{
11-
public int $formId;
12+
public OminityForm $form;
1213

13-
public string $class;
14-
15-
public bool $ajax;
14+
public array $rows = [];
1615

1716
/**
1817
* Create a new component instance.
1918
*
2019
* @return void
2120
*/
22-
public function __construct(int $form, string $class = '', bool $ajax = false)
23-
{
24-
$this->formId = $form;
25-
$this->class = $class;
26-
$this->ajax = $ajax;
21+
public function __construct(
22+
int|OminityForm $form,
23+
public string $class = '',
24+
public bool $ajax = false
25+
) {
26+
if ($form instanceof OminityForm) {
27+
$this->form = $form;
28+
} else {
29+
$config = config('ominity.forms.cache');
30+
if ($config['enabled']) {
31+
$this->form = Cache::store($config['store'])->remember('forms-data-'.$form.'-'.app()->getLocale(), $config['expiration'], function () use ($form) {
32+
return Ominity::api()->modules->forms->forms->get($form, [
33+
'include' => 'fields',
34+
]);
35+
});
36+
} else {
37+
$this->form = Ominity::api()->modules->forms->forms->get($form, [
38+
'include' => 'fields',
39+
]);
40+
}
41+
}
42+
43+
$this->rows = $this->buildFieldRows();
2744
}
2845

2946
/**
@@ -33,41 +50,35 @@ public function __construct(int $form, string $class = '', bool $ajax = false)
3350
*/
3451
public function render()
3552
{
36-
$config = config('ominity.forms.cache');
37-
$cacheKey = 'forms-data-'.$this->formId.'-'.app()->getLocale();
38-
39-
if ($config['enabled']) {
40-
$form = Cache::store($config['store'])->remember(
41-
$cacheKey,
42-
$config['expiration'],
43-
function () {
44-
return $this->fetchFormData();
45-
}
46-
);
47-
} else {
48-
$form = $this->fetchFormData();
49-
}
50-
51-
$class = $this->class;
52-
5353
return view('ominity::components.form', [
54-
'form' => $form,
55-
'class' => $class,
54+
'form' => $this->form,
55+
'rows' => $this->rows,
56+
'class' => $this->class,
5657
'ajax' => $this->ajax,
5758
])->render();
5859
}
5960

60-
/**
61-
* Fetch form data from the API.
62-
*
63-
* @return array
64-
*/
65-
protected function fetchFormData()
61+
protected function buildFieldRows(): array
6662
{
67-
$form = Ominity::api()->modules->forms->forms->get($this->formId, [
68-
'include' => 'fields',
69-
]);
63+
$rows = [];
64+
$currentRow = [];
65+
66+
foreach ($this->form->fields() as $field) {
67+
if (!$field->isInline) {
68+
if (!empty($currentRow)) {
69+
$rows[] = $currentRow;
70+
$currentRow = [];
71+
}
72+
$rows[] = [$field];
73+
} else {
74+
$currentRow[] = $field;
75+
}
76+
}
77+
78+
if (!empty($currentRow)) {
79+
$rows[] = $currentRow;
80+
}
7081

71-
return $form;
82+
return $rows;
7283
}
7384
}

src/Views/Components/FormField/Button.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,8 @@ public function render()
2929
{
3030
$field = $this->field;
3131

32-
$id = Str::random(10);
33-
if (isset($field->css->id)) {
34-
$id = $field->css->id;
35-
}
36-
37-
$style = '';
38-
if ($field->isInline) {
39-
$style = 'display: inline-block; ';
40-
if ($field->width) {
41-
$style .= 'width: calc('.$field->width.' - 3px);';
42-
} else {
43-
$style .= 'width: calc(50% - 3px);';
44-
}
45-
} elseif ($field->width) {
46-
$style = 'width: '.$field->width.';';
47-
} else {
48-
$style = 'width: auto;';
49-
}
32+
$id = $field->css->id ?? Str::random(10);
33+
$style = $field->width ? "width: {$field->width};" : '';
5034

5135
$recaptchaConfig = config('ominity.forms.recaptcha');
5236

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Ominity\Laravel\Views\Components\FormField;
4+
5+
use Illuminate\Support\Str;
6+
use Illuminate\View\Component;
7+
8+
class Checkbox extends Component
9+
{
10+
public $field;
11+
12+
/**
13+
* Create a new component instance.
14+
*
15+
* @param array $items
16+
* @return void
17+
*/
18+
public function __construct($field)
19+
{
20+
$this->field = $field;
21+
}
22+
23+
/**
24+
* Get the view / contents that represent the component.
25+
*
26+
* @return \Illuminate\Contracts\View\View|string
27+
*/
28+
public function render()
29+
{
30+
$field = $this->field;
31+
32+
$id = $field->css->id ?? Str::random(10);
33+
34+
$style = $field->width ? "width: {$field->width};" : '';
35+
36+
return view('ominity::components.form-field.checkbox', compact('field', 'id', 'style'));
37+
}
38+
}

src/Views/Components/FormField/Email.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,9 @@ public function render()
2929
{
3030
$field = $this->field;
3131

32-
$id = Str::random(10);
33-
if (isset($field->css->id)) {
34-
$id = $field->css->id;
35-
}
36-
37-
$style = '';
38-
if ($field->isInline) {
39-
$style = 'display: inline-block; ';
40-
if ($field->width) {
41-
$style .= 'width: calc('.$field->width.' - 3px);';
42-
} else {
43-
$style .= 'width: calc(50% - 3px);';
44-
}
45-
} elseif ($field->width) {
46-
$style = 'width: '.$field->width.';';
47-
} else {
48-
$style = 'width: 100%;';
49-
}
32+
$id = $field->css->id ?? Str::random(10);
33+
34+
$style = $field->width ? "width: {$field->width};" : '';
5035

5136
return view('ominity::components.form-field.email', compact('field', 'id', 'style'));
5237
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Ominity\Laravel\Views\Components\FormField;
4+
5+
use Illuminate\Support\Str;
6+
use Illuminate\View\Component;
7+
8+
class Multicheckbox extends Component
9+
{
10+
public $field;
11+
12+
/**
13+
* Create a new component instance.
14+
*
15+
* @param array $items
16+
* @return void
17+
*/
18+
public function __construct($field)
19+
{
20+
$this->field = $field;
21+
}
22+
23+
/**
24+
* Get the view / contents that represent the component.
25+
*
26+
* @return \Illuminate\Contracts\View\View|string
27+
*/
28+
public function render()
29+
{
30+
$field = $this->field;
31+
32+
$id = Str::random(10);
33+
if (isset($field->css->id)) {
34+
$id = $field->css->id;
35+
}
36+
37+
$id = $field->css->id ?? Str::random(10);
38+
39+
$style = $field->width ? "width: {$field->width};" : '';
40+
41+
return view('ominity::components.form-field.multicheckbox', compact('field', 'id', 'style'));
42+
}
43+
}

src/Views/Components/FormField/Phone.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,9 @@ public function render()
3434
$id = $field->css->id;
3535
}
3636

37-
$style = '';
38-
if ($field->isInline) {
39-
$style = 'display: inline-block; ';
40-
if ($field->width) {
41-
$style .= 'width: calc('.$field->width.' - 3px);';
42-
} else {
43-
$style .= 'width: calc(50% - 3px);';
44-
}
45-
} elseif ($field->width) {
46-
$style = 'width: '.$field->width.';';
47-
} else {
48-
$style = 'width: 100%;';
49-
}
37+
$id = $field->css->id ?? Str::random(10);
38+
39+
$style = $field->width ? "width: {$field->width};" : '';
5040

5141
return view('ominity::components.form-field.phone', compact('field', 'id', 'style'));
5242
}

0 commit comments

Comments
 (0)