Skip to content

Commit

Permalink
Merge pull request #119 from MapColonies/fix_file_picker
Browse files Browse the repository at this point in the history
fix: file picker
  • Loading branch information
EllaMartirosyan authored Feb 20, 2022
2 parents 284abff + 2ca12de commit 8010185
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
6 changes: 6 additions & 0 deletions packages/react-components/src/lib/file-picker/file-picker.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ body[dir='rtl'] .chonky-fileListWrapper [class^='listContainer-'] {
direction: rtl !important;
}

body[dir='rtl']
.chonky-fileEntryClickableWrapper
[class^='listFileEntryProperty-'] {
direction: ltr !important;
}

body[dir='rtl'] .chonky-chonkyRoot {
text-align: right;
}
Expand Down
6 changes: 4 additions & 2 deletions packages/react-components/src/lib/file-picker/file-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export type FileArray = ChonkyFileArray;

export type FileData = ChonkyFileData;

export type FilePickerAction = FileAction;

export class FileHelper extends ChonkyFileHelper {}

// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down Expand Up @@ -124,7 +126,7 @@ export const FilePicker: React.FC<FilePickerProps> = React.memo(
const [disableDragAndDrop, setDisableDragAndDrop] = useState<boolean>(
false
);
const [fileActions, setFileActions] = useState<FileAction[]>();
const [fileActions, setFileActions] = useState<FilePickerAction[]>();
const [i18n, setI18n] = useState<I18nConfig>();
useEffect(() => {
if (theme) {
Expand Down Expand Up @@ -158,7 +160,7 @@ export const FilePicker: React.FC<FilePickerProps> = React.memo(
ref={ref}
files={files ?? []}
folderChain={folderChain}
onFileAction={(data: ChonkyFileActionData): void => {
onFileAction={(data: FileActionData): void => {
if (typeof onFileAction === 'function') {
void onFileAction(data);
}
Expand Down
1 change: 1 addition & 0 deletions packages/react-components/src/lib/file-picker/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './file-picker';
export * from './localization';
41 changes: 34 additions & 7 deletions packages/react-components/src/lib/file-picker/localization.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
/* eslint-disable */
import { ChonkyActions, FileData, FileHelper, I18nConfig } from 'chonky';
import {
ChonkyActions,
ChonkyFormatters,
FileData,
FileHelper,
I18nConfig,
} from 'chonky';
import { IntlShape } from 'react-intl';
import filesize from 'filesize';
import { SupportedLocales } from '../models';

interface ILocalization {
[key: string]: I18nConfig;
export interface FilePickerFormatters extends ChonkyFormatters {
formatFileSize: (
intl: IntlShape | null,
file: FileData | null
) => string | null;
}

const defaultFormatters = {
interface IFileSize {
value: number;
symbol: string;
exponent: number;
unit: string;
}

export const defaultFormatters: FilePickerFormatters = {
formatFileModDate: (
intl: IntlShape,
file: FileData | null
Expand All @@ -23,20 +39,31 @@ const defaultFormatters = {
return null;
}
},
formatFileSize: (intl: IntlShape, file: FileData | null): string | null => {
formatFileSize: (
_intl: IntlShape | null,
file: FileData | null
): string | null => {
if (!file || typeof file.size !== 'number') return null;

const size = file.size;
const sizeData = filesize(size, { bits: false, output: 'object' }) as any;
const sizeData = (filesize(size, {
bits: false,
output: 'object',
}) as unknown) as IFileSize;
if (sizeData.symbol === 'B') {
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
return `${Math.round(sizeData.value / 10) / 100.0} KB`;
} else if (sizeData.symbol === 'KB') {
return `${Math.round(sizeData.value)} ${sizeData.symbol}`;
}
return `${sizeData.value} ${sizeData.symbol}`;
return `${sizeData.value} ${sizeData.symbol.toUpperCase()}`;
},
};

interface ILocalization {
[key: string]: I18nConfig;
}

const englishI18n: I18nConfig = {
locale: SupportedLocales.EN,
formatters: { ...defaultFormatters },
Expand Down

0 comments on commit 8010185

Please sign in to comment.