Skip to content

Commit e6f6749

Browse files
committed
changed default icon behaviour
1 parent 3ed93d2 commit e6f6749

File tree

3 files changed

+49
-37
lines changed

3 files changed

+49
-37
lines changed

src/Config/sortable.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,11 @@
3232
'prefix' => ' ',
3333

3434
'suffix' => '',
35-
],
3635

37-
// Suffix class that is appended when ascending direction is applied
38-
'asc_suffix' => '-asc',
36+
'asc_suffix' => '-asc',
3937

40-
// Suffix class that is appended when descending direction is applied
41-
'desc_suffix' => '-desc',
38+
'desc_suffix' => '-desc',
39+
],
4240

4341
// Default anchor class, not applied if value is null
4442
'anchor_class' => null,

src/Support/SortableLink.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public static function render(array $parameters): string
2121
request()->merge([$mergeTitleAs => $title]);
2222
}
2323

24-
list($icon, $direction) = self::determineDirection($sortColumn, $sortParameter);
24+
list($icon, $direction) = self::getDirectionAndIcon($sortColumn, $sortParameter);
2525

26-
$trailingTag = self::formTrailingTag($icon);
26+
$trailingTag = self::getTrailingTag($icon);
2727

2828
$anchorClass = self::getAnchorClass($sortParameter, $anchorAttributes);
2929

@@ -100,31 +100,34 @@ private static function applyFormatting($title, string $sortColumn)
100100
return $title;
101101
}
102102

103-
private static function determineDirection($sortColumn, $sortParameter): array
103+
private static function getDirectionAndIcon($sortColumn, $sortParameter): array
104104
{
105105
$icon = self::selectIcon($sortColumn);
106106

107-
if ((request()->get('sort') == $sortParameter) && in_array(request()->get('direction'), ['asc', 'desc'])) {
108-
$icon .= (request()->get('direction') === 'asc')
109-
? config('sortable.asc_suffix')
110-
: config('sortable.desc_suffix');
107+
$sort = request()->get('sort');
108+
$dir = request()->get('direction');
111109

112-
$direction = request()->get('direction') === 'desc' ? 'asc' : 'desc';
110+
if (($sort == $sortParameter) && in_array($dir, ['asc', 'desc'])) {
111+
$icon .= ($dir === 'asc')
112+
? config('sortable.icons.asc_suffix')
113+
: config('sortable.icons.desc_suffix');
113114

114-
return [$icon, $direction];
115+
$direction = ($dir === 'desc') ? 'asc' : 'desc';
115116
} else {
116117
$icon = config('sortable.icons.sortable');
117118
$direction = config('sortable.default_direction_unsorted');
118-
119-
return [$icon, $direction];
120119
}
120+
121+
$icon = static::getIconHtml($icon);
122+
123+
return [$icon, $direction];
121124
}
122125

123126
private static function selectIcon($sortColumn): string
124127
{
125128
$icon = config('sortable.icons.default');
126129

127-
foreach (config('sortable.types', []) as $value) {
130+
foreach (config('sortable.types') as $value) {
128131
if (in_array($sortColumn, $value['fields'])) {
129132
$icon = $value['icon'];
130133
}
@@ -136,22 +139,17 @@ private static function selectIcon($sortColumn): string
136139
/**
137140
* @param string|null $icon
138141
*/
139-
private static function formTrailingTag($icon): string
142+
private static function getTrailingTag($icon): string
140143
{
141144
if (! config('sortable.icons.enabled')) {
142145
return '</a>';
143146
}
144147

145-
$clickableIcon = config('sortable.icons.clickable');
146-
$trailingTag = static::getIconHtml($icon) . '</a>';
147-
148-
if ($clickableIcon === false) {
149-
$trailingTag = '</a>' . static::getIconHtml($icon);
150-
151-
return $trailingTag;
148+
if (config('sortable.icons.clickable') === true) {
149+
return $icon . '</a>';
152150
}
153151

154-
return $trailingTag;
152+
return '</a>' . $icon;
155153
}
156154

157155
/**
@@ -167,12 +165,12 @@ private static function getAnchorClass(string $sortColumn, array &$anchorAttribu
167165
}
168166

169167
$activeClass = config('sortable.active_anchor_class');
170-
if ($activeClass !== null && self::shouldShowActive($sortColumn)) {
168+
if (($activeClass !== null) && self::shouldShowActive($sortColumn)) {
171169
$class[] = $activeClass;
172170
}
173171

174172
$directionClassPrefix = config('sortable.direction_anchor_class_prefix');
175-
if ($directionClassPrefix !== null && self::shouldShowActive($sortColumn)) {
173+
if (($directionClassPrefix !== null) && self::shouldShowActive($sortColumn)) {
176174
$class[] = $directionClassPrefix . (request()->get('direction') === 'asc')
177175
? config('sortable.asc_suffix', '-asc')
178176
: config('sortable.desc_suffix', '-desc');
@@ -230,11 +228,13 @@ private static function buildUrl(string $queryString, array $anchorAttributes):
230228
return url($path . "?" . $queryString);
231229
}
232230

233-
public static function getIconHtml($icon = null): string
231+
/**
232+
* @param string|null $icon
233+
*/
234+
public static function getIconHtml($icon): string
234235
{
235236
$prefix = config('sortable.icons.prefix');
236237
$suffix = config('sortable.icons.suffix');
237-
$icon = $icon ?: config('sortable.icons.default');
238238
$wrapper = config('sortable.icons.wrapper');
239239

240240
return $prefix . str_replace('{icon}', $icon, $wrapper) . $suffix;

tests/Feature/LinkTest.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ public function testGeneratingAnchorAttributes(): void
5454
{
5555
$link = SortableLink::render(['column', 'ColumnTitle', ['a' => 'b'], ['c' => 'd']]);
5656

57-
$expected = '<a href="http://localhost?a=b&sort=column&direction=asc" c="d">ColumnTitle</a>' . SortableLink::getIconHtml();
57+
$icon = config('sortable.icons.default');
58+
59+
$expected = '<a href="http://localhost?a=b&sort=column&direction=asc" c="d">ColumnTitle</a>' . SortableLink::getIconHtml($icon);
5860

5961
$this->assertSame($expected, $link);
6062
}
@@ -64,7 +66,9 @@ public function testGeneratingTitleWithoutFormattingFunction(): void
6466
Config::set('sortable.formatting_function', null);
6567
$link = SortableLink::render(['column']);
6668

67-
$expected = '<a href="http://localhost?sort=column&direction=asc">column</a>' . SortableLink::getIconHtml();
69+
$icon = config('sortable.icons.default');
70+
71+
$expected = '<a href="http://localhost?sort=column&direction=asc">column</a>' . SortableLink::getIconHtml($icon);
6872

6973
$this->assertSame($expected, $link);
7074
}
@@ -75,7 +79,9 @@ public function testGeneratingTitle(): void
7579
Config::set('sortable.format_custom_titles', true);
7680
$link = SortableLink::render(['column']);
7781

78-
$expected = '<a href="http://localhost?sort=column&direction=asc">Column</a>' . SortableLink::getIconHtml();
82+
$icon = config('sortable.icons.default');
83+
84+
$expected = '<a href="http://localhost?sort=column&direction=asc">Column</a>' . SortableLink::getIconHtml($icon);
7985

8086
$this->assertSame($expected, $link);
8187
}
@@ -86,7 +92,9 @@ public function testCustomTitle(): void
8692
Config::set('sortable.format_custom_titles', true);
8793
$link = SortableLink::render(['column', 'ColumnTitle']);
8894

89-
$expected = '<a href="http://localhost?sort=column&direction=asc">ColumnTitle</a>' . SortableLink::getIconHtml();
95+
$icon = config('sortable.icons.default');
96+
97+
$expected = '<a href="http://localhost?sort=column&direction=asc">ColumnTitle</a>' . SortableLink::getIconHtml($icon);
9098

9199
$this->assertSame($expected, $link);
92100
}
@@ -97,7 +105,9 @@ public function testCustomTitleWithoutFormatting(): void
97105
Config::set('sortable.format_custom_titles', false);
98106
$link = SortableLink::render(['column', 'ColumnTitle']);
99107

100-
$expected = '<a href="http://localhost?sort=column&direction=asc">ColumnTitle</a>' . SortableLink::getIconHtml();
108+
$icon = config('sortable.icons.default');
109+
110+
$expected = '<a href="http://localhost?sort=column&direction=asc">ColumnTitle</a>' . SortableLink::getIconHtml($icon);
101111

102112
$this->assertSame($expected, $link);
103113
}
@@ -108,7 +118,9 @@ public function testCustomTitleWithHTML(): void
108118
Config::set('sortable.format_custom_titles', true);
109119
$link = SortableLink::render(['column', new HtmlString('<em>ColumnTitle</em>')]);
110120

111-
$expected = '<a href="http://localhost?sort=column&direction=asc"><em>ColumnTitle</em></a>' . SortableLink::getIconHtml();
121+
$icon = config('sortable.icons.default');
122+
123+
$expected = '<a href="http://localhost?sort=column&direction=asc"><em>ColumnTitle</em></a>' . SortableLink::getIconHtml($icon);
112124

113125
$this->assertSame($expected, $link);
114126
}
@@ -117,7 +129,9 @@ public function testCustomHrefAttribute(): void
117129
{
118130
$link = SortableLink::render(['column', 'ColumnTitle', ['a' => 'b'], ['c' => 'd', 'href' => 'http://localhost/custom-path']]);
119131

120-
$expected = '<a href="http://localhost/custom-path?a=b&sort=column&direction=asc" c="d">ColumnTitle</a>' . SortableLink::getIconHtml();
132+
$icon = config('sortable.icons.default');
133+
134+
$expected = '<a href="http://localhost/custom-path?a=b&sort=column&direction=asc" c="d">ColumnTitle</a>' . SortableLink::getIconHtml($icon);
121135

122136
$this->assertSame($expected, $link);
123137
}

0 commit comments

Comments
 (0)