Skip to content

Commit

Permalink
Doc Type Fixes (#1891)
Browse files Browse the repository at this point in the history
* Update NumberFilter and TypeHints

* Update DateFilter/DateTimeFilter and ConfigurableAreasHelpers for typehints and array key exists

* Add Float Test for NumberFilter

* Add ColorColumn View Test

* Adjust ColumnSelectConfigurationTest

---------

Co-authored-by: lrljoe <[email protected]>
  • Loading branch information
lrljoe and lrljoe authored Aug 25, 2024
1 parent b927a98 commit d0da3b1
Show file tree
Hide file tree
Showing 19 changed files with 77 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/Traits/Configuration/FooterConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ public function setUseHeaderAsFooterDisabled(): self
return $this;
}

public function setFooterTrAttributes(callable $callback): self
public function setFooterTrAttributes(\Closure $callback): self
{
$this->footerTrAttributesCallback = $callback;

return $this;
}

public function setFooterTdAttributes(callable $callback): self
public function setFooterTdAttributes(\Closure $callback): self
{
$this->footerTdAttributesCallback = $callback;

Expand Down
4 changes: 2 additions & 2 deletions src/Traits/Configuration/SecondaryHeaderConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public function setSecondaryHeaderDisabled(): self
return $this;
}

public function setSecondaryHeaderTrAttributes(callable $callback): self
public function setSecondaryHeaderTrAttributes(\Closure $callback): self
{
$this->secondaryHeaderTrAttributesCallback = $callback;

return $this;
}

public function setSecondaryHeaderTdAttributes(callable $callback): self
public function setSecondaryHeaderTdAttributes(\Closure $callback): self
{
$this->secondaryHeaderTdAttributesCallback = $callback;

Expand Down
12 changes: 6 additions & 6 deletions src/Traits/Configuration/TableAttributeConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function setTbodyAttributes(array $attributes = []): self
/**
* Set a list of attributes to override on the th elements
*/
public function setThAttributes(callable $callback): self
public function setThAttributes(\Closure $callback): self
{
$this->thAttributesCallback = $callback;

Expand All @@ -78,7 +78,7 @@ public function setThAttributes(callable $callback): self
/**
* Set a list of attributes to override on the th sort button elements
*/
public function setThSortButtonAttributes(callable $callback): self
public function setThSortButtonAttributes(\Closure $callback): self
{
$this->thSortButtonAttributesCallback = $callback;

Expand All @@ -88,7 +88,7 @@ public function setThSortButtonAttributes(callable $callback): self
/**
* Set a list of attributes to override on the td elements
*/
public function setTrAttributes(callable $callback): self
public function setTrAttributes(\Closure $callback): self
{
$this->trAttributesCallback = $callback;

Expand All @@ -98,21 +98,21 @@ public function setTrAttributes(callable $callback): self
/**
* Set a list of attributes to override on the td elements
*/
public function setTdAttributes(callable $callback): self
public function setTdAttributes(\Closure $callback): self
{
$this->tdAttributesCallback = $callback;

return $this;
}

public function setTableRowUrl(callable $callback): self
public function setTableRowUrl(\Closure $callback): self
{
$this->trUrlCallback = $callback;

return $this;
}

public function setTableRowUrlTarget(callable $callback): self
public function setTableRowUrlTarget(\Closure $callback): self
{
$this->trUrlTargetCallback = $callback;

Expand Down
15 changes: 4 additions & 11 deletions src/Traits/Helpers/ConfigurableAreasHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ public function hasConfigurableAreaFor(string $area): bool
return isset($this->configurableAreas[$area]) && $this->getConfigurableAreaFor($area) !== null;
}

/**
* @param string|array<mixed> $area
*/
public function getConfigurableAreaFor($area): ?string
public function getConfigurableAreaFor(string $area): ?string
{
$area = $this->configurableAreas[$area] ?? null;
$area = array_key_exists($area, $this->configurableAreas) ? $this->configurableAreas[$area] : null;

if (is_array($area)) {
return $area[0];
Expand All @@ -35,13 +32,9 @@ public function getConfigurableAreaFor($area): ?string
return $area;
}

/**
* @param string|array<mixed> $area
* @return array<mixed>
*/
public function getParametersForConfigurableArea($area): array
public function getParametersForConfigurableArea(string $area): array
{
$area = $this->configurableAreas[$area] ?? null;
$area = array_key_exists($area, $this->configurableAreas) ? $this->configurableAreas[$area] : null;

if (is_array($area) && isset($area[1]) && is_array($area[1])) {
return $area[1];
Expand Down
6 changes: 5 additions & 1 deletion src/Traits/Helpers/SearchHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ public function getSearchOptions(): string

public function getSearchPlaceholder(): string
{
return $this->hasSearchPlaceholder() ? $this->searchPlaceholder : __('Search');
if ($this->hasSearchPlaceholder()) {
return $this->searchPlaceholder;
}

return __('Search');
}

public function hasSearchPlaceholder(): bool
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/WithFooter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ trait WithFooter

protected bool $columnsWithFooter = false;

protected ?object $footerTrAttributesCallback;
protected ?\Closure $footerTrAttributesCallback;

protected ?object $footerTdAttributesCallback;
protected ?\Closure $footerTdAttributesCallback;

public function setupFooter(): void
{
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/WithSecondaryHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ trait WithSecondaryHeader

protected bool $columnsWithSecondaryHeader = false;

protected ?object $secondaryHeaderTrAttributesCallback;
protected ?\Closure $secondaryHeaderTrAttributesCallback;

protected ?object $secondaryHeaderTdAttributesCallback;
protected ?\Closure $secondaryHeaderTdAttributesCallback;

public function bootedWithSecondaryHeader(): void
{
Expand Down
12 changes: 6 additions & 6 deletions src/Traits/WithTableAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ trait WithTableAttributes

protected array $tbodyAttributes = [];

protected ?object $thAttributesCallback;
protected ?\Closure $thAttributesCallback;

protected ?object $thSortButtonAttributesCallback;
protected ?\Closure $thSortButtonAttributesCallback;

protected ?object $trAttributesCallback;
protected ?\Closure $trAttributesCallback;

protected ?object $tdAttributesCallback;
protected ?\Closure $tdAttributesCallback;

protected ?object $trUrlCallback;
protected ?\Closure $trUrlCallback;

protected ?object $trUrlTargetCallback;
protected ?\Closure $trUrlTargetCallback;
}
2 changes: 1 addition & 1 deletion src/Views/Columns/ColorColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ColorColumn extends Column
ColorColumnHelpers;
use HasDefaultStringValue;

public ?object $colorCallback = null;
public ?\Closure $colorCallback;

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

Expand Down
12 changes: 9 additions & 3 deletions src/Views/Filters/DateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@ public function validate(string $value): string|bool
{
$this->setInputDateFormat('Y-m-d')->setOutputDateFormat($this->getConfig('pillFormat') ?? 'Y-m-d');
$carbonDate = $this->createCarbonDate($value);
if ($carbonDate instanceof \Carbon\Carbon) {
return $carbonDate->format('Y-m-d');
}

return ($carbonDate === false) ? false : $carbonDate->format('Y-m-d');
return false;
}

public function getFilterPillValue($value): array|string|bool|null
public function getFilterPillValue($value): ?string
{
if ($this->validate($value)) {
return $this->outputTranslatedDate($this->createCarbonDate($value));
$carbonDate = $this->createCarbonDate($value);
if ($carbonDate instanceof \Carbon\Carbon) {
return $this->outputTranslatedDate($carbonDate);
}
}

return null;
Expand Down
12 changes: 9 additions & 3 deletions src/Views/Filters/DateTimeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,20 @@ public function validate(string $value): string|bool
$this->setInputDateFormat('Y-m-d\TH:i')->setOutputDateFormat($this->getConfig('pillFormat'));

$carbonDate = $this->createCarbonDate($value);
if ($carbonDate instanceof \Carbon\Carbon) {
return $carbonDate->format('Y-m-d\TH:i');
}

return ($carbonDate === false) ? false : $carbonDate->format('Y-m-d\TH:i');
return false;
}

public function getFilterPillValue($value): array|string|bool|null
public function getFilterPillValue($value): ?string
{
if ($this->validate($value)) {
return $this->outputTranslatedDate($this->createCarbonDate($value));
$carbonDate = $this->createCarbonDate($value);
if ($carbonDate && $carbonDate instanceof \Carbon\Carbon) {
return $this->outputTranslatedDate($carbonDate);
}
}

return null;
Expand Down
12 changes: 10 additions & 2 deletions src/Views/Filters/NumberFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ class NumberFilter extends Filter

protected string $view = 'livewire-tables::components.tools.filters.number';

public function validate(mixed $value): float|int|bool
public function validate(float|int|string|array $value): float|int|string|false
{
return is_numeric($value) ? $value : false;
if (is_array($value)) {
return false;
} elseif (is_float($value)) {
return (float) $value;
} elseif (is_int($value)) {
return (int) $value;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

trait ColorColumnConfiguration
{
public function color(callable $callback): self
public function color(\Closure $callback): self
{
$this->colorCallback = $callback;

Expand Down
2 changes: 1 addition & 1 deletion src/Views/Traits/Core/HasFooter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function footerCallbackIsFilter(): bool
return $callback instanceof Filter;
}

public function getFooterContents(mixed $rows, array $filterGenericData): string|HtmlString
public function getFooterContents(mixed $rows, array $filterGenericData): \Illuminate\Contracts\Foundation\Application|\Illuminate\View\Factory|\Illuminate\View\View|string|HtmlString
{
$value = null;
$callback = $this->getFooterCallback();
Expand Down
2 changes: 1 addition & 1 deletion src/Views/Traits/Core/HasSecondaryHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function secondaryHeaderCallbackIsFilter(): bool
return $callback instanceof Filter;
}

public function getSecondaryHeaderContents(mixed $rows, array $filterGenericData): string|HtmlString
public function getSecondaryHeaderContents(mixed $rows, array $filterGenericData): \Illuminate\Contracts\Foundation\Application|\Illuminate\View\Factory|\Illuminate\View\View|string|HtmlString
{
$value = null;
$callback = $this->getSecondaryHeaderCallback();
Expand Down
2 changes: 1 addition & 1 deletion src/Views/Traits/Helpers/ColorColumnHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait ColorColumnHelpers
// TODO: Test
public function getColor(Model|int $row): string
{
return $this->hasColorCallback() ? app()->call($this->getColorCallback(), ['row' => $row]) : ($this->getValue($row));
return $this->hasColorCallback() ? app()->call($this->getColorCallback(), ['row' => $row, 'value' => $row->{$this->getFrom()} ?? '']) : ($this->getValue($row));
}

public function getColorCallback(): ?callable
Expand Down
4 changes: 3 additions & 1 deletion tests/Traits/Configuration/ColumnSelectConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function test_can_check_all_columns_get_selected(): void

}

public function test_check_get_selected_columns()
public function test_check_get_selected_columns(): void
{

$this->basicTable->deselectAllColumns();
Expand All @@ -116,6 +116,8 @@ public function test_can_check_all_columns_get_selected_and_extra_methods_work()
{
$this->assertTrue($this->basicTable->allSelectedColumnsAreVisibleByDefault());

$this->assertSame(8, count($this->basicTable->getDefaultVisibleColumns()));

$this->assertTrue($this->basicTable->allVisibleColumnsAreSelected());

$this->basicTable->deselectAllColumns();
Expand Down
10 changes: 10 additions & 0 deletions tests/Views/Columns/ColorColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public function test_can_get_the_column_view(): void
$column = ColorColumn::make('Favorite Color', 'favorite_color');

$this->assertSame('livewire-tables::includes.columns.color', $column->getView());
$column->setView('test-color-column');
$this->assertSame('test-color-column', $column->getView());

}

public function test_can_infer_field_name_from_title_if_no_from(): void
Expand All @@ -35,6 +38,13 @@ public function test_can_set_base_field_from_from(): void
$this->assertSame('favorite_color', $column->getField());
}

public function test_can_set_view(): void
{
$column = ColorColumn::make('Favorite Color', 'favorite_color');

$this->assertSame('livewire-tables::includes.columns.color', $column->getView());
}

public function test_can_set_default_value(): void
{
$column = ColorColumn::make('Favorite Color', 'favorite_color')->defaultValue('#FEFEFE');
Expand Down
3 changes: 2 additions & 1 deletion tests/Views/Filters/NumberFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ public function test_can_set_number_filter_to_number(): void
{
$filter = NumberFilter::make('BreedID');
$this->assertSame(123, $filter->validate(123));
$this->assertSame(123, $filter->validate('123'));
$this->assertSame(123.51, $filter->validate(123.51));
$this->assertFalse($filter->validate('123'));
}

public function test_can_get_if_number_filter_empty(): void
Expand Down

0 comments on commit d0da3b1

Please sign in to comment.