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

[Feature Request]: Allow for any attribute to be added to the config of filters #2000

Open
khwadj opened this issue Oct 16, 2024 · 6 comments
Assignees

Comments

@khwadj
Copy link
Contributor

khwadj commented Oct 16, 2024

Overview

As I'm working with NumberFilters (see issue #1999), I realized that the attributes that would be placed on the input are hardcoded in the view for that filter.

A few attributes are allowed in each filter view, and are pulled from the filter's config.

Detailed explanation

Example: NumberFilter

resources/views/components/tools/filters/number.blade.php

     @if($filter->hasConfig('min')) min="{{ $filter->getConfig('min') }}" @endif
     @if($filter->hasConfig('max')) max="{{ $filter->getConfig('max') }}" @endif
     @if($filter->hasConfig('placeholder')) placeholder="{{ $filter->getConfig('placeholder') }}" @endif

This means that the user cannot define other valid attributes for a number input (ex: step, maxlength) without locally overwriting this view.
The user cannot define any custom attribute either, from which they would benefit for whatever reason they might have.

Notes

Proposed solution:

Simply iterates on the config item and set every key in it. It's the developer's responsibility to add attributes that make sense.

resources/views/components/tools/filters/number.blade.php

    @foreach($filter->getConfigs() as $attributeName => $attributeValue )
           {{ $attributeName }}="{{ $attributeValue }}"
    @endforeach

In addition, it could be interesting the implement a blacklist of config keys to avoid breaking stuff: id, class, type, wire:key.

Views/Traits/Core/HasConfig.php

trait HasConfig
{
    public array $config = [];
    public array $configblacklist = [
        'class',
        'id',
        'type',
        'wire:key',
    ];
    
    public function cleanConfig(array $config = []): array
    {
        return array_filter($config, fn($item) => !in_array($item, $this->configblacklist));
    }

    /**
     * @param  array<mixed>  $config
     */
    public function config(array $config = []): Filter
    {
        $this->config = $this->cleanConfig($config);

        return $this;
    }

    ....
}

Finally, it might be worth allowing the user to add custom classes to their input on top of other attributes, but this is arguably another feature.

@lrljoe
Copy link
Collaborator

lrljoe commented Oct 18, 2024

So - custom classes for the input, that's a feature that is in the works, and should be out in a few versions time.

Very good point in terms of allowing a wider range of attributes to be merged in though, and something I'll definitely look at for the next release, with NumberFilter as the first target, as some of the others do have more specific matching built in, so it'd have to be NumberFilter specific to start with.

@lrljoe lrljoe self-assigned this Oct 18, 2024
@lrljoe
Copy link
Collaborator

lrljoe commented Oct 20, 2024

So having looked at the approach for this, blacklisting at this stage on a more global level, probably isn't viable.

However, for NumberFilter, I'm doing some testing:

public function getConfigsForBlade()
{
  return collect($this->getConfigs())->except(['wire:key', 'wire:model', 'wire:model.live', 'wire:model.blur', 'id', 'type', 'class', 'value']);
}

Now for a release, I would need to ensure that any of the alternative filter behaviours etc are also ignored, such as where debounce etc are used.

Then in the "components.tools.filters.number.blade.php"

@foreach($filter->getConfigsForBlade() as $attributeName => $attributeValue )
    {{ $attributeName }}="{{ $attributeValue }}"
@endforeach

Which so far seems to be working, I will just need to do some further testing, and ensure that other approaches aren't impacted

@lrljoe
Copy link
Collaborator

lrljoe commented Nov 1, 2024

Self reminder to review this again

@lrljoe
Copy link
Collaborator

lrljoe commented Nov 6, 2024

So before we can make this a global feature, we need to:

  • Identify (and document) any core attributes that are utilised within the core package

Otherwise, yes, absolutely, and I've added this to the to-do list.

@lrljoe
Copy link
Collaborator

lrljoe commented Nov 16, 2024

So it's looking possible that I'll add in a
setFilterInputAttributes for filters where its possible to support this.

Rather than using the config approach, and then slowly migrate (in a non breaking way) any existing config options that are an input attribute, into this.
Then config will be purely for configuring, rather than attributes.

This should allow the same approach for the majority of the filters, as config for NumberRange filter for example actually does config related things.

@lrljoe
Copy link
Collaborator

lrljoe commented Nov 23, 2024

Just a note that this is still in the works!

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

2 participants