Problem/Motivation

In modern browsers (personally tested in Chrome and Firefox), input elements with type="number" show often show a "spinner" widget in the input box. However, when one of these number input elements has filled/empty as state condition, the JavaScript does not respond when one uses the spinner widget.

Steps to reproduce

1. Create form structure with a dependee number form element:

$form['number'] = [
  '#type' => 'number',
  '#title' => $this->t('Number'),
];
$form['test'] = [
  '#type' => 'item',
  '#title' => $this->t('Test field'),
  '#states' => [
    'visible' => [
      ':input[name="number"]' => ['filled' => TRUE],
    ],
  ],
];

2. Render the form in a modern browser with JavaScript enabled.
3. Use the spinner widget to update the number field; notice how the test field is not made visible.

Proposed resolution

The empty/filled state trigger only listens for the keyup event, add implementation to listen to the change event too.

Comments

Wongjn created an issue. See original summary.

wongjn’s picture

Assigned: wongjn » Unassigned
Status: Active » Needs review
StatusFileSize
new753 bytes

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

awset’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for the patch. I've reviewed the issue summary, and patch,. Based on the following, I'm marking RTBC.

  1. Patch handles the issue noted in the issue summary and nothing else.
  2. Code changes is clear and addressing the issue
  3. Patch applied to 9.4
  4. Test passes
  5. I have conducted manual test in 9.4 and it works as expected.
lauriii’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs tests

Would it be possible to add test coverage for this?

wongjn’s picture

Issue tags: -Needs tests
StatusFileSize
new3.21 KB

Here we go with a test. A bit more synthetic than I would like but as far as I know, there is no API to use the spinner widget programmatically.

joshua1234511’s picture

Status: Needs review » Reviewed & tested by the community

I've reviewed the issue summary, and the latest patch #7 . Based on the following, I'm marking RTBC.

Patch applied to 9.4
Test passes

Results:
Before batch: https://nimb.ws/OyYeod
After Patch: https://nimb.ws/Z3lqzP

Only local images are allowed.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

We need a 10.x version of the patch. There's no .es6 in D10 (it's not needed) and IE support is dropped so JS standards have changed.

wongjn’s picture

Status: Needs work » Needs review
StatusFileSize
new2.73 KB

Here's a patch based on 10.1.x.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Tested on Drupal 10.1 with a standard install
Took the code snippet from the issue summary and added to a random form, in my case it was admin_toolbar.
Confirmed using number widget the field did not appear

With the patch it does.

  • lauriii committed 8542a65d on 10.1.x
    Issue #3181634 by Wongjn: States API: Empty/Filled state with number...

  • lauriii committed 246664fd on 10.0.x
    Issue #3181634 by Wongjn: States API: Empty/Filled state with number...

  • lauriii committed 809ad58f on 9.5.x
    Issue #3181634 by Wongjn: States API: Empty/Filled state with number...
lauriii’s picture

Status: Reviewed & tested by the community » Fixed

Committed 8542a65 and pushed to 10.1.x. Also cherry-picked to 10.0.x, and committed #7 to 9.5.x. Thanks!

idebr’s picture

Version: 10.1.x-dev » 9.5.x-dev

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

kristen pol’s picture

I was reviewing this and don't understand why no one who tested this got issue credit: awset, joshua1234511, smustgrave

lauriii’s picture

I didn't credit @awset or @joshua1234511 because it wasn't sure based on their comments what had been tested. There wasn't anything specific about this issue in their comments. There may have been additional information behind the external links on #8 but since the website was unfamiliar to me, I decided not to visit them.

The comment from @smustgrave on #13 provides a helpful explanation to what had been tested. I don't recall why I decided not to give credit for that, but it could be because there were automated tests already part of the patch. States API changes tend to be something that are helpful to test manually, but usually that would be outside of the specific change that we are introducing to make sure there wasn't regressions to untested functionality outside of the change.

kristen pol’s picture

Thank you for the explanation. I’ll try to share with my coworkers so they can do better in the future.