Skip to content

Commit

Permalink
Add missing configuration methods to TranslatedText component (#1826)
Browse files Browse the repository at this point in the history
This PR solves the issue
[#1819](#1819) by providing
missing configuration methods to the TranslatedText component for
RichEditor option. It also allows to pass method parameters to the child
components that they can take an effect there.

Following methods are now available for TranslatedText component: 

```php

// For enabled rich editor option only
TranslatedText::make('text-editor')
    ->optionRichtext(true)
    ->richtextToolbarButtons() // equal to toolbarButtons on RichEditor
    ->richtextDisableToolbarButtons() // equal to disableToolbarButtons on RichEditor
    ->richtextDisableAllToolbarButtons() // equal to disableAllToolbarButtons on RichEditor
    ->richtextFileAttachmentsDirectory() // equal to fileAttachmentsDirectory on RichEditor
    ->richtextGetUploadedAttachmentUrlUsing() // equal to getUploadedAttachmentUrlUsing on RichEditor
    ->richtextSaveUploadedFileAttachmentsUsing(); // equal to saveUploadedFileAttachmentsUsing on RichEditor

// For text input only
TranslatedText::make('text-input')
    ->tel()
    ->telRegex()
    ->email()
    ->url()
    ->numeric()
    ->integer()
    ->step();

// For both
TranslatedText::make('text')
    ->regex()
    ->minLength()
    ->maxLength();
```

---------

Co-authored-by: Glenn Jacobs <[email protected]>
  • Loading branch information
xalabama and glennjacobs authored Aug 22, 2024
1 parent fa0dcda commit 9afbb43
Showing 1 changed file with 132 additions and 6 deletions.
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

0 comments on commit 9afbb43

Please sign in to comment.