Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
104 changes: 104 additions & 0 deletions resources/dist/components/translations-tabs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Drives the locale tabs of the Translations component. Filament's own
// tabsSchemaComponent is tied to the markup of Filament's tabs view, which
// this package does not render, so the package ships its own component.
export default function translationsTabsComponent({
activeTab,
isTabPersistedInQueryString,
livewireId,
tab,
tabQueryStringKey,
}) {
return {
tab,
unsubscribeLivewireHook: null,

init() {
const tabs = this.getTabs()
const queryString = new URLSearchParams(window.location.search)

if (
isTabPersistedInQueryString &&
queryString.has(tabQueryStringKey) &&
tabs.includes(queryString.get(tabQueryStringKey))
) {
this.tab = queryString.get(tabQueryStringKey)
}

if (!this.tab || !tabs.includes(this.tab)) {
this.tab = tabs[activeTab - 1] ?? tabs[0] ?? null
}

this.$watch('tab', () => {
this.updateQueryString()
this.autofocusFields()
})

this.autofocusFields(true)

this.unsubscribeLivewireHook = Livewire.interceptMessage(
({ message, onSuccess }) => {
onSuccess(() => {
this.$nextTick(() => {
if (message.component.id !== livewireId) {
return
}

const tabs = this.getTabs()

if (!tabs.includes(this.tab)) {
this.tab = tabs[activeTab - 1] ?? this.tab
}
})
})
},
)
},

getTabs() {
return this.$refs.tabsData
? JSON.parse(this.$refs.tabsData.value)
: []
},

updateQueryString() {
if (!isTabPersistedInQueryString) {
return
}

const url = new URL(window.location.href)
url.searchParams.set(tabQueryStringKey, this.tab)

history.replaceState(null, document.title, url.toString())
},

autofocusFields(respectCurrentFocus = false) {
this.$nextTick(() => {
if (
respectCurrentFocus &&
document.activeElement &&
document.activeElement !== document.body &&
this.$el.compareDocumentPosition(document.activeElement) &
Node.DOCUMENT_POSITION_PRECEDING
) {
return
}

const fields = this.$el.querySelectorAll(
'.fi-sc-tabs-tab.fi-active [autofocus]',
)

for (const field of fields) {
field.focus()

if (document.activeElement === field) {
break
}
}
})
},

destroy() {
this.unsubscribeLivewireHook?.()
},
}
}
4 changes: 2 additions & 2 deletions resources/views/forms/components/translations.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
@if (blank($livewireProperty))
<div
x-load
x-load-src="{{ \Filament\Support\Facades\FilamentAsset::getAlpineComponentSrc('tabs', 'filament/schemas') }}"
x-data="tabsSchemaComponent({
x-load-src="{{ \Filament\Support\Facades\FilamentAsset::getAlpineComponentSrc('translations-tabs', 'happenv-com/filament-translatable') }}"
x-data="translationsTabsComponent({
activeTab: @js($activeTab),
isTabPersistedInQueryString: @js($isTabPersistedInQueryString()),
livewireId: @js($this->getId()),
Expand Down
2 changes: 2 additions & 0 deletions src/FilamentTranslatableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Filament\Forms\Components\Field;
use Filament\Support\Assets\AlpineComponent;
use Filament\Support\Assets\Asset;
use Filament\Support\Assets\Css;
use Filament\Support\Facades\FilamentAsset;
Expand Down Expand Up @@ -83,6 +84,7 @@ protected function getAssetPackageName(): ?string
protected function getAssets(): array
{
return [
AlpineComponent::make('translations-tabs', __DIR__ . '/../resources/dist/components/translations-tabs.js'),
Css::make('filament-translatable-styles', __DIR__ . '/../resources/dist/filament-translatable.css'),
];
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Feature/ReviewRegressionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Filament\Forms\Components\TagsInput;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Support\Facades\FilamentAsset;
use Happenv\FilamentTranslatable\Enums\TranslationMode;
use Happenv\FilamentTranslatable\Forms\Component\Translations;
use Happenv\FilamentTranslatable\Forms\Component\Translations\Tab;
Expand Down Expand Up @@ -46,6 +47,21 @@
$component->assertDontSeeHtml("tab !== 'form.");
});

it('drives its tabs with its own Alpine component instead of the Filament tabs component', function (): void {
// Filament's tabsSchemaComponent requires the markup of Filament's own tabs view (#37)
SchemaForm::$componentsUsing = fn (): array => [
Translations::make('translations')
->locales(['en', 'pl'])
->schema([TextInput::make('title')]),
];

livewire(SchemaForm::class)
->assertSeeHtml('x-data="translationsTabsComponent(')
->assertSeeHtml(e(FilamentAsset::getAlpineComponentSrc('translations-tabs', 'happenv-com/filament-translatable')))
->assertDontSeeHtml('tabsSchemaComponent(')
->assertDontSeeHtml(e(FilamentAsset::getAlpineComponentSrc('tabs', 'filament/schemas')));
});

it('loads casted field types from the record', function (TranslationMode $mode): void {
$record = Posts::create($mode, ['title' => ['en' => '1'], 'content' => ['en' => 'a,b']]);

Expand Down
Loading