Skip to content

Commit

Permalink
1.5.0 (#45)
Browse files Browse the repository at this point in the history
* Deprecated the `->icon()` method.
* Added the `->prepend()` method to the table columns (which will replace the previous `->icon()` one) to prepend HTML to a column value.
* Added the `->append()` method to the table columns to append HTML to a column value.
  • Loading branch information
Okipa authored Apr 26, 2020
1 parent bb6a13e commit 826d3df
Show file tree
Hide file tree
Showing 12 changed files with 225 additions and 55 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [1.5.0](https://github.com/Okipa/laravel-table/compare/1.4.0...1.5.0)

2020-04-26

* Deprecated the `->icon()` method.
* Added the `->prepend()` method to the table columns (which will replace the previous `->icon()` one) to prepend HTML to a column value.
* Added the `->append()` method to the table columns to append HTML to a column value.

## [1.4.0](https://github.com/Okipa/laravel-table/compare/1.3.0...1.4.0)

2020-04-26
Expand Down
35 changes: 27 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ Then, display it in the view:
* [->dateTimeFormat()](#column-dateTimeFormat)
* [->button()](#column-button)
* [->link()](#column-link)
* [->icon()](#column-icon)
* [->prepend()](#column-prepend)
* [->append()](#column-append)
* [->stringLimit()](#column-stringLimit)
* [->value()](#-value)
* [->html()](#-html)
Expand Down Expand Up @@ -826,20 +827,36 @@ $table->column()->link(function(News $news) {
});
```

<h3 id="column-icon">->icon()</h3>
<h3 id="column-prepend">->prepend()</h3>

> Add an icon before the displayed value.
> Set the second param as true if you want the icon to be displayed even if the column has no value.
> Prepend HTML to the displayed value.
> Set the second param as true if you want the prepended HTML to be displayed even if the column has no value.
**Note:**

* Signature: `icon(string $icon, bool $displaydisplayIconWhenNoValue = false): \Okipa\LaravelTable\Column`
* Signature: `prepend(string $prependicon, bool $displayPrependEvenIfNoValue = false): \Okipa\LaravelTable\Column`
* Optional

**Use case example:**

```php
$table->column('email')->icon('<i class="fas fa-envelope"></i>', true);
$table->column('email')->prepend('<i class="fas fa-envelope"></i>', true);
```

<h3 id="column-append">->append()</h3>

> Append HTML to the displayed value.
> Set the second param as true if you want the appended HTML to be displayed even if the column has no value.
**Note:**

* Signature: `append(string $prependicon, bool $displayAppendEvenIfNoValue = false): \Okipa\LaravelTable\Column`
* Optional

**Use case example:**

```php
$table->column('email')->append('<i class="fas fa-envelope"></i>', true);
```

<h3 id="column-stringLimit">->stringLimit()</h3>
Expand Down Expand Up @@ -952,7 +969,8 @@ $table->result()->classes(['bg-dark', 'text-white', 'font-weight-bold']);
* **Columns displaying combination:** The following table column methods can be combined to display a result as wished. If you can't get the wanted result, you should use the `->html()` method to build a custom display.
* `->button()`
* `->link()`
* `->icon()`
* `->prepend()`
* `->append()`
* `->stringLimit()`
* `->value()`

Expand Down Expand Up @@ -1023,7 +1041,8 @@ $table->column('content')->stringLimit(30);
$table->column('author')->sortable(true)->searchable('user', ['name']);
$table->column('category_id')
->title('Category custom name')
->icon('your-icon')
->prepend('your-icon')
->append('your-other-icon')
->button(['btn', 'btn-sm', 'btn-outline-primary'])
->value(function (News $news, Column $column) {
return config('news.category.' . $news->{$column->databaseDefaultColumn});
Expand Down
57 changes: 49 additions & 8 deletions src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ class Column
/** @property \Closure $htmlClosure */
public $htmlClosure;

/** @property string $icon */
public $icon;
/** @property string $prepend */
public $prepend;

/** @property bool $displayIconWhenNoValue */
public $displayIconWhenNoValue;
/** @property string $append */
public $append;

/** @property bool $displayPrependEvenIfNoValue */
public $displayPrependEvenIfNoValue;

/** @property bool $displayAppendEvenIfNoValue */
public $displayAppendEvenIfNoValue;

/** @property array $classes */
public $classes;
Expand Down Expand Up @@ -200,14 +206,49 @@ public function stringLimit(int $stringLimit): Column
* Set the second param as true if you want the icon to be displayed even if the column has no value.
*
* @param string $icon
* @param bool $displayIconWhenNoValue
* @param bool $displayPrependEvenIfNoValue
*
* @return \Okipa\LaravelTable\Column
* @deprecated ('Use the ->prepend() method instead.')
*/
public function icon(string $icon, bool $displayPrependEvenIfNoValue = false): Column
{
$this->prepend = $icon;
$this->displayPrependEvenIfNoValue = $displayPrependEvenIfNoValue;

return $this;
}

/**
* Prepend HTML to the displayed value.
* Set the second param as true if you want the prepended HTML to be displayed even if the column has no value.
*
* @param string $prepend
* @param bool $displayPrependEvenIfNoValue
*
* @return \Okipa\LaravelTable\Column
*/
public function prepend(string $prepend, bool $displayPrependEvenIfNoValue = false): Column
{
$this->prepend = $prepend;
$this->displayPrependEvenIfNoValue = $displayPrependEvenIfNoValue;

return $this;
}

/**
* Append HTML to the displayed value.
* Set the second param as true if you want the appended HTML to be displayed even if the column has no value.
*
* @param string $append
* @param bool $displayAppendEvenIfNoValue
*
* @return \Okipa\LaravelTable\Column
*/
public function icon(string $icon, bool $displayIconWhenNoValue = false): Column
public function append(string $append, bool $displayAppendEvenIfNoValue = false): Column
{
$this->icon = $icon;
$this->displayIconWhenNoValue = $displayIconWhenNoValue;
$this->append = $append;
$this->displayAppendEvenIfNoValue = $displayAppendEvenIfNoValue;

return $this;
}
Expand Down
8 changes: 2 additions & 6 deletions tests/Fakers/CompaniesFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

trait CompaniesFaker
{
public $data;

public function createMultipleCompanies(int $count)
{
for ($ii = 0; $ii < $count; $ii++) {
Expand All @@ -27,11 +25,9 @@ public function createUniqueCompany()

public function generateFakeCompanyData()
{
$max = User::all()->count();

return [
'name' => $this->faker->company,
'owner_id' => rand(1, $max),
'name' => $this->faker->unique()->company,
'owner_id' => rand(1, User::all()->count()),
'turnover' => rand(1000, 99999),
];
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Fakers/UsersFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

trait UsersFaker
{
/** @var string $clearPassword */
public $clearPassword;
public $data;

public function createMultipleUsers(int $count)
{
Expand All @@ -31,8 +31,8 @@ public function generateFakeUserData()
$this->clearPassword = $this->faker->word;

return [
'name' => $this->faker->unique()->name(),
'email' => $this->faker->unique()->email,
'name' => str_replace(['\''], '', $this->faker->unique()->name()),
'email' => $this->faker->unique()->email(),
'password' => Hash::make($this->clearPassword),
];
}
Expand Down
6 changes: 4 additions & 2 deletions tests/LaravelTableTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

abstract class LaravelTableTestCase extends TestCase
{
/** @var \Faker\Factory $faker */
protected $faker;

use RoutesFaker;
use UsersFaker;
use CompaniesFaker;
Expand All @@ -28,9 +30,9 @@ protected function getEnvironmentSetUp($app)
// Setup default database to use sqlite :memory:
$app['config']->set('database.default', 'testing');
$app['config']->set('database.connections.testing', [
'driver' => 'sqlite',
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
'prefix' => '',
]);
}

Expand Down
34 changes: 17 additions & 17 deletions tests/Unit/IconTest.php → tests/Unit/AppendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,67 @@
use Okipa\LaravelTable\Test\Models\User;
use Okipa\LaravelTable\Test\LaravelTableTestCase;

class IconTest extends LaravelTableTestCase
class AppendTest extends LaravelTableTestCase
{
public function testSetIconAttribute()
public function testSetAppendAttribute()
{
$table = (new Table)->model(User::class);
$table->column('name')->icon('icon');
$this->assertEquals('icon', $table->columns->first()->icon);
$this->assertEquals(false, $table->columns->first()->displayIconWhenNoValue);
$table->column('name')->append('icon');
$this->assertEquals('icon', $table->columns->first()->append);
$this->assertEquals(false, $table->columns->first()->displayAppendEvenIfNoValue);
}

public function testSetIconAttributeAndSetShowWithNoValue()
public function testSetAppendAttributeAndSetShowWithNoValue()
{
$table = (new Table)->model(User::class);
$table->column('name')->icon('icon', true);
$this->assertEquals('icon', $table->columns->first()->icon);
$this->assertEquals(true, $table->columns->first()->displayIconWhenNoValue);
$table->column('name')->append('icon', true);
$this->assertEquals('icon', $table->columns->first()->append);
$this->assertEquals(true, $table->columns->first()->displayAppendEvenIfNoValue);
}

public function testSetIconHtml()
public function testSetAppendHtml()
{
$this->createMultipleUsers(1);
$this->routes(['users'], ['index']);
$table = (new Table)->model(User::class)->routes(['index' => ['name' => 'users.index']]);
$table->column('name')->icon('icon');
$table->column('name')->append('icon');
$table->render();
$html = view('laravel-table::' . $table->tbodyTemplatePath, compact('table'))->render();
$this->assertStringContainsString('icon', $html);
}

public function testSetIconWithCustomValueHtml()
public function testSetAppendWithCustomValueHtml()
{
$this->createMultipleUsers(1);
$this->routes(['users'], ['index']);
$table = (new Table)->model(User::class)->routes(['index' => ['name' => 'users.index']]);
$table->column('name')->icon('icon')->value(function () {
$table->column('name')->append('icon')->value(function () {
return 'test';
});
$table->render();
$html = view('laravel-table::' . $table->tbodyTemplatePath, compact('table'))->render();
$this->assertStringContainsString('icon', $html);
}

public function testSetIconWithNoValueHtml()
public function testSetAppendWithNoValueHtml()
{
$user = $this->createUniqueUser();
$user->update(['name' => null]);
$this->routes(['users'], ['index']);
$table = (new Table)->model(User::class)->routes(['index' => ['name' => 'users.index']]);
$table->column('name')->icon('icon');
$table->column('name')->append('icon');
$table->render();
$html = view('laravel-table::' . $table->tbodyTemplatePath, compact('table'))->render();
$this->assertStringNotContainsString('icon', $html);
}

public function testSetIconWithNoValueButShowAnywayValueHtml()
public function testSetAppendWithNoValueButShowAnywayValueHtml()
{
$user = $this->createUniqueUser();
$user->update(['name' => null]);
$this->routes(['users'], ['index']);
$table = (new Table)->model(User::class)->routes(['index' => ['name' => 'users.index']]);
$table->column('name')->icon('icon', true);
$table->column('name')->append('icon', true);
$table->render();
$html = view('laravel-table::' . $table->tbodyTemplatePath, compact('table'))->render();
$this->assertStringContainsString('icon', $html);
Expand Down
17 changes: 15 additions & 2 deletions tests/Unit/ButtonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,26 @@ public function testIsButtonWithNoValueHtml()
$this->assertStringNotContainsString('</button>', $html);
}

public function testIsButtonWithNoValueWithIconHtml()
public function testIsButtonWithNoValueWithPrependedHtml()
{
$user = $this->createUniqueUser();
$user->update(['name' => null]);
$this->routes(['users'], ['index']);
$table = (new Table)->model(User::class)->routes(['index' => ['name' => 'users.index']]);
$table->column('name')->button(['btn', 'btn-primary'])->icon('icon', true);
$table->column('name')->button(['btn', 'btn-primary'])->prepend('icon', true);
$table->render();
$html = view('laravel-table::' . $table->tbodyTemplatePath, compact('table'))->render();
$this->assertStringContainsString('<button class="btn btn-primary', $html);
$this->assertStringContainsString('</button>', $html);
}

public function testIsButtonWithNoValueWithAppendedHtml()
{
$user = $this->createUniqueUser();
$user->update(['name' => null]);
$this->routes(['users'], ['index']);
$table = (new Table)->model(User::class)->routes(['index' => ['name' => 'users.index']]);
$table->column('name')->button(['btn', 'btn-primary'])->append('icon', true);
$table->render();
$html = view('laravel-table::' . $table->tbodyTemplatePath, compact('table'))->render();
$this->assertStringContainsString('<button class="btn btn-primary', $html);
Expand Down
16 changes: 14 additions & 2 deletions tests/Unit/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,25 @@ public function testIsLinkWithNoValueHtml()
$this->assertStringNotContainsString('<a href="', $html);
}

public function testIsLinkWithNoValueWithIconHtml()
public function testIsLinkWithNoValueWithPrependedHtml()
{
$user = $this->createUniqueUser();
$user->update(['name' => null]);
$this->routes(['users'], ['index']);
$table = (new Table)->model(User::class)->routes(['index' => ['name' => 'users.index']]);
$table->column('name')->link()->icon('icon', true);
$table->column('name')->link()->prepend('icon', true);
$table->render();
$html = view('laravel-table::' . $table->tbodyTemplatePath, compact('table'))->render();
$this->assertStringNotContainsString('<a href="', $html);
}

public function testIsLinkWithNoValueWithAppendedHtml()
{
$user = $this->createUniqueUser();
$user->update(['name' => null]);
$this->routes(['users'], ['index']);
$table = (new Table)->model(User::class)->routes(['index' => ['name' => 'users.index']]);
$table->column('name')->link()->append('icon', true);
$table->render();
$html = view('laravel-table::' . $table->tbodyTemplatePath, compact('table'))->render();
$this->assertStringNotContainsString('<a href="', $html);
Expand Down
Loading

0 comments on commit 826d3df

Please sign in to comment.