-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from takielias/11.x-dev
Added Tabler Modal.
- Loading branch information
Showing
9 changed files
with
260 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
const TablarModal = { | ||
init() { | ||
this.bindModalTriggers(); | ||
this.bindFormSubmissions(); | ||
}, | ||
|
||
loadModal(url, containerId = 'modal-container') { | ||
fetch(url) | ||
.then(response => { | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
return response.text(); | ||
}) | ||
.then(html => { | ||
document.getElementById(containerId).innerHTML = html; | ||
// Initialize the new modal | ||
const modalElement = document.querySelector('.modal'); | ||
const modal = new bootstrap.Modal(modalElement); | ||
modal.show(); | ||
|
||
// Rebind form submissions for the new modal | ||
this.bindFormSubmissions(); | ||
}) | ||
.catch(error => { | ||
console.error('Error loading modal:', error); | ||
// Optionally show error message to user | ||
alert('Error loading modal content. Please try again.'); | ||
}); | ||
}, | ||
|
||
bindModalTriggers() { | ||
// For buttons with data-modal-url | ||
document.querySelectorAll('[data-modal-url]').forEach(button => { | ||
button.addEventListener('click', (e) => { | ||
e.preventDefault(); | ||
const url = button.dataset.modalUrl; | ||
this.loadModal(url); | ||
}); | ||
}); | ||
|
||
// For links with data-modal-trigger | ||
document.querySelectorAll('[data-modal-trigger]').forEach(link => { | ||
link.addEventListener('click', (e) => { | ||
e.preventDefault(); | ||
const url = link.href; | ||
this.loadModal(url); | ||
}); | ||
}); | ||
}, | ||
|
||
bindFormSubmissions() { | ||
document.querySelectorAll('.modal form').forEach(form => { | ||
form.addEventListener('submit', async (e) => { | ||
e.preventDefault(); | ||
|
||
try { | ||
const response = await fetch(form.action, { | ||
method: form.method, | ||
body: new FormData(form), | ||
headers: { | ||
'X-Requested-With': 'XMLHttpRequest' | ||
} | ||
}); | ||
|
||
const data = await response.json(); | ||
|
||
if (data.success) { | ||
// Close modal | ||
const modal = bootstrap.Modal.getInstance(form.closest('.modal')); | ||
modal.hide(); | ||
|
||
// Clear modal container | ||
document.getElementById('modal-container').innerHTML = ''; | ||
|
||
// Optional: Show success message | ||
if (data.message) { | ||
alert(data.message); | ||
} | ||
|
||
// Optional: Refresh page or update specific content | ||
if (data.reload) { | ||
window.location.reload(); | ||
} | ||
} | ||
} catch (error) { | ||
console.error('Error submitting form:', error); | ||
} | ||
}); | ||
}); | ||
} | ||
}; | ||
|
||
// Initialize TablarModal when DOM is ready | ||
document.addEventListener('DOMContentLoaded', () => { | ||
TablarModal.init(); | ||
}); | ||
|
||
// Global function to open any modal | ||
window.openModal = (url) => { | ||
TablarModal.loadModal(url); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@props([ | ||
'id' => 'modal-form', | ||
'action' => '', | ||
'method' => 'POST' | ||
]) | ||
|
||
<form action="{{ $action }}" method="{{ $method === 'GET' ? 'GET' : 'POST' }}"> | ||
@csrf | ||
@if(!in_array($method, ['GET', 'POST'])) | ||
@method($method) | ||
@endif | ||
|
||
<x-modal :id="$id" {{ $attributes }}> | ||
{{ $slot }} | ||
</x-modal> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
@props([ | ||
'id' => 'modal-default', | ||
'size' => '', // sm, lg, full-width | ||
'status' => '', // success, danger | ||
'title' => '', | ||
'scrollable' => false, | ||
'centered' => true, | ||
]) | ||
|
||
<div {{ $attributes->merge(['class' => 'modal modal-blur fade']) }} | ||
id="{{ $id }}" | ||
tabindex="-1" | ||
role="dialog" | ||
aria-hidden="true"> | ||
<div class="modal-dialog modal-dialog-{{ $centered ? 'centered' : '' }} | ||
{{ $scrollable ? 'modal-dialog-scrollable' : '' }} | ||
{{ $size ? 'modal-'.$size : '' }}" | ||
role="document"> | ||
<div class="modal-content"> | ||
@if($status) | ||
<div class="modal-status bg-{{ $status }}"></div> | ||
@endif | ||
|
||
@if($title || isset($header)) | ||
<div class="modal-header"> | ||
@if(isset($header)) | ||
{{ $header }} | ||
@else | ||
<h5 class="modal-title">{{ $title }}</h5> | ||
@endif | ||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> | ||
</div> | ||
@endif | ||
|
||
<div class="modal-body" id="modal-container"> | ||
{{ $slot }} | ||
</div> | ||
|
||
@isset($footer) | ||
<div class="modal-footer"> | ||
{{ $footer }} | ||
</div> | ||
@endisset | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Takielias\TablarKit\Components\Modals; | ||
|
||
use Illuminate\View\Component; | ||
|
||
class Modal extends Component | ||
{ | ||
public function render() | ||
{ | ||
return view('tablar-kit::components.modals.modal'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Takielias\TablarKit\Components\Modals; | ||
|
||
use Illuminate\View\Component; | ||
|
||
class ModalForm extends Component | ||
{ | ||
public $id; | ||
public $action; | ||
public $method; | ||
public $title; | ||
|
||
public function __construct($id = 'modal-form', $action = '', $method = 'POST', $title = '') | ||
{ | ||
$this->id = $id; | ||
$this->action = $action; | ||
$this->method = $method; | ||
$this->title = $title; | ||
} | ||
|
||
public function render() | ||
{ | ||
return view('tablar-kit::components.modals.form'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TakiElias\TablarKit\Tests\Components\Cards; | ||
|
||
|
||
use TakiElias\TablarKit\Tests\ComponentTestCase; | ||
|
||
class ModalTest extends ComponentTestCase | ||
{ | ||
|
||
/** @test */ | ||
public function the_component_can_be_rendered() | ||
{ | ||
$view = $this->blade('<x-modal status="success" title="Create User"/>' | ||
); | ||
|
||
$view->assertSee('class="modal modal-blur fade"', false) | ||
->assertSee('class="modal-status bg-success"', false) | ||
->assertSee('class="modal-content"', false); | ||
} | ||
|
||
/** @test */ | ||
public function can_render_danger() | ||
{ | ||
$view = $this->blade('<x-modal status="danger" title="Create User"/>' | ||
); | ||
|
||
$view->assertSee('class="modal modal-blur fade"', false) | ||
->assertSee('class="modal-status bg-danger"', false) | ||
->assertSee('class="modal-content"', false); | ||
} | ||
|
||
} |