Skip to content

Commit b6481b2

Browse files
committed
Only set slug when it's not set yet
1 parent c136d59 commit b6481b2

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/Fields/Repeater.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,15 @@ public function getForm(): array
115115
Forms\Components\Toggle::make('config.reorderableWithButtons')
116116
->label(__('Reorderable with buttons'))
117117
->dehydrated()
118-
->disabled(fn (Forms\Get $get): bool => $get('config.reorderable') === false)
118+
->disabled(fn(Forms\Get $get): bool => $get('config.reorderable') === false)
119119
->inline(false),
120120
]),
121121
Forms\Components\Toggle::make('config.collapsible')
122122
->label(__('Collapsible'))
123123
->inline(false),
124124
Forms\Components\Toggle::make('config.collapsed')
125125
->label(__('Collapsed'))
126-
->visible(fn (Forms\Get $get): bool => $get('config.collapsible') === true)
126+
->visible(fn(Forms\Get $get): bool => $get('config.collapsible') === true)
127127
->inline(false),
128128
Forms\Components\Toggle::make('config.cloneable')
129129
->label(__('Cloneable'))
@@ -142,9 +142,9 @@ public function getForm(): array
142142
->live(debounce: 250)
143143
->labelKey('name')
144144
->maxDepth(0)
145-
->addable(fn (string $operation) => $operation !== 'create')
146-
->disabled(fn (string $operation) => $operation === 'create')
147-
->hint(fn (string $operation) => $operation === 'create' ? __('Fields can be added once the field is created.') : '')
145+
->addable(fn(string $operation) => $operation !== 'create')
146+
->disabled(fn(string $operation) => $operation === 'create')
147+
->hint(fn(string $operation) => $operation === 'create' ? __('Fields can be added once the field is created.') : '')
148148
->hintColor('primary')
149149
->form([
150150
Section::make('Field')
@@ -159,7 +159,17 @@ public function getForm(): array
159159
->required()
160160
->placeholder(__('Name'))
161161
->live(onBlur: true)
162-
->afterStateUpdated(fn (Set $set, ?string $state) => $set('slug', Str::slug($state))),
162+
->afterStateUpdated(function (Set $set, Get $get, ?string $state, ?string $old, ?Field $record) {
163+
if (! $record || blank($get('slug'))) {
164+
$set('slug', Str::slug($state));
165+
}
166+
167+
$currentSlug = $get('slug');
168+
169+
if (! $record?->slug && (! $currentSlug || $currentSlug === Str::slug($old))) {
170+
$set('slug', Str::slug($state));
171+
}
172+
}),
163173
TextInput::make('slug')
164174
->readonly(),
165175
Select::make('field_type')
@@ -190,10 +200,10 @@ function () {
190200
])->columnSpanFull(),
191201
Section::make('Configuration')
192202
->columns(3)
193-
->schema(fn (Get $get) => $this->getFieldTypeFormSchema(
203+
->schema(fn(Get $get) => $this->getFieldTypeFormSchema(
194204
$get('field_type')
195205
))
196-
->visible(fn (Get $get) => filled($get('field_type'))),
206+
->visible(fn(Get $get) => filled($get('field_type'))),
197207
]),
198208
])->columns(2),
199209
])->columnSpanFull(),

0 commit comments

Comments
 (0)