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 input validation on blur and cleanup signup error handling #14724

Merged
merged 7 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions airbyte-webapp/src/components/LabeledInput/LabeledInput.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import React from "react";

import { Input, InputProps } from "components/base";
import { ControlLabels } from "components/LabeledControl";
import { ControlLabels, ControlLabelsProps } from "components/LabeledControl";

type IProps = {
success?: boolean;
message?: React.ReactNode;
label?: React.ReactNode;
labelAdditionLength?: number;
} & InputProps;
type LabeledInputProps = Pick<ControlLabelsProps, "success" | "message" | "label" | "labelAdditionLength"> & InputProps;

const LabeledInput: React.FC<IProps> = (props) => (
const LabeledInput: React.FC<LabeledInputProps> = ({
error,
success,
message,
label,
labelAdditionLength,
...inputProps
}) => (
<ControlLabels
error={props.error}
success={props.success}
message={props.message}
label={props.label}
labelAdditionLength={props.labelAdditionLength}
error={error}
success={success}
message={message}
label={label}
labelAdditionLength={labelAdditionLength}
>
<Input {...props} />
<Input {...inputProps} error={error} />
</ControlLabels>
);

Expand Down
18 changes: 18 additions & 0 deletions airbyte-webapp/src/components/base/Input/Input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,22 @@ describe("<Input />", () => {
fireEvent.change(inputEl, { target: { value: "one more test" } });
expect(onChange).toHaveBeenCalledTimes(1);
});

test("should trigger onFocus once", async () => {
const onFocus = jest.fn();
const { getByTestId } = await render(<Input onFocus={onFocus} />);
const inputEl = getByTestId("input");

fireEvent.focus(inputEl);
expect(onFocus).toHaveBeenCalledTimes(1);
});

test("should trigger onBlur once", async () => {
const onBlur = jest.fn();
const { getByTestId } = await render(<Input onBlur={onBlur} />);
const inputEl = getByTestId("input");

fireEvent.blur(inputEl);
expect(onBlur).toHaveBeenCalledTimes(1);
});
});
15 changes: 9 additions & 6 deletions airbyte-webapp/src/components/base/Input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ const VisibilityButton = styled(Button)`
border: none;
`;

const Input: React.FC<InputProps> = (props) => {
const { defaultFocus = false } = props;

const Input: React.FC<InputProps> = ({ defaultFocus = false, onFocus, onBlur, ...props }) => {
const { formatMessage } = useIntl();
const inputRef = useRef<HTMLInputElement | null>(null);
const [isContentVisible, setIsContentVisible] = useToggle(false);
Expand All @@ -93,7 +91,6 @@ const Input: React.FC<InputProps> = (props) => {
const isPassword = props.type === "password";
const isVisibilityButtonVisible = isPassword && !props.disabled;
const type = isPassword ? (isContentVisible ? "text" : "password") : props.type;
const onInputFocusChange = () => toggleFocused();

useEffect(() => {
if (defaultFocus && inputRef.current !== null) {
Expand All @@ -109,8 +106,14 @@ const Input: React.FC<InputProps> = (props) => {
ref={inputRef}
type={type}
isPassword={isPassword}
onFocus={onInputFocusChange}
onBlur={onInputFocusChange}
onFocus={(event) => {
toggleFocused();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we're touching this right now: Could we maybe change this useToggle into a useState and actually use onFocus/onBlur to set the focused state correctly. Most of the times it shouldn't be a problem, but I've encountered in the past some weird edge-cases where onFocus/onBlur might be called twice (e.g. when browser window focus changes in a weird way), in which case the toggle solution might lose correct track of actual focus.

onFocus?.(event);
}}
onBlur={(event) => {
toggleFocused();
onBlur?.(event);
}}
/>
{isVisibilityButtonVisible ? (
<VisibilityButton
Expand Down
4 changes: 2 additions & 2 deletions airbyte-webapp/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"form.continue": "Continue",
"form.yourEmail": "Your email",
"form.email.placeholder": "[email protected]",
"form.email.error": "This email address doesn’t seem correct.",
"form.empty.error": "Field is required",
"form.email.error": "Enter a valid email",
"form.empty.error": "Required",
"form.selectConnector": "Type to search for a connector",
"form.searchName": "search by name...",
"form.noResult": "No result",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export const SignupForm: React.FC = () => {
})
}
validateOnBlur
validateOnChange
validateOnChange={false}
>
{({ isValid, isSubmitting, values, status }) => (
<Form>
Expand Down