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

lwb-255 Implement interactive details component #256

Merged
merged 2 commits into from
Jul 14, 2024
Merged
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
3 changes: 3 additions & 0 deletions resources/views/interactive/details.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<details {!! $globalAttributes !!} {!! $specificAttributes !!}>
{{ $slot }}
</details>
73 changes: 73 additions & 0 deletions src/Components/Interactive/Details.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace T73biz\LwBits\Components\Interactive;

use Illuminate\Contracts\Foundation\Application as ContractedApplication;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View as ContractedView;
use Illuminate\Foundation\Application;
use Illuminate\View\View;
use Livewire\Component;
use Livewire\Features\SupportAttributes\AttributeCollection;
use T73biz\LwBits\Components\GlobalAttributesTrait;

/**
* Class Details
*/
class Details extends Component
{
use GlobalAttributesTrait;

/**
* This attribute enables multiple <details> elements to be connected, with only one open at a time. This allows
* developers to easily create UI features such as accordions without scripting.
*
* The name attribute specifies a group name — give multiple <details> elements the same name value to group them.
* Only one of the grouped <details> elements can be open at a time — opening one will cause another to close. If
* multiple grouped <details> elements are given the open attribute, only the first one in the source order will be
* rendered open.
*/
public string $name = '';

/**
* This Boolean attribute indicates whether the details — that is, the contents of the <details> element — are
* currently visible. The details are shown when this attribute exists, or hidden when this attribute is absent.
* By default this attribute is absent which means the details are not visible.
*/
public bool $open = false;

/**
* The specific attributes for the details element.
*/
private AttributeCollection $specificAttributes;

/**
* Standard mount function
*/
public function mount(): void
{
$this->setGlobalAttributes();
$this->specificAttributes = new AttributeCollection();
if ($this->open) {
$this->specificAttributes->add(['open']);
}
if (! empty($this->name)) {
$this->specificAttributes->add(['name' => $this->name]);
}
}

/**
* Standard render function
*/
public function render(): Application|ContractedApplication|ContractedView|Factory|View
{
return view(
'lw-bits::interactive.details',
[
'globalAttributes' => $this->getGlobalAttributes(),
'specificAttributes' => $this->parseAttributes($this->specificAttributes),
'slot' => '',
]
);
}
}
129 changes: 129 additions & 0 deletions tests/Feature/Interactive/DetailsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

use Livewire\Livewire;
use T73biz\LwBits\Tests\TestSupport\Components\Interactive\MyDetails;

it('can render', function () {
Livewire::test(MyDetails::class)
->assertStatus(200);
});

it('can render with accesskey', function () {
Livewire::test(MyDetails::class, ['accessKey' => true])
->assertSee('accesskey');
});

it('can render with autocapitalize', function () {
Livewire::test(MyDetails::class, ['autoCapitalize' => true])
->assertSee('autocapitalize');
});

it('can render with autofocus', function () {
Livewire::test(MyDetails::class, ['autoFocus' => true])
->assertSee('autofocus');
});

it('can render with contenteditable', function () {
Livewire::test(MyDetails::class, ['contentEditable' => true])
->assertSee('contenteditable');
});

it('can render with css classes', function () {
Livewire::test(MyDetails::class, ['cssClasses' => ['class1', 'class2']])
->assertSee('class="class1 class2"', false);
});

it('can render with data-*', function () {
Livewire::test(MyDetails::class, ['data' => ['name' => 'main-details']])
->assertSee('data-name="main-details"', false);
});

it('can render with dir', function () {
Livewire::test(MyDetails::class, ['direction' => 'rtl'])
->assertSee('dir="rtl"', false);
});

it('can render with draggable', function () {
Livewire::test(MyDetails::class, ['draggable' => true])
->assertSee('draggable');
});

it('can render with enterkeyhint', function () {
Livewire::test(MyDetails::class, ['enterKeyHint' => 'enter'])
->assertSee('enterkeyhint="enter"', false);
});

it('can render with hidden', function () {
Livewire::test(MyDetails::class, ['hidden' => true])
->assertSee('hidden');
});

it('can render with id', function () {
Livewire::test(MyDetails::class, ['id' => 'main-details'])
->assertSee('id="main-details"', false);
});

it('can render with language', function () {
Livewire::test(MyDetails::class, ['language' => 'en'])
->assertSee('lang="en"', false);
});

it('can render with nonce', function () {
Livewire::test(MyDetails::class, ['nonce' => 'abc123'])
->assertSee('nonce="abc123"', false);
});

it('can render with parts', function () {
Livewire::test(MyDetails::class, ['parts' => ['part1', 'part2']])
->assertSee('part="part1 part2"', false);
});

it('can render with popOver', function () {
Livewire::test(MyDetails::class, ['popOver' => true])
->assertSee('popover');
});

it('can render with role', function () {
Livewire::test(MyDetails::class, ['role' => 'main'])
->assertSee('role="main"', false);
});

it('can render with slot', function () {
Livewire::test(MyDetails::class, ['slotAttribute' => 'main-details'])
->assertSee('slot="main-details"', false);
});

it('can render with spellcheck', function () {
Livewire::test(MyDetails::class, ['spellCheck' => true])
->assertSee('spellcheck');
});

it('can render with styles', function () {
Livewire::test(MyDetails::class, ['styles' => ['color' => 'red', 'font-size' => '16px']])
->assertSee('style="color:red;font-size:16px"', false);
});

it('can render with tabindex', function () {
Livewire::test(MyDetails::class, ['tabIndex' => 1])
->assertSee('tabindex="1"', false);
});

it('can render with title', function () {
Livewire::test(MyDetails::class, ['title' => 'main-details'])
->assertSee('title="main-details"', false);
});

it('can render with translate', function () {
Livewire::test(MyDetails::class, ['translate' => true])
->assertSee('translate');
});

it('can render with open', function () {
Livewire::test(MyDetails::class, ['open' => true])
->assertSee('open');
});

it('can render with name', function () {
Livewire::test(MyDetails::class, ['name' => 'main-details'])
->assertSee('name="main-details"', false);
});
7 changes: 7 additions & 0 deletions tests/TestSupport/Components/Interactive/MyDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace T73biz\LwBits\Tests\TestSupport\Components\Interactive;

use T73biz\LwBits\Components\Interactive\Details;

class MyDetails extends Details {}