1
0
mirror of https://github.com/e107inc/e107.git synced 2025-07-27 01:40:22 +02:00

Fixes #4751 - Form tabs issue. Test added.

This commit is contained in:
Cameron
2022-04-07 13:11:56 -07:00
parent 9b5a76cd0f
commit f8b04b64d4
3 changed files with 105 additions and 64 deletions

View File

@@ -570,9 +570,13 @@ class e_form
/** /**
* Render Bootstrap Tabs * Render Bootstrap Tabs
* *
* @param $array * @param array $array
* @param $options * @param array $options = [
* @return string * 'active' => (string|int) - array key of the active tab.
* 'fade' => (bool) - use fade effect or not.
* 'class' => (string) - custom css class of the tab content container
* ]
* @return string html
* @example * @example
* $array = array( * $array = array(
* 'home' => array('caption' => 'Home', 'text' => 'some tab content' ), * 'home' => array('caption' => 'Home', 'text' => 'some tab content' ),
@@ -581,14 +585,23 @@ class e_form
*/ */
public function tabs($array, $options = array()) public function tabs($array, $options = array())
{ {
$initTab = varset($options['active'],false); $initTab = varset($options['active'], false);
$id = !empty($options['id']) ? 'id="'.$options['id'].'"' : '';
if(is_numeric($initTab))
{
$initTab = 'tab-'.$initTab;
}
$id = !empty($options['id']) ? 'id="'.$options['id'].'" ' : '';
$toggle = ($this->_bootstrap > 3) ? 'data-bs-toggle="tab"' : 'data-toggle="tab"';
$text =' $text ='
<!-- Nav tabs --> <!-- Nav tabs -->
<ul '.$id.' class="nav nav-tabs">'; <ul '.$id.'class="nav nav-tabs">';
$c = 0; $c = 0;
$act = $initTab;
foreach($array as $key=>$tab) foreach($array as $key=>$tab)
{ {
@@ -597,44 +610,43 @@ class e_form
$key = 'tab-'.$key; $key = 'tab-'.$key;
} }
if($c == 0 & $initTab == false) if($c === 0 && ($act === false))
{ {
$initTab = $key; $act = $key;
} }
$active = ($key == $act) ? ' active' : '';
$text .= '<li class="nav-item'.$active.'"><a class="nav-link'.$active.'" href="#'.$key.'" '.$toggle.'>'.$tab['caption'].'</a></li>';
$active = ($key ==$initTab) ? 'active"' : '';
$text .= '<li class="nav-item '.$active.'"><a class="nav-link '.$active.'" href="#'.$key.'" data-toggle="tab" data-bs-toggle="tab">'.$tab['caption'].'</a></li>';
$c++; $c++;
} }
$text .= '</ul>'; $text .= '</ul>';
$initTab = varset($options['active'],false);
$tabClass = varset($options['class'],null); $tabClass = varset($options['class'],null);
$fade = !empty($options['fade']) ? ' fade' : '';
$show = !empty($options['fade']) ? ($this->_bootstrap > 3 ? ' show' : ' in') : '';
$text .= ' $text .= '
<!-- Tab panes --> <!-- Tab panes -->
<div class="tab-content '.$tabClass.'">'; <div class="tab-content '.$tabClass.'">';
$c=0; $c=0;
$act = $initTab;
foreach($array as $key=>$tab) foreach($array as $key=>$tab)
{ {
if(is_numeric($key)) if(is_numeric($key))
{ {
$key = 'tab-'.$key; $key = 'tab-'.$key;
} }
if($c == 0 & $initTab == false) if($c == 0 && ($act === false))
{ {
$initTab = $key; $act = $key;
} }
$active = ($key == $initTab) ? ' active' : ''; $active = ($key == $act) ? $show.' active' : '';
$text .= '<div class="tab-pane'.$active.'" id="'.$key.'">'.$tab['text'].'</div>'; $text .= '<div class="tab-pane'.$fade.$active.'" id="'.$key.'" role="tabpanel">'.$tab['text'].'</div>';
$c++; $c++;
} }
@@ -7679,7 +7691,7 @@ var_dump($select_options);*/
$query = isset($form['query']) ? $form['query'] : e_QUERY ; $query = isset($form['query']) ? $form['query'] : e_QUERY ;
$url = (isset($form['url']) ? $this->tp->replaceConstants($form['url'], 'abs') : e_SELF).($query ? '?'.$query : ''); $url = (isset($form['url']) ? $this->tp->replaceConstants($form['url'], 'abs') : e_SELF).($query ? '?'.$query : '');
$curTab = (string) varset($_GET['tab'], '0'); $curTab = varset($_GET['tab'], false);
$text .= " $text .= "
<form method='post' action='".$url."' id='{$form['id']}-form' enctype='multipart/form-data' autocomplete='off' > <form method='post' action='".$url."' id='{$form['id']}-form' enctype='multipart/form-data' autocomplete='off' >
@@ -7702,7 +7714,7 @@ var_dump($select_options);*/
} }
$text .= $this->tabs($tabs); $text .= $this->tabs($tabs, ['active'=>$curTab]);
} }
else // No Tabs Present else // No Tabs Present
{ {

View File

@@ -289,7 +289,6 @@ class e_formTest extends \Codeception\Test\Unit
{ {
$result = $this->_frm->help('my tip'); $result = $this->_frm->help('my tip');
$this->assertSame('<i class="admin-ui-help-tip far fa-question-circle"><!-- --></i><div class="field-help" data-placement="left" style="display:none">my tip</div>', $result); $this->assertSame('<i class="admin-ui-help-tip far fa-question-circle"><!-- --></i><div class="field-help" data-placement="left" style="display:none">my tip</div>', $result);
} }
/* /*
public function testGetRequiredString() public function testGetRequiredString()
@@ -306,12 +305,41 @@ class e_formTest extends \Codeception\Test\Unit
{ {
} }
*/
public function testTabs() public function testTabs()
{ {
$array = [
0 => ['caption'=> 'Cap 0', 'text'=>'Text 0'],
1 => ['caption'=> 'Cap 1', 'text'=>'Text 1'],
];
// Test 1
$opt = ['active'=>'1', 'fade'=>true];
$result = $this->_frm->tabs($array,$opt);
$this->assertStringContainsString('<a class="nav-link active" href="#tab-1"',$result, 'Test 1 Nav Failed'); // Nav
$this->assertStringContainsString('<div class="tab-pane fade in active" id="tab-1"', $result, 'Test 1 Pane Failed'); // Pane
// Test 2.
$opt = array();
$result = $this->_frm->tabs($array,$opt);
$this->assertStringContainsString('<a class="nav-link active" href="#tab-0"',$result, 'Test 2 Nav Failed'); // Nav
$this->assertStringContainsString('<div class="tab-pane active" id="tab-0"', $result, 'Test 2 Pane Failed'); // Pane
// Test 3.
$opt = ['active'=>'slide2'];
$array = [
'slide1' => ['caption'=> 'Cap 0', 'text'=>'Text 0'],
'slide2' => ['caption'=> 'Cap 1', 'text'=>'Text 1'],
];
$result = $this->_frm->tabs($array,$opt);
$this->assertStringContainsString('<li class="nav-item active"><a class="nav-link active" href="#slide2"',$result, 'Test 3 Nav Failed'); // Nav
$this->assertStringContainsString('<div class="tab-pane active" id="slide2"', $result, 'Test 3 Pane Failed'); // Pane
} }
*/
public function testCarousel() public function testCarousel()
{ {
$slides = [ $slides = [
@@ -332,6 +360,7 @@ class e_formTest extends \Codeception\Test\Unit
$this->assertStringContainsString($expected, $result); $this->assertStringContainsString($expected, $result);
} }
/* /*
public function testUrl() public function testUrl()
{ {