Skip to content

Commit

Permalink
Only add the clear_button plugin to TomSelect if the form element is …
Browse files Browse the repository at this point in the history
…not required
  • Loading branch information
bendavies committed Feb 29, 2024
1 parent 4ae2f72 commit 5bfb733
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Autocomplete/assets/dist/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class default_1 extends Controller {
_default_1_instances = new WeakSet(), _default_1_getCommonConfig = function _default_1_getCommonConfig() {
const plugins = {};
const isMultiple = !this.selectElement || this.selectElement.multiple;
if (!this.formElement.disabled && !isMultiple) {
if (!this.formElement.required && !this.formElement.disabled && !isMultiple) {
plugins.clear_button = { title: '' };
}
if (isMultiple) {
Expand Down
2 changes: 1 addition & 1 deletion src/Autocomplete/assets/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default class extends Controller {

// multiple values excepted if this is NOT A select (i.e. input) or a multiple select
const isMultiple = !this.selectElement || this.selectElement.multiple;
if (!this.formElement.disabled && !isMultiple) {
if (!this.formElement.required && !this.formElement.disabled && !isMultiple) {
plugins.clear_button = { title: '' };
}

Expand Down
10 changes: 10 additions & 0 deletions src/Autocomplete/tests/Fixtures/Form/ProductType.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'autocomplete' => true,
'mapped' => false,
])
->add('colour', ChoiceType::class, [
'choices' => [
'red' => 'red',
'blue' => 'blue',
'green' => 'green',
],
'autocomplete' => true,
'mapped' => false,
'required' => false,
])
->add('tags', TextType::class, [
'mapped' => false,
'autocomplete' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,21 @@ public function testFieldsRenderWithStimulusController()
->assertElementAttributeContains('#product_category', 'data-symfony--ux-autocomplete--autocomplete-url-value', '/test/autocomplete/category_autocomplete_type?extra_options=')
->assertElementAttributeContains('#product_category', 'data-symfony--ux-autocomplete--autocomplete-min-characters-value', '2')
->assertElementAttributeContains('#product_category', 'data-symfony--ux-autocomplete--autocomplete-max-results-value', '25')
->assertElementAttributeContains('#product_category', 'required', 'required')

->assertElementAttributeContains('#product_ingredients', 'required', 'required')

->assertElementAttributeContains('#product_portionSize', 'data-controller', 'symfony--ux-autocomplete--autocomplete')
->assertElementAttributeContains('#product_portionSize', 'required', 'required')

->assertElementAttributeContains('#product_colour', 'data-controller', 'symfony--ux-autocomplete--autocomplete')
// not implemented in zenstruck/browser
// ->elementAttributeNotExists('#product_colour', 'required')
->assertNotSeeElement('#product_colour[required]')

->assertElementAttributeContains('#product_tags', 'data-controller', 'symfony--ux-autocomplete--autocomplete')
->assertElementAttributeContains('#product_tags', 'data-symfony--ux-autocomplete--autocomplete-tom-select-options-value', 'createOnBlur')
->assertElementAttributeContains('#product_tags', 'required', 'required')
;
}

Expand Down

0 comments on commit 5bfb733

Please sign in to comment.