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

[Bug]: TableToolbarSearch is calling onChangeProp with incorrect parameters when there is a default value #14655

Open
2 tasks done
laramlewis opened this issue Sep 14, 2023 · 4 comments

Comments

@laramlewis
Copy link

Package

carbon-components-react

Browser

No response

Package version

v7.59.8

React version

v16.14.0

Description

In TableToolbarSearch.js, it is calling onChangeProp with two strings instead of an event as expected when a default value is specified.

  useEffect(
    () => {
      if (defaultValue) {
        onChangeProp?.('', defaultValue);
      }
    },
    //eslint-disable-next-line react-hooks/exhaustive-deps
    []
  );

As a result the onChange handler is passed '' instead of an event with a target value.

Reproduction/example

N/A

Steps to reproduce

  • Render a TableToolbarSearch component with a defaultValue and an onChange handler.
  • The onChange handler will be called with an emptyString instead of an event.

Suggested Severity

Severity 3 = User can complete task, and/or has a workaround within the user experience of a given component.

Application/PAL

IBM Knowledge Catalog (formally Watson Knowledge Catalog)

Code of Conduct

@tay1orjones
Copy link
Member

Thanks for reporting this! Here's the relevant source you're referring to:

useEffect(
() => {
if (defaultValue) {
onChangeProp?.('', defaultValue);
}
},
//eslint-disable-next-line react-hooks/exhaustive-deps
[]
);

Since the dependency array is empty, this is only ran once, after the initial render. What event would you expect onChange to be called with in this case? I agree in principle that the format of what's passed through the callback should ideally be the same as the event populated from the actual input's onChange, but I'm not sure what we'd change it to be. What are your thoughts?

@laramlewis
Copy link
Author

@tay1orjones - Thank you for your quick response. I discussed this with my colleague and we have the following thoughts:

Since the current implementation is broken and calls onChange with an empty string instead of an event, consumers are probably not relying on this handler being called when TableToolbarSearch is mounted with a default value.

@wkeese
Copy link
Contributor

wkeese commented Dec 19, 2023

Another thought my colleague had is to make it consistent to TextInput where onChange is not called on ‘mount’ but only when it’s actually changed.

I agree, that's also consistent with how onchange events work on native HTML elements.

Since the current implementation is broken and calls onChange with an empty string instead of an event, consumers are probably not relying on this handler being called when TableToolbarSearch is mounted with a default value.

Unfortunately, I'm guessing the opposite, that some code uses the initial onChange() call to trigger a search on the default value. And that code would break if we just dropped the initial onChange() call.

In other words, I agree with dropping the initial onChange() call but it seems like you would need to wait until Carbon 12. Likewise for replacing the null-like event object with a custom event.

@kubijo
Copy link
Contributor

kubijo commented Jan 23, 2024

Just to add another five cents of context into this, please make sure to back port to the 7.* version line. We are now on 7.59.19 because of the incomplete state of typescript annotations (and the massive undertaking it will take to migrate).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ⏱ Backlog
Development

No branches or pull requests

4 participants