New method addAlert on OptionForm

This commit is contained in:
Pig Fang 2019-12-05 23:42:51 +08:00
parent ba93788fc9
commit a70260ca41
3 changed files with 51 additions and 2 deletions

View File

@ -27,6 +27,7 @@ class OptionForm
protected $buttons = [];
protected $messages = [];
protected $alerts = [];
protected $hookBefore;
protected $hookAfter;
@ -175,6 +176,24 @@ class OptionForm
return $this;
}
/**
* Add an alert to the top of option form.
*
* @param string $msg
* @param string $style
* @return $this
*/
public function addAlert($msg = self::AUTO_DETECT, $style = 'info')
{
if ($msg == self::AUTO_DETECT) {
$msg = trans("options.$this->id.alert");
}
$this->alerts[] = ['content' => $msg, 'type' => $style];
return $this;
}
/**
* Add callback which will be executed before handling options.
*
@ -266,7 +285,7 @@ class OptionForm
call_user_func($this->hookAfter, $this);
}
$this->addMessage(trans('options.option-saved'), 'success');
$this->addAlert(trans('options.option-saved'), 'success');
}
return $this;

View File

@ -12,6 +12,21 @@
</div>
{% endfor %}
{% for alert in alerts %}
<div class="alert alert-{{ alert.type }}">
{% if alert.type == 'success' %}
<i class="fas fa-check icon"></i>
{% elseif alert.type == 'info' %}
<i class="fas fa-info icon"></i>
{% elseif alert.type == 'warning' %}
<i class="fas fa-exclamation-triangle icon"></i>
{% elseif alert.type == 'danger' %}
<i class="fas fa-times-circle icon"></i>
{% endif %}
<span>{{ alert.content }}</span>
</div>
{% endfor %}
{% if renderWithoutTable %}
{% for item in items %}
{{ item.render()|raw }}

View File

@ -113,6 +113,21 @@ class OptionFormTest extends TestCase
$this->assertEquals('greeting', trim($crawler->filter('.callout-warning')->text()));
}
public function testAddAlert()
{
$form = new OptionForm('test', 'test');
$returned = $form->addAlert();
$form->addAlert('greeting', 'warning');
$this->assertSame($form, $returned);
$crawler = new Crawler($form->render());
$this->assertEquals(
trans('options.test.alert'),
trim($crawler->filter('.alert-info')->text())
);
$this->assertEquals('greeting', trim($crawler->filter('.alert-warning')->text()));
}
public function testHookBefore()
{
$called = false;
@ -240,7 +255,7 @@ class OptionFormTest extends TestCase
$crawler = new Crawler($form->render());
$this->assertEquals(
trans('options.option-saved'),
trim($crawler->filter('.callout-success')->text())
trim($crawler->filter('.alert-success')->text())
);
$this->assertEquals('formatted value', option('t'));
}