You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CustomTableRepeater.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Filament\Forms\Components;
  3. use Awcodes\TableRepeater\Components\TableRepeater;
  4. use Closure;
  5. use Filament\Forms\Components\Actions\Action;
  6. class CustomTableRepeater extends TableRepeater
  7. {
  8. protected bool | Closure $spreadsheet = false;
  9. protected bool | Closure $reorderAtStart = false;
  10. /**
  11. * @var array<string> | Closure | null
  12. */
  13. protected array | Closure | null $excludedAttributesForCloning = [
  14. 'id',
  15. 'line_number',
  16. 'created_by',
  17. 'updated_by',
  18. 'created_at',
  19. 'updated_at',
  20. ];
  21. public function spreadsheet(bool | Closure $condition = true): static
  22. {
  23. $this->spreadsheet = $condition;
  24. return $this;
  25. }
  26. public function isSpreadsheet(): bool
  27. {
  28. return (bool) $this->evaluate($this->spreadsheet);
  29. }
  30. public function reorderAtStart(bool | Closure $condition = true): static
  31. {
  32. $this->reorderAtStart = $condition;
  33. return $this;
  34. }
  35. public function isReorderAtStart(): bool
  36. {
  37. return $this->evaluate($this->reorderAtStart) && $this->isReorderable();
  38. }
  39. /**
  40. * @param array<string> | Closure | null $attributes
  41. */
  42. public function excludeAttributesForCloning(array | Closure | null $attributes): static
  43. {
  44. $this->excludedAttributesForCloning = $attributes;
  45. return $this;
  46. }
  47. /**
  48. * @return array<string> | null
  49. */
  50. public function getExcludedAttributesForCloning(): ?array
  51. {
  52. return $this->evaluate($this->excludedAttributesForCloning);
  53. }
  54. protected function setUp(): void
  55. {
  56. parent::setUp();
  57. $this->minItems(1);
  58. $this->extraAttributes(function (): array {
  59. $attributes = [];
  60. if ($this->isSpreadsheet()) {
  61. $attributes['class'] = 'is-spreadsheet';
  62. }
  63. return $attributes;
  64. });
  65. $this->reorderAction(function (Action $action) {
  66. if ($this->isReorderAtStart()) {
  67. $action->icon('heroicon-m-bars-3');
  68. }
  69. return $action;
  70. });
  71. $this->cloneAction(function (Action $action) {
  72. return $action
  73. ->action(function (array $arguments, CustomTableRepeater $component): void {
  74. $newUuid = $component->generateUuid();
  75. $items = $component->getState();
  76. $clone = $items[$arguments['item']];
  77. foreach ($component->getExcludedAttributesForCloning() as $attribute) {
  78. unset($clone[$attribute]);
  79. }
  80. if ($newUuid) {
  81. $items[$newUuid] = $clone;
  82. } else {
  83. $items[] = $clone;
  84. }
  85. $component->state($items);
  86. $component->collapsed(false, shouldMakeComponentCollapsible: false);
  87. $component->callAfterStateUpdated();
  88. });
  89. });
  90. }
  91. public function getView(): string
  92. {
  93. return 'filament.forms.components.custom-table-repeater';
  94. }
  95. }