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

[Autocomplete] Configurable results #2541

Open
wants to merge 9 commits into
base: 2.x
Choose a base branch
from
Open

[Autocomplete] Configurable results #2541

wants to merge 9 commits into from

Conversation

J-Ben87
Copy link

@J-Ben87 J-Ben87 commented Jan 31, 2025

Q A
Bug fix? no
New feature? yes
Issues
License MIT

TomSelect accepts by default an array of results, each item containing a "text" and a "value".
Therefore this bundle exposes results formatted like so.

However one may need to expose additional data such as a "disabled" state to customize TomSelect's rendering.

This PR aims to offer this possibility by letting EntityAutocompleters drive the creation of the results array.

@carsonbot carsonbot added Autocomplete Feature New Feature Status: Needs Review Needs to be reviewed labels Jan 31, 2025
@J-Ben87
Copy link
Author

J-Ben87 commented Jan 31, 2025

I'm not sure why I am getting failures related to "direct deprecation notices" on [LiveComponent] (see https://github.com/symfony/ux/actions/runs/13069237861/job/36467092283?pr=2541 and https://github.com/symfony/ux/actions/runs/13069237861/job/36467096825?pr=2541) when I didn't update this component nor anything related to zenstruck/foundry 🤔

@daFish
Copy link
Contributor

daFish commented Jan 31, 2025

Hey @J-Ben87, this is awesome. This will be very useful in the context of #2534.

@carsonbot carsonbot added Status: Needs Work Additional work is needed Status: Needs Review Needs to be reviewed and removed Status: Needs Review Needs to be reviewed Status: Needs Work Additional work is needed labels Jan 31, 2025
Comment on lines 57 to 60
/**
* Returns the autocomplete result, usually the label as "text" and the value as "value".
* May be used to expose extra options such as "disabled" so that tom-select's rendering may be customized.
*
* @param T $entity
*
* @return array<string, mixed>
*/
/* public function getResult(object $entity): array; */

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well... i'm now pretty sure.

We have

  • a getLabel method (equivalent to choice_label in Form options
  • a getValue method (equivalent to choice_value in Form options)

Instead of introducing a new method that englobes/replaces the previously existing one, i'd rather add a new method

/**
 * @return array<string, string|null>|null
 */
public function getAttributes(object $entity): ?array

In your scenario, you could return ['disabled' => true] when you'd want to.

Wdyt ?

@smnandre
Copy link
Member

smnandre commented Feb 1, 2025

Thank you for the PR, this feature (and the effort to improve things passing by -- docblocks for static analysis)!

Minor changes to match existing DX / wording in symfony/form, and maybe add one or two tests to check everything work if the method is not implemented, or does not override getValue/getText

On an implementation level, you should probably do something like this in the formatResult method

$attributes = [];
if (method_exists(..... 
    ....     
}

return [
   ...$attributes, 
   'value' => $autocompleter->getValue(),
   'text' => $autocompleter->getText(),
];

@smnandre smnandre added Status: Reviewing Review is ongoing, refining with author and removed Status: Needs Review Needs to be reviewed labels Feb 1, 2025
…Interface::getResult() method until next major version
… attributes rather than allowing to override the result entirely
@J-Ben87
Copy link
Author

J-Ben87 commented Feb 1, 2025

Thanks for your review.

I like your idea of merging extra attributes into an array containing the text and the value.

WDYT of the latest version of my branch?

@smnandre
Copy link
Member

smnandre commented Feb 1, 2025

Tiny details, then lets see the CI failures and LGTM :)

Would you be ok (on another PR) to write a paragraph + example about this in the documentation ? I can help if you need / want.

@J-Ben87
Copy link
Author

J-Ben87 commented Feb 2, 2025

Ok everything should be fine now 👍🏻

Regarding the failing tests however, I have no clue why they are failing since they are related to zenstruck/foundry deprecations in another component...
It says:

Since zenstruck/foundry 2.4: In order to use Foundry, you must use the trait "Symfony\Bundle\FrameworkBundle\Test\KernelTestCase" in your "Symfony\UX\LiveComponent\Tests\Unit\Form\ComponentWithFormTest" tests. This will throw an exception in 3.0.

However there is no KernelTestCase trait available in the project. I assume the error message is inaccurate 🤔


Edit: I found the failing tests and I fixed them 🥳

@J-Ben87
Copy link
Author

J-Ben87 commented Feb 2, 2025

No worries for writing a bit of documentation, could you just point me the place and the kind of doc you expect?

@J-Ben87 J-Ben87 requested a review from Kocal February 2, 2025 17:32
@carsonbot carsonbot added the Status: Needs Review Needs to be reviewed label Feb 2, 2025
Copy link
Member

@smnandre smnandre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for me!

Nice PR and thank you @J-Ben87

@carsonbot carsonbot added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Review Needs to be reviewed labels Feb 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Autocomplete Feature New Feature Status: Reviewed Has been reviewed by a maintainer Status: Reviewing Review is ongoing, refining with author
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants