Skip to content

Commit 3ecf41c

Browse files
author
Zabilsya
committed
[DOP-24079] add file size select
1 parent 48a5a33 commit 3ecf41c

File tree

14 files changed

+161
-23
lines changed

14 files changed

+161
-23
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/** Cross-import entities using public API in FSD https://feature-sliced.design/ru/docs/reference/public-api#public-api-for-cross-imports */
2+
export { FileSizeUnit } from '../types';
3+
export { parseFileSize } from '../utils';

src/entities/file/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
export enum FileSizeUnit {
2+
b = 'b',
3+
Kb = 'Kb',
4+
Mb = 'Mb',
5+
Gb = 'Gb',
6+
}
7+
8+
export enum FileSizeUnitValue {
9+
b = 1,
10+
Kb = 1000,
11+
Mb = 1000 * 1000,
12+
Gb = 1000 * 1000 * 1000,
13+
}
14+
115
export enum FileCompression {
216
NONE = 'none',
317
BZIP2 = 'bzip2',

src/entities/file/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './parseFileSize';
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { FileSizeUnit, FileSizeUnitValue } from '@entities/file';
2+
3+
import { ParseFileSizeReturn } from './types';
4+
5+
/** Util for parsing file size in bytes to appropriate unit and value */
6+
export const parseFileSize = (bytes: number): ParseFileSizeReturn => {
7+
if (bytes >= FileSizeUnitValue.Gb) {
8+
return { value: bytes / FileSizeUnitValue.Gb, unit: FileSizeUnit.Gb };
9+
}
10+
if (bytes >= FileSizeUnitValue.Mb) {
11+
return { value: bytes / FileSizeUnitValue.Mb, unit: FileSizeUnit.Mb };
12+
}
13+
if (bytes >= FileSizeUnitValue.Kb) {
14+
return { value: bytes / FileSizeUnitValue.Kb, unit: FileSizeUnit.Kb };
15+
}
16+
return { value: bytes, unit: FileSizeUnit.b };
17+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { FileSizeUnit } from '@entities/file/@x/transformation';
2+
3+
export interface ParseFileSizeReturn {
4+
value: number;
5+
unit: FileSizeUnit;
6+
}

src/entities/transformation/ui/TransformationForm/components/FilterFileValue/constants.ts renamed to src/entities/transformation/ui/TransformationForm/components/FilterFileValue/components/FilterFileRegexpValue/constants.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import { TransformationFilterFileType } from '@entities/transformation';
2-
import { NUMBER_REGEXP } from '@shared/constants';
32
import { isValidRegex } from '@shared/utils';
4-
import { RuleObject } from 'antd/lib/form';
53

64
/** Regexp to search file with global magic characters (e.g. *.svg) */
75
export const NAME_GLOB_REGEXP = /([*?[])/;
86

9-
export const CONTROL_RULES: Record<TransformationFilterFileType, RuleObject[]> = {
7+
export const CONTROL_RULES = {
108
[TransformationFilterFileType.NAME_GLOB]: [{ required: true, pattern: NAME_GLOB_REGEXP }],
119
[TransformationFilterFileType.NAME_REGEXP]: [{ required: true, validator: isValidRegex }],
12-
[TransformationFilterFileType.FILE_SIZE_MIN]: [{ required: true, pattern: NUMBER_REGEXP }],
13-
[TransformationFilterFileType.FILE_SIZE_MAX]: [{ required: true, pattern: NUMBER_REGEXP }],
1410
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
import { Form, Input } from 'antd';
3+
4+
import { FilterFileRegexpValueProps } from './types';
5+
import classes from './styles.module.less';
6+
import { CONTROL_RULES } from './constants';
7+
8+
export const FilterFileRegexpValue = ({ name, type }: FilterFileRegexpValueProps) => {
9+
return (
10+
<Form.Item className={classes.control} label="Value" name={[name, 'value']} rules={CONTROL_RULES[type]}>
11+
<Input className="nodrag" size="large" />
12+
</Form.Item>
13+
);
14+
};

src/entities/transformation/ui/TransformationForm/components/FilterFileValue/styles.module.less renamed to src/entities/transformation/ui/TransformationForm/components/FilterFileValue/components/FilterFileRegexpValue/styles.module.less

File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { TransformationFilterFileType } from '@entities/transformation';
2+
3+
export interface FilterFileRegexpValueProps {
4+
type: Extract<
5+
TransformationFilterFileType,
6+
TransformationFilterFileType.NAME_GLOB | TransformationFilterFileType.NAME_REGEXP
7+
>;
8+
name: number;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { FileSizeUnit } from '@entities/file/@x/transformation';
2+
import { prepareOptionsForSelect } from '@shared/ui';
3+
4+
export const FILE_SIZE_UNIT_SELECT_OPTIONS = prepareOptionsForSelect({
5+
data: Object.values(FileSizeUnit),
6+
renderLabel: (data) => data,
7+
renderValue: (data) => data,
8+
});

0 commit comments

Comments
 (0)