Skip to content

Commit 9a1960f

Browse files
committed
Add on_change callback and active order binding
1 parent ed24310 commit 9a1960f

File tree

1 file changed

+74
-16
lines changed

1 file changed

+74
-16
lines changed

components/Sorting.php

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Tutor\Components;
1515

16+
use Tutor\Helpers\QueryHelper;
1617
use TUTOR\Icon;
1718

1819
defined( 'ABSPATH' ) || exit;
@@ -26,6 +27,8 @@
2627
* ->order( 'DESC' )
2728
* ->label_asc( 'Oldest' )
2829
* ->label_desc( 'Newest' )
30+
* ->on_change( 'changeOrder' )
31+
* ->bind_active_order( 'currentOrder' )
2932
* ->render();
3033
* ```
3134
*
@@ -54,6 +57,20 @@ class Sorting extends BaseComponent {
5457
*/
5558
protected $label_desc;
5659

60+
/**
61+
* On change callback function name
62+
*
63+
* @var string
64+
*/
65+
protected $on_change = '';
66+
67+
/**
68+
* Alpine.js variable name for active state
69+
*
70+
* @var string
71+
*/
72+
protected $active_order = '';
73+
5774
/**
5875
* Constructor
5976
*/
@@ -70,7 +87,7 @@ public function __construct() {
7087
* @return self
7188
*/
7289
public function order( string $order ): self {
73-
$this->order = $order;
90+
$this->order = QueryHelper::get_valid_sort_order( $order );
7491
return $this;
7592
}
7693

@@ -98,13 +115,39 @@ public function label_desc( string $label ): self {
98115
return $this;
99116
}
100117

118+
/**
119+
* Set JS callback on change
120+
*
121+
* @param string $method_name JS method name.
122+
*
123+
* @return self
124+
*/
125+
public function on_change( string $method_name ): self {
126+
$this->on_change = $method_name;
127+
return $this;
128+
}
129+
130+
/**
131+
* Set Alpine.js active state binding
132+
*
133+
* @param string $active_order Alpine variable name.
134+
* @return self
135+
*/
136+
public function bind_active_order( string $active_order ): self {
137+
$this->active_order = $active_order;
138+
return $this;
139+
}
140+
101141
/**
102142
* Get component content
103143
*
104144
* @return string
105145
*/
106146
public function get(): string {
107-
$order = $this->order;
147+
$orders = array(
148+
'DESC' => $this->label_desc,
149+
'ASC' => $this->label_asc,
150+
);
108151

109152
ob_start();
110153
?>
@@ -114,29 +157,44 @@ public function get(): string {
114157
offset: 4,
115158
})"
116159
>
117-
<button type="button" x-ref="trigger" @click="toggle()" class="tutor-btn tutor-btn-outline tutor-btn-x-small tutor-btn-icon">
160+
<button
161+
type="button"
162+
x-ref="trigger"
163+
@click="toggle()"
164+
class="tutor-btn tutor-btn-outline tutor-btn-x-small tutor-btn-icon"
165+
>
118166
<?php tutor_utils()->render_svg_icon( Icon::STEPPER, 16, 16, array( 'class' => 'tutor-icon-secondary' ) ); ?>
119167
</button>
120-
<div
168+
169+
<div
121170
x-ref="content"
122171
x-show="open"
123172
x-cloak
124173
@click.outside="handleClickOutside()"
125174
class="tutor-popover"
126175
>
127176
<div class="tutor-popover-menu" style="min-width: 108px;">
128-
<a
129-
href="<?php echo esc_attr( add_query_arg( 'order', 'DESC' ) ); ?>"
130-
class="tutor-popover-menu-item<?php echo esc_attr( 'DESC' === $order ? ' tutor-active' : '' ); ?>"
131-
>
132-
<?php echo esc_html( $this->label_desc ); ?>
133-
</a>
134-
<a
135-
href="<?php echo esc_attr( add_query_arg( 'order', 'ASC' ) ); ?>"
136-
class="tutor-popover-menu-item<?php echo esc_attr( 'ASC' === $order ? ' tutor-active' : '' ); ?>"
137-
>
138-
<?php echo esc_html( $this->label_asc ); ?>
139-
</a>
177+
<?php foreach ( $orders as $order => $label ) : ?>
178+
<?php if ( $this->on_change ) : ?>
179+
<button
180+
type="button"
181+
@click="<?php echo esc_js( $this->on_change ); ?>('<?php echo esc_attr( $order ); ?>'); hide()"
182+
class="tutor-popover-menu-item <?php echo esc_attr( ! $this->active_order && $this->order === $order ? ' tutor-active' : '' ); ?>"
183+
<?php if ( $this->active_order ) : ?>
184+
:class="{ 'tutor-active': (<?php echo esc_attr( $this->active_order ); ?> || '') === '<?php echo esc_attr( $order ); ?>' }"
185+
<?php endif; ?>
186+
>
187+
<?php echo esc_html( $label ); ?>
188+
</button>
189+
<?php else : ?>
190+
<a
191+
href="<?php echo esc_attr( add_query_arg( 'order', $order ) ); ?>"
192+
class="tutor-popover-menu-item <?php echo esc_attr( $this->order === $order ? ' tutor-active' : '' ); ?>"
193+
>
194+
<?php echo esc_html( $label ); ?>
195+
</a>
196+
<?php endif; ?>
197+
<?php endforeach; ?>
140198
</div>
141199
</div>
142200
</div>

0 commit comments

Comments
 (0)