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

Fix <NumberInput> and <AutocompleteInput> do not forward the event when calling onBlur #9730

Merged
merged 8 commits into from
Feb 21, 2025

Conversation

yanchesky
Copy link
Contributor

Fixes #9726

@yanchesky
Copy link
Contributor Author

Any clues why mobile.cy.js test faills? 🤔 All e2e tests are passing locally (although once mobile.cy.js failed)

Copy link
Member

@fzaninotto fzaninotto left a comment

Choose a reason for hiding this comment

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

I don't know why the mobile e2e tests are failing, you'll have to investigate.

@@ -153,7 +153,9 @@ export type InputProps<ValueType = any> = Omit<
export type UseInputValue = {
id: string;
isRequired: boolean;
field: ControllerRenderProps;
field: Omit<ControllerRenderProps, 'onBlur'> & {
onBlur: (...event: any[]) => void;
Copy link
Member

Choose a reason for hiding this comment

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

Why don't you use React's built-in FocusEventHandler?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I relied on implementation of onBlur function in useInput

const onBlur = useEvent((...event: any[]) => {
controllerField.onBlur();
if (initialOnBlur) {
initialOnBlur(...event);
}
});

and onBlur of InputProps where type is written in such way.

There are also 2 cases where the FocusEventHandler will fail the type contract.

  1. A component needs to blur the input (BooleanInput, RichTextInput, FileInput, CheckboxGroupInput) because FocusEventHandler requires event as an argument.
const handleChange = useCallback(
        event => {
            field.onChange(event);
            // Ensure field is considered as touched
            field.onBlur();
        },
        [field]
    );
  1. RichTextInput provides EditorEvents['blur'] instead, which doesn't match with the FocusEvent.

Here is the solution I came up that matches type contracts. If you like it i'll commit it

 field: Omit<ControllerRenderProps, 'onBlur'> & {
        onBlur: (
            event?:
                | FocusEvent<HTMLInputElement | HTMLTextAreaElement>
                | EditorEvents['blur']
        ) => void;
    };

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry it took so long to answer. Yes, this new type looks fine

@yanchesky
Copy link
Contributor Author

I don't know why the mobile e2e tests are failing, you'll have to investigate.

yanchesky#1
On my fork, all tests pass, leading me to suspect that this failure is not related to committed changes.

@djhi
Copy link
Collaborator

djhi commented Feb 11, 2025

I don't know why the mobile e2e tests are failing, you'll have to investigate.

yanchesky#1 On my fork, all tests pass, leading me to suspect that this failure is not related to committed changes.

All e2e tests pass locally. Probably a random CI failure

@yanchesky
Copy link
Contributor Author

Are there any other steps I should take in order to finalise this PR?

@djhi
Copy link
Collaborator

djhi commented Feb 12, 2025

Are there any other steps I should take in order to finalise this PR?

Can you implement the onBlur type as you suggested in #9730 (comment)? Hopefully that should trigger the CI again. For some Github reason, I can't restart the CI job. Probably because we took to long to react

Copy link
Contributor

@slax57 slax57 left a comment

Choose a reason for hiding this comment

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

lgtm

But can you also please fix the conflict with the base branch?
Thanks

@yanchesky
Copy link
Contributor Author

Instead of adding a new HTMLSpanElement type, I changed the FocusEvent to accept a generic HTMLElement type.

Copy link
Contributor

@slax57 slax57 left a comment

Choose a reason for hiding this comment

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

Thanks!

@slax57 slax57 merged commit 7f8333a into marmelab:master Feb 21, 2025
15 checks passed
@slax57 slax57 added this to the 5.6.1 milestone Feb 21, 2025
@slax57 slax57 changed the title Fixed onBlur callback on NumberInput and AutocompleteInput Fix <NumberInput> and <AutocompleteInput> do not forward the event when calling onBlur Feb 21, 2025
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.

NumberInput's onBlur prop does not provide an event object
4 participants