Skip to content

Commit 5da7c6c

Browse files
committed
Add Text Alignment
1 parent 390d635 commit 5da7c6c

File tree

10 files changed

+204
-23
lines changed

10 files changed

+204
-23
lines changed

docs/columns/styling.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,26 @@ public function configure(): void
308308
```php
309309
Column::make("Name", "name")
310310
->wrapText(),
311-
```
311+
```
312+
313+
## Set Text Alignment
314+
315+
You may customise the alignment of the text within the Column td elements as follows. This will over-ride the value set by "setDefaultBodyTextAlignment" methods
316+
317+
### Left
318+
```php
319+
Column::make("Name", "name")
320+
->setTextAlignLeft(),
321+
```
322+
323+
### Center
324+
```php
325+
Column::make("Name", "name")
326+
->setTextAlignCenter(),
327+
```
328+
329+
### Right
330+
```php
331+
Column::make("Name", "name")
332+
->setTextAlignRight(),
333+
```

docs/datatable/styling.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,4 +496,21 @@ public function configure(): void
496496
}
497497
```
498498

499-
Keep in mind that you must only call methods from configure() once to avoid overriding or conflicting results.
499+
Keep in mind that you must only call methods from configure() once to avoid overriding or conflicting results.
500+
501+
502+
### Default TD Content Alignment
503+
504+
Should you wish to default the text alignment for each td element, you may use one of the following methods:
505+
506+
```php
507+
public function configure(): void
508+
{
509+
$this->setDefaultBodyTextAlignLeft(); // Sets Default To Left
510+
$this->setDefaultBodyTextAlignCenter(); // Sets Default To Center
511+
$this->setDefaultBodyTextAlignRight(); // Sets Default To Right
512+
}
513+
```
514+
When not set, this will adhere to the parent element text alignment.
515+
516+
You can customise this on a per-Column basis.

resources/views/components/table/td/td.blade.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
@aware(['isTailwind','isTailwind4','isBootstrap', 'collapsingColumnInfo', 'tableRowDetails'])
2-
@props(['colIndex', 'isHtml' => false, 'wrapText' => false, 'isClickable' => false, 'customAttributes' => ['default' => true, 'default-colors' => true, 'default-styling' => true]])
2+
@props(['colIndex', 'isHtml' => false, 'wrapText' => false, 'isClickable' => false, 'customAttributes' => ['default' => true, 'default-colors' => true, 'default-styling' => true], 'textAlign' => $this->getDefaultBodyTextAlign()])
33

44
<td {{
55
$attributes->merge($isClickable ? $tableRowDetails['tdAttribs'] : [])->merge($customAttributes)
66
->class([
77
'whitespace-wrap' => $wrapText && $isTailwind,
88
])
99
->class([
10+
'text-left' => $textAlign == 'left' && $isTailwind,
11+
'text-center' => $textAlign == 'center' && $isTailwind,
12+
'text-right' => $textAlign == 'right' && $isTailwind,
13+
1014
'whitespace-wrap' => (!$wrapText && $isHtml) && $isTailwind && ($customAttributes['default-styling'] ?? true),
1115
'whitespace-nowrap' => (!$wrapText && !$isHtml) && $isTailwind && ($customAttributes['default-styling'] ?? true),
1216
1317
'px-6 py-4 text-sm font-medium' => $isTailwind && ($customAttributes['default-styling'] ?? true),
1418
'dark:text-white' => $isTailwind && ($customAttributes['default-colors'] ?? true),
1519
16-
1720
'cursor-pointer' => $isTailwind && ($isClickable && ($tableRowDetails['url'] !== null && ($tableRowDetails['attributes']['default'] ?? true))),
1821
1922
'tw4ph whitespace-wrap' => (!$wrapText && $isHtml) && $isTailwind4 && ($customAttributes['default-styling'] ?? true),
2023
'tw4ph whitespace-nowrap' => (!$wrapText && !$isHtml) && $isTailwind4 && ($customAttributes['default-styling'] ?? true),
2124
25+
'tw4ph text-left' => $textAlign == 'left' && $isTailwind4,
26+
'tw4ph text-center' => $textAlign == 'center' && $isTailwind4,
27+
'tw4ph text-right' => $textAlign == 'right' && $isTailwind4,
28+
29+
2230
'tw4ph px-6 py-4 text-sm font-medium' => $isTailwind4 && ($customAttributes['default-styling'] ?? true),
2331
'tw4ph dark:text-white' => $isTailwind4 && ($customAttributes['default-colors'] ?? true),
2432

resources/views/components/tbody/tbody.blade.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@
88
] : [])
99
->merge($tableRowDetails['attributes'])
1010
->class([
11+
'text-left' => $this->getDefaultBodyTextAlign() == 'left' && $isTailwind,
12+
'text-center' => $this->getDefaultBodyTextAlign() == 'center' && $isTailwind,
13+
'text-right' => $this->getDefaultBodyTextAlign() == 'right' && $isTailwind,
1114
'even:bg-white even:dark:bg-gray-700 odd:bg-gray-50 odd:dark:bg-gray-800 dark:text-white' => $isTailwind,
1215
'divide-gray-200 dark:divide-none' => $isTailwind && ($coreTableAttributes['tbody']['default-colors'] ?? ($coreTableAttributes['tbody']['default'] ?? true)),
1316
'divide-y' => $isTailwind && ($coreTableAttributes['tbody']['default-styling'] ?? ($coreTableAttributes['tbody']['default'] ?? true)),
1417
1518
'tw4ph even:bg-white even:dark:bg-gray-700 odd:bg-gray-50 odd:dark:bg-gray-800 dark:text-white' => $isTailwind4,
1619
'tw4ph divide-gray-200 dark:divide-none' => $isTailwind4 && ($coreTableAttributes['tbody']['default-colors'] ?? ($coreTableAttributes['tbody']['default'] ?? true)),
1720
'tw4ph divide-y' => $isTailwind4 && ($coreTableAttributes['tbody']['default-styling'] ?? ($coreTableAttributes['tbody']['default'] ?? true)),
21+
'tw4ph text-left' => $this->getDefaultBodyTextAlign() == 'left' && $isTailwind4,
22+
'tw4ph text-center' => $this->getDefaultBodyTextAlign() == 'center' && $isTailwind4,
23+
'tw4ph text-right' => $this->getDefaultBodyTextAlign() == 'right' && $isTailwind4,
24+
1825
])
1926
->except(['default','default-styling','default-colors'])
2027
}} x-data="{ opening: false, }" >
@@ -33,7 +40,7 @@
3340
@endif
3441

3542
@tableloop($selectedVisibleColumns as $colIndex => $column)
36-
<x-livewire-tables::table.td :isClickable="$column->isClickable()" :wrapText="$column->shouldWrapText()" :isHtml="$column->isHtml()" :customAttributes="$this->getTdAttributes($column, $row, $colIndex, $rowIndex)" :$colIndex wire:key="{{ $dataTableFingerprint . '-table-td-'.$rowPk.'-'.$column->getSlug() }}" x-ref="{{ $dataTableFingerprint . '_' . $rowIndex . '_' . $colIndex }}">
43+
<x-livewire-tables::table.td :textAlign="$column->hasTextAlign() ? $column->getTextAlign() : $this->getDefaultBodyTextAlign()" :isClickable="$column->isClickable()" :wrapText="$column->shouldWrapText()" :isHtml="$column->isHtml()" :customAttributes="$this->getTdAttributes($column, $row, $colIndex, $rowIndex)" :$colIndex wire:key="{{ $dataTableFingerprint . '-table-td-'.$rowPk.'-'.$column->getSlug() }}" x-ref="{{ $dataTableFingerprint . '_' . $rowIndex . '_' . $colIndex }}">
3744
@if($column->setIndexes($rowIndex, $colIndex)->isHtml())
3845
{!! $column->renderContents($row) !!}
3946
@else

src/Features/Columns/Views/Column.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Illuminate\Support\Str;
66
use Rappasoft\LaravelLivewireTables\Traits\Core\HasLocalisations;
7-
use Rappasoft\LaravelLivewireTables\Features\Columns\Views\Traits\{HasDataTableComponent,IsReorderColumn,HasColumnLabelStatus,HasRelations,HasLabelFormat,HasClickable,HasSlug,IsCollapsible,IsSearchable,IsSelectable,IsSortable,HasColumnView,HasFooter,HasSecondaryHeader,HasVisibility, Configuration\ColumnConfiguration, Helpers\ColumnHelpers};
7+
use Rappasoft\LaravelLivewireTables\Features\Columns\Views\Traits\{HasDataTableComponent,IsReorderColumn,HasColumnLabelStatus,HasRelations,HasLabelFormat,HasClickable,HasSlug,IsCollapsible,IsSearchable,IsSelectable,IsSortable,HasColumnView,HasFooter,HasSecondaryHeader,HasVisibility, HasTextAlign, Configuration\ColumnConfiguration, Helpers\ColumnHelpers};
88
use Rappasoft\LaravelLivewireTables\Views\Traits\Core\{HasAttributes, HasLabelAttributes, HasTheme};
99

1010
class Column
@@ -29,7 +29,8 @@ class Column
2929
HasLabelAttributes,
3030
HasSecondaryHeader,
3131
HasTheme,
32-
HasVisibility;
32+
HasVisibility,
33+
HasTextAlign;
3334

3435
// What displays in the columns header
3536
protected string $title;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Features\Columns\Views\Traits;
4+
5+
trait HasTextAlign
6+
{
7+
protected ?string $textAlign;
8+
/**
9+
* Undocumented function
10+
*
11+
* @return boolean
12+
*/
13+
public function hasTextAlign(): bool
14+
{
15+
return isset($this->textAlign);
16+
}
17+
18+
public function getTextAlign(): string
19+
{
20+
return $this->textAlign;
21+
}
22+
23+
public function setTextAlign(string $textAlign): self
24+
{
25+
if($textAlign == 'left' || $textAlign == 'center' || $textAlign == 'right')
26+
{
27+
$this->textAlign = $textAlign;
28+
}
29+
30+
return $this;
31+
}
32+
33+
public function setTextAlignLeft(): self
34+
{
35+
return $this->setTextAlign('left');
36+
}
37+
38+
public function setTextAlignCenter(): self
39+
{
40+
return $this->setTextAlign('center');
41+
}
42+
43+
public function setTextAlignRight(): self
44+
{
45+
return $this->setTextAlign('right');
46+
}
47+
48+
}

src/Traits/Configuration/TableAttributeConfiguration.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,30 @@ public function setShouldBeHidden(): void
145145
{
146146
$this->setShouldBeDisplayedStatus(false);
147147
}
148+
149+
public function setDefaultBodyTextAlign(string $align): self
150+
{
151+
if($align == 'left' || $align == 'center' || $align == 'right')
152+
{
153+
$this->defaultBodyTextAlign = $align;
154+
}
155+
156+
return $this;
157+
}
158+
159+
protected function setDefaultBodyTextAlignLeft(): self
160+
{
161+
return $this->setDefaultBodyTextAlign('left');
162+
}
163+
164+
protected function setDefaultBodyTextAlignCenter(): self
165+
{
166+
return $this->setDefaultBodyTextAlign('center');
167+
}
168+
169+
protected function setDefaultBodyTextAlignRight(): self
170+
{
171+
return $this->setDefaultBodyTextAlign('right');
172+
}
173+
148174
}

src/Traits/Helpers/TableAttributeHelpers.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,5 +395,8 @@ public function getTdAttributesNew(Column $column, Model $row, int $colIndex, in
395395
return $tdAttribs;
396396
}
397397

398-
398+
public function getDefaultBodyTextAlign(): string
399+
{
400+
return $this->defaultBodyTextAlign ?? '';
401+
}
399402
}

src/Traits/WithTableAttributes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ trait WithTableAttributes
104104
*/
105105
protected ?Closure $trUrlTargetCallback;
106106

107+
protected string $defaultBodyTextAlign = '';
107108

108109
/**
109110
* Undocumented function

tests/Visuals/SearchVisualsTest.php

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Rappasoft\LaravelLivewireTables\Tests\Visuals;
44

55
use Livewire\Livewire;
6+
use Livewire\Attributes\Url;
67
use PHPUnit\Framework\Attributes\Group;
78
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable;
89
use Rappasoft\LaravelLivewireTables\Tests\TestCase;
@@ -100,37 +101,84 @@ public function test_search_via_query_string_functions(): void
100101
->assertSee('Chico')
101102
->assertDontSee('Cartman');
102103

103-
$mock = new class extends PetsTable
104+
105+
}
106+
107+
public function test_search_via_query_string_functions_new(): void
108+
{
109+
110+
Livewire::withQueryParams(['petsearch' => 'Chico'])
111+
->test(new class extends PetsTable
104112
{
113+
#[Url(as: 'petsearch')]
114+
public string $search = '';
115+
105116
public ?array $testAttributesArray;
117+
118+
public function refreshQueryString()
119+
{
120+
$this->queryStringHasQueryStringForSearch();
121+
}
106122

107123
public function configure(): void
108124
{
109125
$this->setPrimaryKey('id');
110-
$this->setDataTableFingerprint('test');
111-
$this->setQueryStringAliasForSearch('pet-search');
126+
$this->setQueryStringForSearchEnabled();
127+
$this->setQueryStringAliasForSearch('petsearch');
128+
$this->queryStringHasQueryStringForSearch();
112129
}
113-
};
130+
})
131+
->assertSeeHtml('Chico')
132+
->assertDontSeeHtml('Cartman');
114133

115-
Livewire::withQueryParams(['table-search' => 'Chico'])
116-
->test($mock)
117-
->assertSee('Chico')
118-
->assertSee('Cartman');
134+
Livewire::withQueryParams(['petsearch' => null])
135+
->test(new class extends PetsTable
136+
{
137+
#[Url(as: 'petsearch')]
138+
public string $search = '';
119139

120-
Livewire::withQueryParams(['pet-search' => 'Chico'])
121-
->test($mock)
122-
->assertSee('Chico')
123-
->assertDontSee('Cartman');
140+
public ?array $testAttributesArray;
141+
142+
public function refreshQueryString()
143+
{
144+
$this->queryStringHasQueryStringForSearch();
145+
}
124146

125-
Livewire::withQueryParams(['pet-search' => null])
126-
->test($mock)
147+
public function configure(): void
148+
{
149+
$this->setPrimaryKey('id');
150+
$this->setQueryStringForSearchEnabled();
151+
$this->setQueryStringAliasForSearch('petsearch');
152+
$this->queryStringHasQueryStringForSearch();
153+
}
154+
})
127155
->assertSee('Chico')
128156
->assertSee('Cartman');
129157

130158
Livewire::withQueryParams([])
131-
->test($mock)
159+
->test(new class extends PetsTable
160+
{
161+
#[Url(as: 'petsearch')]
162+
public string $search = '';
163+
164+
public ?array $testAttributesArray;
165+
166+
public function refreshQueryString()
167+
{
168+
$this->queryStringHasQueryStringForSearch();
169+
}
170+
171+
public function configure(): void
172+
{
173+
$this->setPrimaryKey('id');
174+
$this->setQueryStringForSearchEnabled();
175+
$this->setQueryStringAliasForSearch('petsearch');
176+
$this->queryStringHasQueryStringForSearch();
177+
}
178+
})
132179
->assertSee('Chico')
133180
->assertSee('Cartman');
134181

135182
}
183+
136184
}

0 commit comments

Comments
 (0)