Skip to content

[7.x] Add support for default values for the "props" blade tag#32177

Merged
taylorotwell merged 2 commits intolaravel:7.xfrom
Persaeus:patch-1
Mar 31, 2020
Merged

[7.x] Add support for default values for the "props" blade tag#32177
taylorotwell merged 2 commits intolaravel:7.xfrom
Persaeus:patch-1

Conversation

@Persaeus
Copy link
Contributor

Currently, to make default properties for anonymous components, you have to either upgrade to a full component with a component class, or maybe do something like the following:

<!-- /resources/views/components/alert.blade.php -->

@props(['type', 'message'])
@php
    $type = $type ?? 'info'
@endphp

<div {{ $attributes->merge(['class' => 'alert alert-'.$type]) }}>
    {{ $message }}
</div>

This PR adds support for default property values that you specify in the props tag.

For the previous example, this would look something like the following:

@props(['type' => 'info', 'message'])

This benefits the user, as it's more terse and looks neater (in my opinion).

It should not conflict with how the props tag works currently, as users were not expected to pass associative or mixed arrays as arguments.

As for tests, I could not find any tests related to the props tag, so I'm no sure what I need to do.

We use `array_filter($expression, 'is_string', ARRAY_FILTER_USE_KEY)` to extract properties with string keys, and set the corresponding variable to the default value if unset
@taylorotwell taylorotwell merged commit af6cc0b into laravel:7.x Mar 31, 2020
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

Successfully merging this pull request may close these issues.

2 participants