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

Button doesn't react on first click due internal exception #19171

Closed
skynet-core opened this issue Jan 11, 2020 · 10 comments
Closed

Button doesn't react on first click due internal exception #19171

skynet-core opened this issue Jan 11, 2020 · 10 comments
Labels
support: question Community support but can be turned into an improvement

Comments

@skynet-core
Copy link

I am using this library in one simple POC project. May be it is related to typescript compiling, but will be grateful for any help.
Lib version:

"@material-ui/core": "^4.8.1"

Here is an example of code:

import React from "react";
import { Formik, FormikProps, Form } from "formik";
import InputField from "../../components/forms/fields/Input";
import {
    validator,
    required,
    requiredAtLeastOne,
} from "../../components/forms/fields/validators";
import { Grid, Box, makeStyles } from "@material-ui/core";
import ActionButton from "../../components/buttons/ActionButton";
import { signIn } from '../../../main/api';
import { toast } from 'react-toastify';
export interface SignInFormValues {
    username: string;
    password: string;
}

export interface SignInProps {
    onChange: any;
    onSignin: any;
    onConfirm: any;
}

const useStyles = makeStyles((theme: any) => (
    {
        linkButton: {
            backgroundColor: 'transparent',
            outline: 'none',
            border: 'none',
            cursor: 'pointer',
            '&:hover': {
                color: '#6E09EA',
                textDecoration: 'underline'
            }
        }
    }
));

export default (props: SignInProps) => {
    const [loading, setLoading] = React.useState(false);
    const classes = useStyles();
    const { onChange, onSignin, onConfirm } = props;
    return <Formik
        initialValues={{
            username: '',
            password: '',
        }}
        onSubmit={(values: SignInFormValues, actions: any) => {
            const { username, password } = values;
            setLoading(true);
            signIn({ username, password })
                .catch(err => {
                    toast.error(`Sigin In failed: ${err.message}`, {
                        position: toast.POSITION.TOP_RIGHT,
                    });
                    if (err.code === "UserNotConfirmedException") {
                        onConfirm();
                    }
                    return null;
                }).then((user) => {
                    actions.setSubmitting(false);
                    setLoading(false);
                    onSignin(user);
                });
        }}>{(formProps: FormikProps<SignInFormValues>) => {
            return (
                <Grid container>
                    <Grid item sm={4}>
                    </Grid>
                    <Grid item sm={4}>
                        <Form onSubmit={formProps.handleSubmit}>
                            <Grid container spacing={2}>
                                <Grid item xs={12}>
                                    <InputField
                                        name="username"
                                        label="Username"
                                        size="sm"
                                        gutter={false}
                                        inputProps={{
                                            autoComplete: "name",
                                            id: "signin-name",
                                        }}
                                        validate={validator(required, requiredAtLeastOne)}
                                    />

                                </Grid>
                                <Grid item xs={12}>
                                    <InputField
                                        name="password"
                                        type="password"
                                        label="login.password"
                                        size="sm"
                                        gutter={false}
                                        inputProps={{
                                            autoComplete: "password",
                                            id: "signin-password",
                                        }}
                                        validate={validator(required)}
                                    />
                                </Grid>
                                <Grid item xs={12}>
                                    <Grid container>
                                        <Grid item xs={4} style={{
                                            display: 'flex',
                                            flexDirection: 'row',
                                            alignItems: 'center'
                                        }}>
                                            <Box width={1} display="flex" mt={3}>
                                                <div style={{ width: '100%' }}>
                                                    <button type="button" className={classes.linkButton}
                                                        onClick={(event: any) => { onChange() }}>Sign Up</button>
                                                </div>
                                            </Box>
                                        </Grid>
                                        <Grid item xs={2}>
                                        </Grid>
                                        <Grid item xs={2}></Grid>
                                        <Grid item xs={4}>
                                            <Box width={1} display="flex" justifyContent="flex-end" mt={3}>
                                                <ActionButton title="Sign In" disabled={loading} feedBack={loading} />
                                            </Box>
                                        </Grid>
                                    </Grid>
                                </Grid>
                            </Grid>
                        </Form>
                    </Grid>
                    <Grid item sm={4}>
                    </Grid>
                </Grid>
            )
        }}</Formik>
};

Here is an screenshot of an error
image

File where error occurs @material-ui/core/esm/utils/focusVisible.js
Function isFocusVisible

Additional information. After second click on button it works fine.

@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 11, 2020

There is a try catch around. We run a feature detection. This is expected. You can disable the stop on all exceptions of Chrome Dev Tools.

I'm curious, what's the version of your browser?

@oliviertassinari
Copy link
Member

I have checked, we are all good:

cc @eps1lon as you might enjoy this issue (for curiosity).

@oliviertassinari oliviertassinari added the support: question Community support but can be turned into an improvement label Jan 11, 2020
@skynet-core
Copy link
Author

skynet-core commented Jan 11, 2020

@oliviertassinari , thank you. I put on this check box only for detecting the issue. The problem that "button doesn't fire "submit" within form on first click", doesn't matter debugger is on or off. Is it clear? It appears in Chrome/Firefox/Safari. I guess it something connected to that exception

@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 11, 2020

@skynet-core Notice the default value of the type prop the Button uses. I'm sorry if it's counter-intuitive.

@skynet-core

This comment has been minimized.

@eps1lon
Copy link
Member

eps1lon commented Jan 13, 2020

cc @eps1lon as you might enjoy this issue (for curiosity).

Either they changed it or I didn't look quite right. But I do explain in the very next line what is happening. The only issue is that it states that a SyntaxError is thrown while it's actually a DOMException.

Apparently this is misleading in case you break on every exception (not just uncaught). Since this kind of debugging is usually exploratory (from my experience) any comment that is slightly off can be very confusing.

I'll check again what type of error should be thrown.

@skynet-core
Copy link
Author

skynet-core commented Jan 13, 2020

@eps1lon thank you a lot. Actually my problem isn't exception within debugger. I added it just to show you because I thought it could help. The problem is that BUTTON with type="submit" doesn't submit form on first click. But simple html <button type="submit"> works well. Do you have any idea how I can solve it? Or do you think better solution will be to implement own button from scratch? :)

@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 13, 2020

The only issue is that it states that a SyntaxError is thrown while it's actually a DOMException.

@eps1lon Oh wow 🔍! On my side, Firefox: SyntaxError, Safari: SyntaxError, Chrome: DOMException 🤷‍♂️, Spec: SyntaxError.

@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 13, 2020

@skynet-core This is very likely a support request, please ask on StackOverflow (What's the probability for having a form submission bug in a framework used by hundreds of thousands of developers each month and having only surface it now?). Also, note that if you do ask there, people won't be able to help you without a reproduction (codesandbox is a great tool for that).

@eps1lon
Copy link
Member

eps1lon commented Jan 13, 2020

The problem is that BUTTON with type="submit" doesn't submit form on first click. But simple html <button type="submit"> works well. Do you have any idea how I can solve it?

This is better asked on StackOverflow. Adding the complete code (with a codesandbox) will accelerate the response time.

Or do you think better solution will be to implement own button from scratch? :)

Very likely not worth it.

@eps1lon Oh wow mag! On my side, Firefox: SyntaxError, Safari: SyntaxError, Chrome: DOMException man_shrugging, Spec: SyntaxError

To be fair a DOMException makes more sense if you refer to SyntaxError on MDN that defines it as an error that is thrown when the parser encounters code that isn't valid JS. This is invalid CSS though. ES spec does not tie SyntaxError to JS. Just states that

SyntaxError
Indicates that a parsing error has occurred.

which does apply. Either way it's a good reminder that catching specific errors can be problematic since browsers might not throw the correct one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests

3 participants