Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added activeAccordion method to Accordion.php #2939

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions resources/views/layouts/accordion.blade.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<div id="accordion-{{$templateSlug}}" class="accordion mb-3">
@foreach($manyForms as $name => $forms)
<div class="accordion-heading @if ($loop->index) collapsed @endif"
<div class="accordion-heading @if (!in_array($name, $open)) collapsed @endif"
id="heading-{{\Illuminate\Support\Str::slug($name)}}"
data-bs-toggle="collapse"
data-bs-target="#collapse-{{\Illuminate\Support\Str::slug($name)}}"
aria-expanded="true"
aria-expanded="{{ in_array($name, $open) ? 'true' : 'false' }}"
aria-controls="collapse-{{\Illuminate\Support\Str::slug($name)}}">
<h6 class="btn btn-link btn-group-justified pt-2 pb-2 mb-0 pe-0 ps-0 d-flex align-items-center">
<x-orchid-icon path="bs.chevron-right" class="small me-2"/> {!! $name !!}
</h6>
</div>

<div id="collapse-{{\Illuminate\Support\Str::slug($name)}}"
class="mt-2 collapse @if (!$loop->index) show @endif"
class="mt-2 collapse @if (in_array($name, $open)) show @endif"
aria-labelledby="heading-{{\Illuminate\Support\Str::slug($name)}}"
@if (!$stayOpen)
data-bs-parent="#accordion-{{$templateSlug}}"
@endif
@endif
>
@foreach($forms as $form)
{!! $form !!}
@endforeach
@foreach($forms as $form)
{!! $form !!}
@endforeach
</div>
@endforeach
</div>
29 changes: 29 additions & 0 deletions src/Screen/Layouts/Accordion.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Orchid\Screen\Layouts;

use Illuminate\Support\Arr;
use Orchid\Screen\Layout;
use Orchid\Screen\Repository;

Expand All @@ -17,11 +18,17 @@ abstract class Accordion extends Layout
*/
protected $template = 'platform::layouts.accordion';

/**
* @var bool
*/
private $openSet = false;

/**
* @var array
*/
protected $variables = [
'stayOpen' => false,
'open' => [],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should aim for more consistency with naming? Since we're calling the method active*, perhaps it would make sense to also use the key as active.

];

/**
Expand All @@ -32,6 +39,7 @@ abstract class Accordion extends Layout
public function __construct(array $layouts = [])
{
$this->layouts = $layouts;
$this->variables['open'] = [array_key_first($this->layouts)];
}

/**
Expand All @@ -55,4 +63,25 @@ public function stayOpen(bool $stayOpen = true): self

return $this;
}

/**
* Set active accordion(s).
*
* @param string|array $activeAccordion
*
* @return $this
*/
public function open(string|array $activeAccordion): self
{
$activeAccordion = Arr::wrap($activeAccordion);

$this->variables['open'] = $this->openSet
? array_merge($this->variables['open'], $activeAccordion)
: $activeAccordion;

$this->openSet = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of the openSet property in our case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When creating a class, we set a standard key for the first tab in the constructor to preserve the standard previous behavior.

A flag property is needed so that when the method is first called, it can be distinguished whether it was called earlier and, depending on this, merge the array or overwrite it.


return $this;
}

}
Loading