Skip to content
Snippets Groups Projects
Verified Commit 80f65eb9 authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

feat: support for keyboard and button file

parent 6985cd4b
No related branches found
No related tags found
No related merge requests found
...@@ -49,10 +49,12 @@ ...@@ -49,10 +49,12 @@
position: relative; position: relative;
} }
.button.simple:focus,
.button.simple:hover { .button.simple:hover {
background-color: #50cb93; background-color: #50cb93;
} }
.button.simple.red:focus,
.button.simple.red:hover { .button.simple.red:hover {
background-color: #e63946; background-color: #e63946;
} }
......
import React, { ChangeEvent, FC } from "react"; import React, { ChangeEvent, FC, useRef } from "react";
import "./button.css"; import "./button.css";
...@@ -37,6 +37,7 @@ export const Button: FC<ButtonProps> = ({ ...@@ -37,6 +37,7 @@ export const Button: FC<ButtonProps> = ({
file ? "file" : "", file ? "file" : "",
...style ...style
].join(" "); ].join(" ");
const fileRef = useRef<HTMLInputElement>(null);
const onFileChange = (event: ChangeEvent<HTMLInputElement>) => { const onFileChange = (event: ChangeEvent<HTMLInputElement>) => {
if (!event.target.files) return; if (!event.target.files) return;
if (event.target.files.length === 0) return; if (event.target.files.length === 0) return;
...@@ -54,6 +55,7 @@ export const Button: FC<ButtonProps> = ({ ...@@ -54,6 +55,7 @@ export const Button: FC<ButtonProps> = ({
}; };
const onKeyPress = (event: React.KeyboardEvent) => { const onKeyPress = (event: React.KeyboardEvent) => {
if (event.key !== "Enter") return; if (event.key !== "Enter") return;
if (file) fileRef.current?.click();
onClick && onClick(); onClick && onClick();
}; };
const renderSimple = () => ( const renderSimple = () => (
...@@ -77,7 +79,12 @@ export const Button: FC<ButtonProps> = ({ ...@@ -77,7 +79,12 @@ export const Button: FC<ButtonProps> = ({
> >
{image && <img src={image} alt={imageAlt ?? text ?? "button"} />} {image && <img src={image} alt={imageAlt ?? text ?? "button"} />}
{file && ( {file && (
<input type="file" accept={accept} onChange={onFileChange} /> <input
ref={fileRef}
type="file"
accept={accept}
onChange={onFileChange}
/>
)} )}
<span>{text}</span> <span>{text}</span>
</span> </span>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment