Skip to content

Commit

Permalink
Brand buttons show focus state when tabbing, file uploader opens when…
Browse files Browse the repository at this point in the history
… pressing enter
  • Loading branch information
RachelElysia committed Oct 14, 2024
1 parent 3d25d46 commit ecc8075
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
22 changes: 20 additions & 2 deletions frontend/components/FileUploader/FileUploader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React, { useState, useRef } from "react";
import classnames from "classnames";

import Button from "components/buttons/Button";
Expand Down Expand Up @@ -74,18 +74,32 @@ export const FileUploader = ({
fileDetails,
}: IFileUploaderProps) => {
const [isFileSelected, setIsFileSelected] = useState(!!fileDetails);
const fileInputRef = useRef<HTMLInputElement>(null);

const classes = classnames(baseClass, className, {
[`${baseClass}__file-preview`]: isFileSelected,
});
const buttonVariant = buttonType === "button" ? "brand" : "text-icon";

const triggerFileInput = () => {
fileInputRef.current?.click();
};

const onFileSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
const files = e.target.files;
onFileUpload(files);
setIsFileSelected(true);

e.target.value = "";
if (fileInputRef.current) {
fileInputRef.current.value = "";
}
};

const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === "Enter") {
e.preventDefault();
triggerFileInput();
}
};

const renderGraphics = () => {
Expand Down Expand Up @@ -113,13 +127,17 @@ export const FileUploader = ({
variant={buttonVariant}
isLoading={isLoading}
disabled={disabled}
onKeyDown={handleKeyDown}
tabIndex={0}
onClick={triggerFileInput}
>
<label htmlFor="upload-file">
{buttonType === "link" && <Icon name="upload" />}
<span>{buttonMessage}</span>
</label>
</Button>
<input
ref={fileInputRef}
accept={accept}
id="upload-file"
type="file"
Expand Down
3 changes: 3 additions & 0 deletions frontend/components/buttons/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface IButtonProps {
onClick?:
| ((value?: any) => void)
| ((evt: React.MouseEvent<HTMLButtonElement>) => void);
onKeyDown?: React.KeyboardEventHandler<HTMLButtonElement>;
isLoading?: boolean;
}

Expand Down Expand Up @@ -101,6 +102,7 @@ class Button extends React.Component<IButtonProps, IButtonState> {
title,
variant,
isLoading,
onKeyDown,
} = this.props;
const fullClassName = classnames(
baseClass,
Expand All @@ -122,6 +124,7 @@ class Button extends React.Component<IButtonProps, IButtonState> {
className={fullClassName}
disabled={disabled}
onClick={handleClick}
onKeyDown={onKeyDown}
tabIndex={tabIndex}
type={type}
title={title}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import strUtils from "utilities/strings";

import Modal from "components/Modal";
import Button from "components/buttons/Button";
import CustomLink from "components/CustomLink";

const baseClass = "delete-host-modal";

Expand Down Expand Up @@ -59,12 +58,7 @@ const DeleteHostModal = ({
};

return (
<Modal
title="Delete host"
onExit={onCancel}
onEnter={onSubmit}
className={baseClass}
>
<Modal title="Delete host" onExit={onCancel} className={baseClass}>
<>
<p>
This will remove the record of <b>{hostText()}</b>.{largeVolumeText()}
Expand Down

0 comments on commit ecc8075

Please sign in to comment.