Blade -> Twig (for forms)

This commit is contained in:
Pig Fang 2019-12-03 17:03:30 +08:00
parent a9e11f7785
commit 322ef7cdeb
18 changed files with 133 additions and 89 deletions

View File

@ -108,7 +108,7 @@ class OptionForm
$hintContent = trans("options.$this->id.hint");
}
$this->hint = view('common.option-form.hint')->with('hint', $hintContent)->render();
$this->hint = view('forms.hint')->with('hint', $hintContent)->render();
return $this;
}
@ -148,13 +148,11 @@ class OptionForm
'name' => '',
], $info);
$classes = "btn btn-{$info['style']} ".implode(' ', (array) Arr::get($info, 'class'));
if ($info['href']) {
$this->buttons[] = "<a href='{$info['href']}' class='$classes'>{$info['text']}</a>";
} else {
$this->buttons[] = "<button type='{$info['type']}' name='{$info['name']}' class='$classes'>{$info['text']}</button>";
}
$info['class'] = array_merge(
['btn', 'btn-'.$info['style']],
(array) Arr::get($info, 'class')
);
$this->buttons[] = $info;
return $this;
}
@ -172,7 +170,7 @@ class OptionForm
$msg = trans("options.$this->id.message");
}
$this->messages[] = "<div class='callout callout-$style'>$msg</div>";
$this->messages[] = ['content' => $msg, 'type' => $style];
return $this;
}
@ -353,7 +351,9 @@ class OptionForm
$this->assignValues();
return view('common.option-form.main')->with(array_merge(get_object_vars($this)))->render();
return view('forms.form')
->with(array_merge(get_object_vars($this)))
->render();
}
/**
@ -411,7 +411,7 @@ class OptionFormItem
$hintContent = trans("options.$this->parentId.$this->id.hint");
}
$this->hint = view('common.option-form.hint')->with('hint', $hintContent)->render();
$this->hint = view('forms.hint')->with('hint', $hintContent)->render();
return $this;
}
@ -425,7 +425,7 @@ class OptionFormItem
public function disabled($disabled = 'disabled')
{
$this->disabled = "disabled=\"$disabled\"";
$this->disabled = $disabled;
return $this;
}
@ -460,7 +460,7 @@ class OptionFormText extends OptionFormItem
public function render()
{
return view('common.option-form.text')->with([
return view('forms.text')->with([
'id' => $this->id,
'value' => $this->value,
'disabled' => $this->disabled,
@ -486,7 +486,7 @@ class OptionFormCheckbox extends OptionFormItem
public function render()
{
return view('common.option-form.checkbox')->with([
return view('forms.checkbox')->with([
'id' => $this->id,
'value' => $this->value,
'label' => $this->label,
@ -508,7 +508,7 @@ class OptionFormTextarea extends OptionFormItem
public function render()
{
return view('common.option-form.textarea')->with([
return view('forms.textarea')->with([
'id' => $this->id,
'rows' => $this->rows,
'value' => $this->value,
@ -530,9 +530,9 @@ class OptionFormSelect extends OptionFormItem
public function render()
{
return view('common.option-form.select')->with([
return view('forms.select')->with([
'id' => $this->id,
'options' => $this->options,
'options' => (array) $this->options,
'selected' => $this->value,
'disabled' => $this->disabled,
]);
@ -575,13 +575,13 @@ class OptionFormGroup extends OptionFormItem
$item['value'] = option_localized($item['id']);
}
$rendered[] = view('common.option-form.'.$item['type'])->with([
$rendered[] = view('forms.'.$item['type'])->with([
'id' => $item['id'],
'value' => $item['value'],
'placeholder' => Arr::get($item, 'placeholder'),
]);
}
return view('common.option-form.group')->with('items', $rendered);
return view('forms.group')->with('items', $rendered);
}
}

View File

@ -1,3 +0,0 @@
<label for="{{ $id }}">
<input {!! $value ? 'checked="true"' : '' !!} type="checkbox" id="{{ $id }}" name="{{ $id }}" {{ $disabled ?? '' }} value="true"> {{ $label }}
</label>

View File

@ -1,5 +0,0 @@
<div class="input-group">
@foreach($items as $item)
{!! $item->render() !!}
@endforeach
</div>

View File

@ -1 +0,0 @@
<i class="fas fa-question-circle" title="{!! $hint !!}" data-toggle="tooltip" data-placement="top"></i>

View File

@ -1,5 +0,0 @@
{!! $item->render() !!}
@if ($item->description)
<p class="description">{!! $item->description !!}</p>
@endif

View File

@ -1,41 +0,0 @@
<div class="card card-{{ $type }}">
<div class="card-header">
<h3 class="card-title">{{ $title }} {!! $hint ?? '' !!}</h3>
</div>
<form method="post">
@csrf
<input type="hidden" name="option" value="{{ $id }}">
<div class="card-body">
@foreach($messages as $msg)
{!! $msg !!}
@endforeach
@if ($renderWithoutTable)
@each('common.option-form.item', $items, 'item')
@else
<table class="table">
<tbody>
@foreach($items as $item)
<tr>
@unless ($renderInputTagsOnly)
<td class="key">{{ $item->name }} {!! $item->hint ?? '' !!}</td>
@endunless
<td class="value">
@include('common.option-form.item', compact('item'))
</td>
</tr>
@endforeach
</tbody>
</table>
@endif
</div>
<div class="card-footer">
@foreach($buttons as $button)
{!! $button !!}
@endforeach
</div>
</form>
</div>

View File

@ -1,7 +0,0 @@
<select class="form-control" name="{{ $id }}" {{ $disabled ?? '' }}>
@foreach ((array) $options as $option)
<option {!! $selected == $option['value'] ? 'selected="selected"' : '' !!} value="{{ $option['value'] }}">{{ $option['name'] }}</option>
@endforeach
</select>

View File

@ -1 +0,0 @@
<input type="text" class="form-control" name="{{ $id }}" {{ $disabled ?? '' }} value="{{ $value }}" placeholder="{{ $placeholder ?? '' }}">

View File

@ -1 +0,0 @@
<textarea class="form-control" rows="{{ $rows }}" name="{{ $id }}" {{ $disabled ?? '' }}>{{ $value }}</textarea>

View File

@ -0,0 +1,10 @@
<label for="{{ id }}">
<input
type="checkbox"
{{ value ? 'checked="true"' : '' }}
id="{{ id }}"
name="{{ id }}"
{{ disabled ? 'disabled=disabled' : '' }}
value="true"
> {{ label }}
</label>

View File

@ -0,0 +1,62 @@
<div class="card card-{{ type }}">
<div class="card-header">
<h3 class="card-title">{{ title }} {{ hint|raw }}</h3>
</div>
<form method="post">
{{ csrf_field() }}
<input type="hidden" name="option" value="{{ id }}">
<div class="card-body">
{% for message in messages %}
<div class="callout callout-{{ message.type }}">
{{ message.content|raw }}
</div>
{% endfor %}
{% if renderWithoutTable %}
{% for item in items %}
{{ item.render()|raw }}
{% if item.description %}
<p class="description">{{ item.description|raw }}</p>
{% endif %}
{% endfor %}
{% else %}
<table class="table">
<tbody>
{% for item in items %}
<tr>
{% if not renderInputTagsOnly %}
<td class="key">
{{ item.name }} {{ item.hint|raw }}
</td>
{% endif %}
<td class="value">
{{ item.render()|raw }}
{% if item.description %}
<p class="description">{{ item.description|raw }}</p>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</div>
<div class="card-footer">
{% for button in buttons %}
{% if button.href %}
<a href="{{ button.href }}" class="{{ button.class|join(' ') }}">
{{ button.text|raw }}
</a>
{% else %}
<button
type="{{ button.type }}"
name="{{ button.name }}"
class="{{ button.class|join(' ') }}"
>
{{ button.text|raw }}
</button>
{% endif %}
{% endfor %}
</div>
</form>
</div>

View File

@ -0,0 +1,5 @@
<div class="input-group">
{% for item in items %}
{{ item.render()|raw }}
{% endfor %}
</div>

View File

@ -0,0 +1 @@
<i class="fas fa-question-circle" title="{{ hint }}" data-toggle="tooltip" data-placement="top"></i>

View File

@ -0,0 +1,10 @@
<select class="form-control" name="{{ id }}" {{ disabled ? 'disabled=disabled' : '' }}>
{% for option in options %}
<option
value="{{ option.value }}"
{{ selected == option.value ? 'selected=selected' : '' }}
>
{{ option.name }}
</option>
{% endfor %}
</select>

View File

@ -0,0 +1,8 @@
<input
type="text"
class="form-control"
name="{{ id }}"
{{ disabled ? 'disabled=disabled' : '' }}
value="{{ value }}"
placeholder="{{ placeholder }}"
>

View File

@ -0,0 +1,6 @@
<textarea
class="form-control"
rows="{{ rows }}"
name="{{ id }}"
{{ disabled ? 'disabled=disabled' : '' }}
>{{ value }}</textarea>

View File

@ -87,10 +87,10 @@ class OptionFormTest extends TestCase
$a = $crawler->filter('a');
$this->assertEquals('http://example.com', $a->attr('href'));
$this->assertEquals('btn btn-default a b', $a->attr('class'));
$this->assertEquals('link', $a->text());
$this->assertEquals('link', trim($a->text()));
$button = $crawler->filter('button.btn-primary');
$this->assertEquals('press me', $button->text());
$this->assertEquals('press me', trim($button->text()));
$this->assertEquals('btn', $button->attr('name'));
$this->assertEquals('button', $button->attr('type'));
@ -106,8 +106,11 @@ class OptionFormTest extends TestCase
$this->assertSame($form, $returned);
$crawler = new Crawler($form->render());
$this->assertEquals(trans('options.test.message'), $crawler->filter('.callout-info')->text());
$this->assertEquals('greeting', $crawler->filter('.callout-warning')->text());
$this->assertEquals(
trans('options.test.message'),
trim($crawler->filter('.callout-info')->text())
);
$this->assertEquals('greeting', trim($crawler->filter('.callout-warning')->text()));
}
public function testHookBefore()
@ -217,7 +220,7 @@ class OptionFormTest extends TestCase
$crawler = new Crawler($form->render());
$button = $crawler->filter('button');
$this->assertStringContainsString('btn-primary', $button->attr('class'));
$this->assertEquals(trans('general.submit'), $button->text());
$this->assertEquals(trans('general.submit'), trim($button->text()));
$this->assertEquals('submit', $button->attr('type'));
$this->assertEquals('submit_test', $button->attr('name'));
}
@ -235,7 +238,10 @@ class OptionFormTest extends TestCase
$form->handle();
$crawler = new Crawler($form->render());
$this->assertEquals(trans('options.option-saved'), $crawler->filter('.callout-success')->text());
$this->assertEquals(
trans('options.option-saved'),
trim($crawler->filter('.callout-success')->text())
);
$this->assertEquals('formatted value', option('t'));
}