Skip to content

Commit

Permalink
Merge pull request #1589 from LowerRockLabs/DateColumn
Browse files Browse the repository at this point in the history
Add DateColumn
  • Loading branch information
lrljoe committed Dec 9, 2023
2 parents 69a64c3 + d0c8dd8 commit dc97763
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
## UNRELEASED
### New Features
- Add capability to use as a Full Page Component
- Add DateColumn

### Tweaks
- Internal - modify GitHub workflows to improve caching, but use unique caches per workflow matrix
Expand Down
Binary file modified database/sqlite.database
Binary file not shown.
25 changes: 25 additions & 0 deletions docs/columns/other-column-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,31 @@ BooleanColumn::make('Active')
->yesNo()
```

## Date Columns

Date columns provide an easy way to display dates in a given format, without having to use repetitive format() methods or partial views.

You may pass either a DateTime object, in which you can define an "outputFormat"
```php
DateColumn::make('Updated At', 'updated_at')
->outputFormat('Y-m-d H:i:s),
```

Or you may pass a string, in which case you can define an "inputFormat" in addition to the outputFormat:
```php
DateColumn::make('Last Charged', 'last_charged_at')
->inputFormat('Y-m-d H:i:s')
->outputFormat('Y-m-d'),
```

You may also set an "emptyValue" to use when there is no value from the database:
```php
DateColumn::make('Last Charged', 'last_charged_at')
->inputFormat('Y-m-d H:i:s')
->outputFormat('Y-m-d')
->emptyValue('Not Found'),
```

## Image Columns

Image columns provide a way to display images in your table without having to use `format()` or partial views:
Expand Down
3 changes: 3 additions & 0 deletions resources/views/includes/columns/date.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="livewire-tables-dateColumn">
{{ $value }}
</div>
52 changes: 52 additions & 0 deletions src/Views/Columns/DateColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Views\Columns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\HtmlString;
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\DateColumnConfiguration;
use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\DateColumnHelpers;
use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn;

class DateColumn extends Column
{
use IsColumn;
use DateColumnConfiguration,
DateColumnHelpers;

public string $inputFormat = 'Y-m-d';

public string $outputFormat = 'Y-m-d';

public string $emptyValue = '';

protected string $view = 'livewire-tables::includes.columns.date';

public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
{

$dateTime = $this->getValue($row);
if (! ($dateTime instanceof \DateTime)) {
try {
// Check if format matches what is expected
if (! \Carbon\Carbon::hasFormatWithModifiers($dateTime, $this->getInputFormat())) {
throw new \Exception('DateColumn Received Invalid Format');
}

// Create DateTime Object
$dateTime = \DateTime::createFromFormat($this->getInputFormat(), $dateTime);
} catch (\Exception $exception) {
report($exception);

// Return Null
return $this->getEmptyValue();
}
}

// Return
return $dateTime->format($this->getOutputFormat());

}
}
36 changes: 36 additions & 0 deletions src/Views/Traits/Configuration/DateColumnConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Views\Traits\Configuration;

trait DateColumnConfiguration
{
/**
* Define the outputFormat to use for the Column
*/
public function outputFormat(string $outputFormat): self
{
$this->outputFormat = $outputFormat;

return $this;
}

/**
* Define the inputFormat to use for the Column
*/
public function inputFormat(string $inputFormat): self
{
$this->inputFormat = $inputFormat;

return $this;
}

/**
* Define the Empty Value to use for the Column
*/
public function emptyValue(string $emptyValue): self
{
$this->emptyValue = $emptyValue;

return $this;
}
}
30 changes: 30 additions & 0 deletions src/Views/Traits/Helpers/DateColumnHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Views\Traits\Helpers;

trait DateColumnHelpers
{
/**
* Retrieve the outputFormat to use for the Column
*/
public function getOutputFormat(): string
{
return $this->outputFormat ?? 'Y-m-d';
}

/**
* Retrieve the inputFormat to use for the Column
*/
public function getInputFormat(): ?string
{
return $this->inputFormat ?? null;
}

/**
* Retrieve the Empty Value to use for the Column
*/
public function getEmptyValue(): string
{
return $this->emptyValue;
}
}
10 changes: 5 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ protected function setUp(): void
]);

Pet::insert([
['id' => 1, 'name' => 'Cartman', 'age' => 22, 'species_id' => 1, 'breed_id' => 4],
['id' => 2, 'name' => 'Tux', 'age' => 8, 'species_id' => 1, 'breed_id' => 4],
['id' => 3, 'name' => 'May', 'age' => 2, 'species_id' => 2, 'breed_id' => 102],
['id' => 4, 'name' => 'Ben', 'age' => 5, 'species_id' => 3, 'breed_id' => 200],
['id' => 5, 'name' => 'Chico', 'age' => 7, 'species_id' => 3, 'breed_id' => 202],
['id' => 1, 'name' => 'Cartman', 'age' => 22, 'species_id' => 1, 'breed_id' => 4, 'last_visit' => '2023-01-04'],
['id' => 2, 'name' => 'Tux', 'age' => 8, 'species_id' => 1, 'breed_id' => 4, 'last_visit' => '2023-02-04'],
['id' => 3, 'name' => 'May', 'age' => 2, 'species_id' => 2, 'breed_id' => 102, 'last_visit' => null],
['id' => 4, 'name' => 'Ben', 'age' => 5, 'species_id' => 3, 'breed_id' => 200, 'last_visit' => '2023-04-04'],
['id' => 5, 'name' => 'Chico', 'age' => 7, 'species_id' => 3, 'breed_id' => 202, 'last_visit' => '2023-05-04'],
]);

Veterinary::insert([
Expand Down
103 changes: 103 additions & 0 deletions tests/Views/Columns/DateColumnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\Views\Columns;

use Rappasoft\LaravelLivewireTables\Tests\TestCase;
use Rappasoft\LaravelLivewireTables\Views\Columns\DateColumn;

class DateColumnTest extends TestCase
{
/** @test */
public function can_set_the_column_title(): void
{
$column = DateColumn::make('Last Visit', 'last_visit');

$this->assertSame('Last Visit', $column->getTitle());
}

/** @test */
public function can_infer_field_name_from_title_if_no_from(): void
{
$column = DateColumn::make('My Title');

$this->assertSame('my_title', $column->getField());
}

/** @test */
public function can_set_base_field_from_from(): void
{
$column = DateColumn::make('Name', 'last_visit');

$this->assertSame('last_visit', $column->getField());
}

/** @test */
public function can_set_relation_field_from_from(): void
{
$column = DateColumn::make('Name', 'last_visit');

$this->assertSame('last_visit', $column->getField());
}

/** @test */
public function can_get_column_formatted_contents(): void
{
$column = DateColumn::make('Name', 'last_visit')->inputFormat('Y-m-d')->outputFormat('Y-m-d');

$rows = $this->basicTable->getRows();

$this->assertSame($rows->last()->last_visit, $column->getContents($rows->last()));
$this->assertSame($rows->last()->last_visit, '2023-05-04');
}

/** @test */
public function can_get_column_reformatted_contents(): void
{
$column = DateColumn::make('Name', 'last_visit')->inputFormat('Y-m-d')->outputFormat('d-m-Y');

$rows = $this->basicTable->getRows();

$this->assertSame('04-05-2023', $column->getContents($rows->last()));
}

/** @test */
public function can_not_get_column_reformatted_contents_with_bad_values(): void
{
$column = DateColumn::make('Name', 'last_visit')->inputFormat('d-m-Y')->outputFormat('d-m-Y');

$firstRow = $this->basicTable->getRows()->first();

$firstRow->last_visit = '44-12-2023';

$this->assertSame('', $column->getContents($firstRow));

$firstRow->last_visit = '04-01-2023';

$this->assertSame('04-01-2023', $column->getContents($firstRow));

$this->assertSame('04-01-2023', $column->emptyValue('Unknown')->getContents($firstRow));

$firstRow->last_visit = '44-12-2023';

$this->assertSame('Unknown', $column->emptyValue('Unknown')->getContents($firstRow));

}

/** @test */
public function can_set_column_empty_value(): void
{
$column = DateColumn::make('Name', 'last_visit')->inputFormat('d-m-Y')->outputFormat('d-m-Y');
$this->assertSame('', $column->getEmptyValue());

$column->emptyValue('Not Found');
$this->assertSame('Not Found', $column->getEmptyValue());

$thirdRow = $this->basicTable->getRows()->slice(3, 1)->first();

$this->assertSame('Not Found', $column->getContents($thirdRow));

$column->emptyValue('');
$this->assertSame('', $column->getContents($thirdRow));

}
}

0 comments on commit dc97763

Please sign in to comment.