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

Using name attribute with Scoped Slots #47057

Closed
AntonCooper opened this issue May 12, 2023 · 2 comments
Closed

Using name attribute with Scoped Slots #47057

AntonCooper opened this issue May 12, 2023 · 2 comments
Assignees

Comments

@AntonCooper
Copy link

Laravel Version

9.52.7

PHP Version

8.2.5

Database Driver & Version

No response

Description

Passing a name attribute into a component scoped slot will cause the supplied name attribute to be missing in the scoped slot's attribute variable. This only occurs when name is the first attribute passed in.

ComponentTagCompiler::compileSlots is responsible for this. My understanding is that slots used to be named by passing a name attribute. This is no longer the documented way of creating a named slot but this check is still in the code to support backwards compatibility.

Because name is a very common HTML attribute used on forms, this gotcha can catch you out very easily and result in some unexpected behaviour of the name attribute just disappearing - which can cause forms to break

Possible Solutions

  • Document this behaviour in the docs
  • Deprecate naming slots using the name attribute and drop support for in the next major release
  • Make the name attribute available in the scoped slot's attribute variable

Steps To Reproduce

Create a component in resources/views/components/input.blade.php

@props([
    'input'
])

<div>
    <input type="text" {{ $input->attributes->class('input') }} />
</div>

Render the component, passing a name attribute as the first first attribute

Blade::render('
<x-input>
    <x-slot:input name="my_form_field" class="text-input-lg" data-test="data">Test</x-slot:input>
</x-input>
');

Observe, the name does not get rendered in the slot's attributes

<div class="block">
    <input type="text" class="input text-input-lg" data-test="data" />
</div>

Moving the name attribute to not being the first attribute will make the name attribute render

Blade::render('
<x-input>
    <x-slot:input class="text-input-lg" name="my_form_field" data-test="data">Test</x-slot:input>
</x-input>
');

Outputs

<div class="block">
    <input type="text" class="input text-input-lg" name="my_form_field" data-test="data" />
</div>
@nunomaduro
Copy link
Member

Made a pull request clarifying this situation: laravel/docs#8788. No worries, if Taylor thinks that this issue should be addressed for Laravel 10 or 11, I will follow up. Thanks!

@taylorotwell
Copy link
Member

#47065

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants