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

Clear field error state if the field is cleared #2424

Merged
merged 12 commits into from
Jan 16, 2025
Merged

Conversation

beaesguerra
Copy link
Member

@beaesguerra beaesguerra commented Jan 10, 2025

Summary:

Previously, if a MultiSelect component had shortcuts enabled, if it was in an error state, the "select all" and "select none" options do not clear the error state. Changing the selected values using these shortcuts now clear the error.

Issue: WB-1846

Test plan:

MultiSelect

Select none

  1. Navigate to ?path=/story/packages-dropdown-multiselect--error-from-validation and confirm that the shortcuts control is enabled
  2. Press the "Select none" option on the field that has an error already
  3. Verify that the error message is cleared, even after blurring the field (since Jupiter is not selected)

Select all

  1. Navigate to ?path=/story/packages-dropdown-multiselect--error-from-validation and confirm that the shortcuts control is enabled
  2. Press the "Select all" option on the field that has an error already
  3. Verify that the error message is cleared. After blurring the field, the error message should return since Jupiter is selected

… error when the field is cleared (including when using the clear button in the SearchField or backspacing the value until it's clear)
…ic value instead of an empty field since errors are cleared for empty fields
…ld: Clear error state when field is cleared and it is not required
@beaesguerra beaesguerra self-assigned this Jan 10, 2025
Copy link

changeset-bot bot commented Jan 10, 2025

🦋 Changeset detected

Latest commit: a993ea9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@khanacademy/wonder-blocks-dropdown Patch
@khanacademy/wonder-blocks-birthday-picker Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented Jan 11, 2025

Size Change: +14 B (+0.01%)

Total Size: 96.5 kB

Filename Size Change
packages/wonder-blocks-dropdown/dist/es/index.js 19.1 kB +14 B (+0.07%)
ℹ️ View Unchanged
Filename Size
packages/wonder-blocks-accordion/dist/es/index.js 3.77 kB
packages/wonder-blocks-banner/dist/es/index.js 1.53 kB
packages/wonder-blocks-birthday-picker/dist/es/index.js 1.77 kB
packages/wonder-blocks-breadcrumbs/dist/es/index.js 887 B
packages/wonder-blocks-button/dist/es/index.js 4.12 kB
packages/wonder-blocks-cell/dist/es/index.js 2.01 kB
packages/wonder-blocks-clickable/dist/es/index.js 3.06 kB
packages/wonder-blocks-core/dist/es/index.js 2.9 kB
packages/wonder-blocks-data/dist/es/index.js 6.24 kB
packages/wonder-blocks-form/dist/es/index.js 6.2 kB
packages/wonder-blocks-grid/dist/es/index.js 1.36 kB
packages/wonder-blocks-icon-button/dist/es/index.js 2.95 kB
packages/wonder-blocks-icon/dist/es/index.js 871 B
packages/wonder-blocks-labeled-field/dist/es/index.js 72 B
packages/wonder-blocks-layout/dist/es/index.js 1.82 kB
packages/wonder-blocks-link/dist/es/index.js 2.28 kB
packages/wonder-blocks-modal/dist/es/index.js 5.42 kB
packages/wonder-blocks-pill/dist/es/index.js 1.65 kB
packages/wonder-blocks-popover/dist/es/index.js 4.85 kB
packages/wonder-blocks-progress-spinner/dist/es/index.js 1.52 kB
packages/wonder-blocks-search-field/dist/es/index.js 1.36 kB
packages/wonder-blocks-switch/dist/es/index.js 1.92 kB
packages/wonder-blocks-testing-core/dist/es/index.js 3.74 kB
packages/wonder-blocks-testing/dist/es/index.js 1.07 kB
packages/wonder-blocks-theming/dist/es/index.js 693 B
packages/wonder-blocks-timing/dist/es/index.js 1.8 kB
packages/wonder-blocks-tokens/dist/es/index.js 2.36 kB
packages/wonder-blocks-toolbar/dist/es/index.js 905 B
packages/wonder-blocks-tooltip/dist/es/index.js 6.99 kB
packages/wonder-blocks-typography/dist/es/index.js 1.23 kB

compressed-size-action

Copy link
Contributor

github-actions bot commented Jan 11, 2025

A new build was pushed to Chromatic! 🚀

https://5e1bf4b385e3fb0020b7073c-ynbxygwart.chromatic.com/

Chromatic results:

Metric Total
Captured snapshots 177
Tests with visual changes 0
Total stories 517
Inherited (not captured) snapshots [TurboSnap] 201
Tests on the build 378

@@ -357,10 +357,12 @@ const MultiSelect = (props: Props) => {
.filter((option) => !!option && !option.props.disabled)
.map((option) => option.props.value);
onChange(selected);
onSelectedValuesChangeValidation();
Copy link
Member Author

Choose a reason for hiding this comment

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

Since the values change when the Select none / Select all options are picked, we call the onSelectedValuesChangeValidation handler

@@ -729,10 +729,10 @@ describe("Required LabeledTextField", () => {
"test-labeled-text-field-field",
);
textField.focus();
await userEvent.clear(textField);
Copy link
Member Author

Choose a reason for hiding this comment

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

Since we now clear the error state when the field is cleared, this test case can be updated with an example where we update the input value instead of clear it

@beaesguerra beaesguerra marked this pull request as ready for review January 11, 2025 00:05
@khan-actions-bot khan-actions-bot requested a review from a team January 11, 2025 00:06
@khan-actions-bot
Copy link
Contributor

khan-actions-bot commented Jan 11, 2025

Gerald

Required Reviewers
  • @Khan/wonder-blocks for changes to .changeset/twenty-ducks-double.md, __docs__/wonder-blocks-dropdown/multi-select.stories.tsx, packages/wonder-blocks-dropdown/src/components/multi-select.tsx, packages/wonder-blocks-dropdown/src/components/__tests__/multi-select.test.tsx

Don't want to be involved in this pull request? Comment #removeme and we won't notify you of further changes.

Copy link
Contributor

github-actions bot commented Jan 11, 2025

npm Snapshot: Published

🎉 Good news!! We've packaged up the latest commit from this PR (bb2a588) and published all packages with changesets to npm.

You can install the packages in webapp by running:

./services/static/dev/tools/deploy_wonder_blocks.js --tag="PR2424"

Packages can also be installed manually by running:

yarn add @khanacademy/wonder-blocks-<package-name>@PR2424

Copy link
Member

@jandrade jandrade left a comment

Choose a reason for hiding this comment

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

This looks great! thanks for including all these tests to cover that 🚢

@@ -141,6 +142,17 @@ export const useFieldValidation = ({
}
});

React.useEffect(() => {
// If it is not required, clear the error when the value is now empty
if (!required && prevValue.current !== value && value.length === 0) {
Copy link
Member

Choose a reason for hiding this comment

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

question: I wonder if you need to use trim() here to cover the case where the user only types spaces (e.g. " ")

Copy link
Member Author

Choose a reason for hiding this comment

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

Great question! We don't need to use trim here to cover the " " case since the error should only be cleared if the field is completely empty so any validation errors are still shown!

@beaesguerra
Copy link
Member Author

beaesguerra commented Jan 15, 2025

I'm testing the npm snapshot in webapp and found that there are some tests that are failing because they rely on the error message to be shown still after the TextField is cleared. I'm going to remove the updates to use-field-validation so we can preserve the existing functionality. It was originally added to address the case where pressing the "clear button" on the SearchField didn't immediately clear the error state. This isn't necessary though since once the user updates the field, the error state will get cleared anyways (unless the new value is invalid).

webapp PR is now passing with only the dropdown changes!

…earchField: Clear error state when field is cleared and it is not required"

This reverts commit d8e0485.
…eared for textfield, textarea, and searchfield"

This reverts commit b5fb688.
…a specific value instead of an empty field since errors are cleared for empty fields"

This reverts commit 7eca261.
…lears an error when the field is cleared (including when using the clear button in the SearchField or backspacing the value until it's clear)"

This reverts commit 7800d39.
@beaesguerra
Copy link
Member Author

I will release and deploy these changes with the LabeledField feature branch!

@beaesguerra beaesguerra requested a review from jandrade January 16, 2025 01:06
Copy link
Member

@jandrade jandrade left a comment

Choose a reason for hiding this comment

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

Thanks for including the snapshot release results. These recent changes make sense! :shipit:

@beaesguerra beaesguerra merged commit b2df9d3 into main Jan 16, 2025
17 of 25 checks passed
@beaesguerra beaesguerra deleted the wb-1846-clear-error branch January 16, 2025 19:03
Copy link

codecov bot commented Jan 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (2ecdfb5) to head (a993ea9).
Report is 18 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@     Coverage Diff      @@
##   main   #2424   +/-   ##
============================
============================

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2ecdfb5...a993ea9. Read the comment docs.

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.

3 participants