I have a FileElement and I would like to add a class to it dynamically based on some properties. For example, take this piece of code:
public static function makeImageFileUpload(
string $name,
string $directory = 'profiles/images',
string $visibility = 'public',
): FileUpload {
return FileUpload::make($name)
->image()
->imageEditor()
->imageEditorAspectRatios(['9:16'])
->disk($visibility)
->directory($directory)
->visibility($visibility)
->downloadable()
->openable()
->deletable()
->live()
->extraAttributes(function (FileUpload $component, Get $get) use ($name): array {
$fileCount = $component->isMultiple() ? $component->getMaxFiles() : 1;
Log::info($name, ['maxCount' => $fileCount, 'actualCount' => count($get($name))]);
if ($fileCount === count($get($name))) {
Log::info($name . ' hidden');
return ['class' => 'file-upload-hidden'];
} else {
Log::info($name . ' visible');
return ['class' => 'file-upload-visible'];
}
})
;
}
What happens is that the extraAttributes
method gets invoked when I change the FileUpload
data in the form, however it does not update. It initially renders with the class that matches the logic, but if that changes, it does not add/remove any other classes. It’s like the class property of the html element is only set during the first render, and any other changes to it are ignored. Is there anything I could do about it?