💄 front: fix rename dialog for keyboard use
This commit is contained in:
@@ -4,7 +4,7 @@ import { Input } from '@atoms/input/input-text';
|
|||||||
import { Modal, ModalContent } from '@atoms/modal';
|
import { Modal, ModalContent } from '@atoms/modal';
|
||||||
import { useDriveActions } from '@features/drive/hooks/use-drive-actions';
|
import { useDriveActions } from '@features/drive/hooks/use-drive-actions';
|
||||||
import { useDriveItem } from '@features/drive/hooks/use-drive-item';
|
import { useDriveItem } from '@features/drive/hooks/use-drive-item';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
import { atom, useRecoilState } from 'recoil';
|
import { atom, useRecoilState } from 'recoil';
|
||||||
import Languages from '@features/global/services/languages-service';
|
import Languages from '@features/global/services/languages-service';
|
||||||
|
|
||||||
@@ -38,15 +38,46 @@ const PropertiesModalContent = ({ id, onClose }: { id: string; onClose: () => vo
|
|||||||
const { update } = useDriveActions();
|
const { update } = useDriveActions();
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [name, setName] = useState('');
|
const [name, setName] = useState('');
|
||||||
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
refresh(id);
|
refresh(id);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setTimeout(() => {
|
||||||
|
if (inputRef.current) {
|
||||||
|
inputRef.current.focus();
|
||||||
|
const lastDot = inputRef.current.value.lastIndexOf('.');
|
||||||
|
const endRange = lastDot >= 0 ? lastDot : inputRef.current.value.length;
|
||||||
|
inputRef.current.setSelectionRange(0, endRange);
|
||||||
|
}
|
||||||
|
}, 100);
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!name) setName(item?.name || '');
|
if (!name) setName(item?.name || '');
|
||||||
}, [item?.name]);
|
}, [item?.name]);
|
||||||
|
|
||||||
|
const doSave = async () => {
|
||||||
|
setLoading(true);
|
||||||
|
if (item) {
|
||||||
|
let finalName = name;
|
||||||
|
//TODO: Confirm rename if extension changed ?
|
||||||
|
if (!item?.is_directory) {
|
||||||
|
//TODO: Why do we trim extensions on folders ?
|
||||||
|
const lastDotIndex = finalName.lastIndexOf('.');
|
||||||
|
if (lastDotIndex !== -1) {
|
||||||
|
const fileExtension = name.slice(lastDotIndex);
|
||||||
|
finalName = finalName.slice(0, lastDotIndex) + fileExtension;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await update({ name: finalName }, id, item.parent_id);
|
||||||
|
}
|
||||||
|
onClose();
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ModalContent
|
<ModalContent
|
||||||
title={Languages.t('components.PropertiesModalContent_rename') + ' ' + item?.name}
|
title={Languages.t('components.PropertiesModalContent_rename') + ' ' + item?.name}
|
||||||
@@ -57,7 +88,16 @@ const PropertiesModalContent = ({ id, onClose }: { id: string; onClose: () => vo
|
|||||||
input={
|
input={
|
||||||
<Input
|
<Input
|
||||||
value={name}
|
value={name}
|
||||||
|
inputRef={inputRef}
|
||||||
onChange={e => setName(e.target.value)}
|
onChange={e => setName(e.target.value)}
|
||||||
|
onKeyUp={({ key }) => {
|
||||||
|
if (!loading) {
|
||||||
|
if (key === 'Enter')
|
||||||
|
doSave();
|
||||||
|
else if (key === "Escape")
|
||||||
|
onClose();
|
||||||
|
}
|
||||||
|
}}
|
||||||
placeholder={Languages.t('components.PropertiesModalContent_place_holder')}
|
placeholder={Languages.t('components.PropertiesModalContent_place_holder')}
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
@@ -68,22 +108,7 @@ const PropertiesModalContent = ({ id, onClose }: { id: string; onClose: () => vo
|
|||||||
className="float-right mt-4"
|
className="float-right mt-4"
|
||||||
theme="primary"
|
theme="primary"
|
||||||
loading={loading}
|
loading={loading}
|
||||||
onClick={async () => {
|
onClick={doSave}
|
||||||
setLoading(true);
|
|
||||||
if (item) {
|
|
||||||
let finalName = name;
|
|
||||||
if (!item?.is_directory) {
|
|
||||||
const lastDotIndex = finalName.lastIndexOf('.');
|
|
||||||
if (lastDotIndex !== -1) {
|
|
||||||
const fileExtension = name.slice(lastDotIndex);
|
|
||||||
finalName = finalName.slice(0, lastDotIndex) + fileExtension;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
await update({ name: finalName }, id, item.parent_id);
|
|
||||||
}
|
|
||||||
onClose();
|
|
||||||
setLoading(false);
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
{Languages.t('components.PropertiesModalContent_update_button')}
|
{Languages.t('components.PropertiesModalContent_update_button')}
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
Reference in New Issue
Block a user