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

Add missing configuration methods to TranslatedText component #1826

Merged
Merged
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
138 changes: 132 additions & 6 deletions packages/admin/src/Support/Forms/Components/TranslatedText.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lunar\Admin\Support\Forms\Components;

use Closure;
use Filament\Forms\ComponentContainer;
use Filament\Forms\Components\Component;
use Filament\Forms\Components\TextInput;
Expand All @@ -16,6 +17,24 @@ class TranslatedText extends TextInput

public bool $optionRichtext = false;

protected ?array $richtextToolbarButtons = null;

protected array $richtextDisableToolbarButtons = [];

protected bool $richtextDisableAllToolbarButtons = false;

protected Closure|null|string $richtextFileAttachmentsDisk = null;

protected Closure|null|string $richtextFileAttachmentsDirectory = null;

protected Closure|string $richtextFileAttachmentsVisibility = 'public';

protected ?Closure $richtextGetUploadedAttachmentUrlUsing = null;

protected ?Closure $richtextSaveUploadedFileAttachmentsUsing = null;

protected bool $mergeExtraInputAttributes = false;

public Language $defaultLanguage;

public Collection $components;
Expand All @@ -37,12 +56,91 @@ public function prepareChildComponents()
{
$this->components = collect(
$this->getLanguages()->map(fn ($lang) => $this->getOptionRichtext() ?
TranslatedRichEditor::make($lang->code)->statePath($lang->code) :
TranslatedTextInput::make($lang->code)->statePath($lang->code)
$this->getTranslatedRichEditorComponent($lang->code) :
$this->getTranslatedTextComponent($lang->code)
)
);
}

protected function getTranslatedRichEditorComponent(string $langCode): TranslatedRichEditor
{
$component = TranslatedRichEditor::make($langCode)
->statePath($langCode)
->disableAllToolbarButtons($this->richtextDisableAllToolbarButtons)
->fileAttachmentsVisibility($this->richtextFileAttachmentsVisibility)
->fileAttachmentsDirectory($this->richtextFileAttachmentsDirectory)
->fileAttachmentsDisk($this->richtextFileAttachmentsDisk)
->getUploadedAttachmentUrlUsing($this->richtextGetUploadedAttachmentUrlUsing)
->saveUploadedFileAttachmentsUsing($this->richtextSaveUploadedFileAttachmentsUsing);

if (! empty($this->richtextToolbarButtons)) {
$component->disableToolbarButtons($this->richtextToolbarButtons);
}

if ($this->richtextToolbarButtons !== null) {
$component->toolbarButtons($this->richtextToolbarButtons);
}

return $this->prepareTranslatedTextComponent($component);
}

public function extraInputAttributes(array | Closure $attributes, bool $merge = false): static
{
$this->mergeExtraInputAttributes = $merge;

if ($merge) {
$this->extraInputAttributes[] = $attributes;
} else {
$this->extraInputAttributes = [$attributes];
}

return $this;
}

protected function getTranslatedTextComponent(string $langCode): TranslatedTextInput
{
$component = TranslatedTextInput::make($langCode)
->statePath($langCode)
->telRegex($this->telRegex)
->step($this->step);

if ($this->isEmail) {
$component->email();
}

if ($this->isTel) {
$component->tel();
}

if ($this->isUrl) {
$component->url();
}

if ($this->isNumeric) {
$component->numeric();
}

if ($this->step === 1) {
$component->integer();
}

return $this->prepareTranslatedTextComponent($component);
}

protected function prepareTranslatedTextComponent(TranslatedTextInput|TranslatedRichEditor $component): TranslatedTextInput|TranslatedRichEditor
{
$component
->regex($this->regexPattern)
->minLength($this->minLength)
->maxLength($this->maxLength);

if (!empty($this->extraInputAttributes)) {
$component->extraInputAttributes($this->extraInputAttributes, $this->mergeExtraInputAttributes);
}

return $component;
}

public function prepareTranslateLocaleComponent(Component $component, string $locale)
{
$localeComponent = clone $component;
Expand Down Expand Up @@ -83,17 +181,45 @@ public function getOptionRichtext(): bool
return $this->optionRichtext;
}

public function getExpanded()
public function richtextToolbarButtons(array $buttons): static
{
$this->richtextToolbarButtons = $buttons;

return $this;
}

public function richtextDisableToolbarButtons(array $buttons): static
{
$this->richtextDisableToolbarButtons = $buttons;

return $this;
}

public function richtextDisableAllToolbarButtons(bool $condition = true): static
{
$this->richtextDisableAllToolbarButtons = $condition;

return $this;
}

public function richtextFileAttachmentsDirectory(string | Closure | null $name): static
{
$this->richtextFileAttachmentsDirectory = $name;

return $this;
}

public function getExpanded(): bool
{
return $this->expanded;
}

public function getDefaultLanguage()
public function getDefaultLanguage(): Language
{
return $this->languages->first(fn ($lang) => $lang->default);
}

public function getMoreLanguages()
public function getMoreLanguages(): Collection
{
return $this->languages->filter(fn ($lang) => ! $lang->default);
}
Expand All @@ -103,7 +229,7 @@ public function getLanguageDefaults(): array
return $this->getLanguages()->mapWithKeys(fn ($language) => [$language->code => ''])->toArray();
}

public function getLanguages()
public function getLanguages(): Collection
{
return $this->languages;
}
Expand Down