2022-05-04 17:09:48 +01:00
|
|
|
/*
|
2023-01-03 16:55:26 +00:00
|
|
|
Copyright 2022 New Vector Ltd
|
2022-05-04 17:09:48 +01:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-18 16:02:27 -08:00
|
|
|
import { useObjectRef } from "@react-aria/utils";
|
2022-08-09 11:44:46 +02:00
|
|
|
import React, { AllHTMLAttributes, useEffect } from "react";
|
2022-02-18 16:02:27 -08:00
|
|
|
import { useCallback } from "react";
|
|
|
|
import { useState } from "react";
|
|
|
|
import { forwardRef } from "react";
|
|
|
|
import classNames from "classnames";
|
2022-10-10 09:19:10 -04:00
|
|
|
import { useTranslation } from "react-i18next";
|
2022-08-09 11:44:46 +02:00
|
|
|
|
|
|
|
import { Avatar, Size } from "../Avatar";
|
|
|
|
import { Button } from "../button";
|
2022-02-18 16:02:27 -08:00
|
|
|
import { ReactComponent as EditIcon } from "../icons/Edit.svg";
|
|
|
|
import styles from "./AvatarInputField.module.css";
|
|
|
|
|
2022-08-09 11:44:46 +02:00
|
|
|
interface Props extends AllHTMLAttributes<HTMLInputElement> {
|
|
|
|
id: string;
|
|
|
|
label: string;
|
|
|
|
avatarUrl: string;
|
|
|
|
displayName: string;
|
|
|
|
onRemoveAvatar: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const AvatarInputField = forwardRef<HTMLInputElement, Props>(
|
2022-02-18 16:02:27 -08:00
|
|
|
(
|
|
|
|
{ id, label, className, avatarUrl, displayName, onRemoveAvatar, ...rest },
|
|
|
|
ref
|
|
|
|
) => {
|
2022-10-10 09:19:10 -04:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
2022-02-18 16:02:27 -08:00
|
|
|
const [removed, setRemoved] = useState(false);
|
2022-08-09 11:44:46 +02:00
|
|
|
const [objUrl, setObjUrl] = useState<string>(null);
|
2022-02-18 16:02:27 -08:00
|
|
|
|
|
|
|
const fileInputRef = useObjectRef(ref);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-08-09 11:44:46 +02:00
|
|
|
const currentInput = fileInputRef.current;
|
|
|
|
|
|
|
|
const onChange = (e: Event) => {
|
|
|
|
const inputEvent = e as unknown as React.ChangeEvent<HTMLInputElement>;
|
|
|
|
if (inputEvent.target.files.length > 0) {
|
|
|
|
setObjUrl(URL.createObjectURL(inputEvent.target.files[0]));
|
2022-02-18 16:02:27 -08:00
|
|
|
setRemoved(false);
|
|
|
|
} else {
|
|
|
|
setObjUrl(null);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-09 11:44:46 +02:00
|
|
|
currentInput.addEventListener("change", onChange);
|
2022-02-18 16:02:27 -08:00
|
|
|
|
|
|
|
return () => {
|
2022-08-09 11:44:46 +02:00
|
|
|
currentInput?.removeEventListener("change", onChange);
|
2022-02-18 16:02:27 -08:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const onPressRemoveAvatar = useCallback(() => {
|
|
|
|
setRemoved(true);
|
|
|
|
onRemoveAvatar();
|
|
|
|
}, [onRemoveAvatar]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={classNames(styles.avatarInputField, className)}>
|
|
|
|
<div className={styles.avatarContainer}>
|
|
|
|
<Avatar
|
2022-08-09 11:44:46 +02:00
|
|
|
size={Size.XL}
|
2022-02-18 16:02:27 -08:00
|
|
|
src={removed ? null : objUrl || avatarUrl}
|
|
|
|
fallback={displayName.slice(0, 1).toUpperCase()}
|
|
|
|
/>
|
|
|
|
<input
|
|
|
|
id={id}
|
|
|
|
accept="image/png, image/jpeg"
|
|
|
|
ref={fileInputRef}
|
|
|
|
type="file"
|
|
|
|
className={styles.fileInput}
|
|
|
|
role="button"
|
|
|
|
aria-label={label}
|
|
|
|
{...rest}
|
|
|
|
/>
|
|
|
|
<label htmlFor={id} className={styles.fileInputButton}>
|
|
|
|
<EditIcon />
|
|
|
|
</label>
|
|
|
|
</div>
|
2022-03-02 19:18:23 -08:00
|
|
|
{(avatarUrl || objUrl) && !removed && (
|
|
|
|
<Button
|
|
|
|
className={styles.removeButton}
|
|
|
|
variant="icon"
|
|
|
|
onPress={onPressRemoveAvatar}
|
|
|
|
>
|
2022-10-10 09:19:10 -04:00
|
|
|
{t("Remove")}
|
2022-03-02 19:18:23 -08:00
|
|
|
</Button>
|
|
|
|
)}
|
2022-02-18 16:02:27 -08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|