Skip to content

Commit

Permalink
feat(upload): 国际化支持upload,tree
Browse files Browse the repository at this point in the history
  • Loading branch information
lincao committed Jan 23, 2022
1 parent 8a9f9a8 commit 34ba53e
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 59 deletions.
30 changes: 29 additions & 1 deletion src/locale/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export default {
treeSelect: {
empty: 'Empty Data',
loadingText: 'loading...',
placeholder: '',
},
datePicker: {
placeholder: {
Expand Down Expand Up @@ -120,7 +121,34 @@ export default {
},
upload: {
sizeLimitMessage: 'File is too large to upload. {sizeLimit}',
cancelUploadText: 'Cancel',
trigger: {
file: 'Upload',
image: 'Click to upload',
cancel: 'Cancel',
},
dragger: {
drop: 'Drop hear',
click: 'Click',
drag: 'Drag file to this area to upload',
clickAndDrag: 'Click "Upload" or Drag file to this area to upload',
},
infoTable: {
name: 'filename',
size: 'size',
status: 'status',
operation: 'operation',
date: 'date',
},
progress: {
cancel: 'Cancel',
reupload: 'Reupload',
delete: 'Delete',
start: 'Upload',
uploading: 'Uploading',
waiting: 'Waiting',
fail: 'Failed',
success: 'Success',
},
},
tag: {
closeIcon: undefined,
Expand Down
39 changes: 39 additions & 0 deletions src/locale/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default {
select: {
empty: '暂无数据',
loadingText: '加载中',
placeholder: '-请选择-',
// 清除按钮
clearIcon: null,
},
Expand All @@ -76,6 +77,7 @@ export default {
treeSelect: {
empty: '暂无数据',
loadingText: '加载中',
placeholder: '请选择',
},
datePicker: {
placeholder: {
Expand Down Expand Up @@ -107,5 +109,42 @@ export default {
steps: {},
upload: {
sizeLimitMessage: '图片大小不能超过 {sizeLimit}',
trigger: {
file: '选择文件',
image: '点击上传图片',
cancel: '取消',
},
dragger: {
drop: '释放鼠标',
click: '点击上传',
drag: '拖拽到此区域',
clickAndDrag: '点击上方“选择文件”或将文件拖拽到此区域',
},
infoTable: {
name: '文件名',
size: '文件大小',
status: '状态',
operation: '操作',
date: '上传日期',
},
progress: {
start: '点击上传',
cancel: '取消上传',
reupload: '重新上传',
delete: '删除',
uploading: '上传中',
waiting: '待上传',
fail: '上传失败',
success: '上传成功',
},
},
tag: {
closeIcon: undefined,
},
form: {
requiredMark: true,
},
input: {
placeholder: '',
},
};
8 changes: 5 additions & 3 deletions src/select/base/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const Select = forwardRefWithStatics(
panelTopContent,
} = useDefaultValue(props);

const { classPrefix } = useConfig();
const { classPrefix, locale } = useConfig();

const name = `${classPrefix}-select`; // t-select

Expand Down Expand Up @@ -268,7 +268,7 @@ const Select = forwardRefWithStatics(
},
)}
>
{selectedLabel || placeholder || '-请选择-'}
{selectedLabel || placeholder || locale.select.placeholder}
</span>
);

Expand Down Expand Up @@ -320,7 +320,9 @@ const Select = forwardRefWithStatics(
const renderInput = () => (
<Input
value={isString(inputVal) || multiple ? inputVal : selectedLabel}
placeholder={multiple && get(value, 'length') > 0 ? null : selectedLabel || placeholder || '-请选择-'}
placeholder={
multiple && get(value, 'length') > 0 ? null : selectedLabel || placeholder || locale.select.placeholder
}
className={`${name}__input`}
onChange={handleInputChange}
onKeydown={handleInputKeyDown}
Expand Down
14 changes: 8 additions & 6 deletions src/tree-select/TreeSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ export interface NodeOptions {
}

const TreeSelect = forwardRef((props: TreeSelectProps, ref: React.Ref<HTMLDivElement>) => {
// 国际化文本初始化
const [local, t] = useLocaleReceiver('treeSelect');
const emptyText = t(local.empty);
const placeholderText = t(local.placeholder);
const loadingTextLabel = t(local.loadingText);

const {
className: treeSelectClassName,
style: treeSelectStyle,
Expand All @@ -36,6 +42,7 @@ const TreeSelect = forwardRef((props: TreeSelectProps, ref: React.Ref<HTMLDivEle
valueType,
loading,
max,
placeholder = placeholderText,
treeProps,
empty,
data,
Expand Down Expand Up @@ -232,11 +239,6 @@ const TreeSelect = forwardRef((props: TreeSelectProps, ref: React.Ref<HTMLDivEle
}
}

// 国际化文本初始化
const [local, t] = useLocaleReceiver('treeSelect');
const emptyText = t(local.empty);
const loadingTextLabel = t(local.loadingText);

const treeItem = !loading && (
<Tree
ref={treeRef}
Expand Down Expand Up @@ -304,6 +306,7 @@ const TreeSelect = forwardRef((props: TreeSelectProps, ref: React.Ref<HTMLDivEle
selectedSingle={selectedSingle}
setFocusing={setFocusing}
setFilterText={setFilterText}
placeholder={placeholder}
{...props}
/>

Expand Down Expand Up @@ -332,7 +335,6 @@ TreeSelect.defaultProps = {
loadingText: '',
max: 0,
multiple: false,
placeholder: '请输入',
size: 'medium',
valueType: 'value',
minCollapsedNum: 0,
Expand Down
1 change: 0 additions & 1 deletion src/tree-select/_example/base.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export default function Example() {
<TreeSelect
data={options}
clearable
placeholder="请选择"
value={value}
// defaultValue="guangdong"
onChange={(val) => {
Expand Down
4 changes: 2 additions & 2 deletions src/tree/TreeItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const TreeItem = forwardRef((props: TreeItemProps, ref: React.Ref<HTMLDivElement
} = props;
const { level } = node;

const { treeClassNames } = useTreeConfig();
const { treeClassNames, locale } = useTreeConfig();

const handleClick = (evt: MouseEvent<HTMLDivElement>) => {
onClick?.(node, {
Expand Down Expand Up @@ -159,7 +159,7 @@ const TreeItem = forwardRef((props: TreeItemProps, ref: React.Ref<HTMLDivElement
useRipple(labelRef);

const renderLabel = () => {
const emptyView = '暂无数据';
const emptyView = locale.empty;
let labelText: string | ReactNode = '';
if (label instanceof Function) {
labelText = label(node.getModel()) || emptyView;
Expand Down
5 changes: 3 additions & 2 deletions src/tree/useTreeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useMemo } from 'react';
import useConfig from '../_util/useConfig';

export function useTreeConfig() {
const { classPrefix: prefix } = useConfig();
const { classPrefix: prefix, locale } = useConfig();

return useMemo(() => {
const tree = `${prefix}-tree`;
Expand Down Expand Up @@ -54,6 +54,7 @@ export function useTreeConfig() {
transitionNames,
transitionClassNames,
transitionDuration: 300,
locale: locale.tree,
};
}, [prefix]);
}, [locale.tree, prefix]);
}
6 changes: 3 additions & 3 deletions src/upload/__tests__/__snapshots__/upload.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ exports[`file-flow-list.jsx 1`] = `
文件名
</th>
<th>
大小
文件大小
</th>
<th>
状态
Expand Down Expand Up @@ -332,7 +332,7 @@ exports[`file-flow-list.jsx 1`] = `
<span
class="t-button__text"
>
开始上传
点击上传
</span>
</button>
</div>
Expand Down Expand Up @@ -620,7 +620,7 @@ exports[`img-flow-list.jsx 1`] = `
<span
class="t-button__text"
>
开始上传
点击上传
</span>
</button>
</div>
Expand Down
11 changes: 6 additions & 5 deletions src/upload/dragger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ export interface DraggerProps {

const Dragger: FC<DraggerProps> = (props) => {
const { file, display, onUpload, onRemove, customDraggerRender } = props;
const { classPrefix } = useConfig();
const { classPrefix, locale } = useConfig();
const [dragActive, setDragActive] = useState(false);
const target = React.useRef();
const draggerLocaleText = locale.upload.dragger;

const classes = classNames(
`${classPrefix}-upload__dragger`,
Expand All @@ -35,13 +36,13 @@ const Dragger: FC<DraggerProps> = (props) => {
const defaultDragElement = React.useMemo(() => {
const unActiveElement = (
<div>
<span className={`${classPrefix}-upload--highlight`}>点击上传</span>
<span>&nbsp;&nbsp;/&nbsp;&nbsp;拖拽到此区域</span>
<span className={`${classPrefix}-upload--highlight`}>{draggerLocaleText.click}</span>
<span>&nbsp;&nbsp;/&nbsp;&nbsp;{draggerLocaleText.drag}</span>
</div>
);
const activeElement = <div>释放鼠标</div>;
const activeElement = <div>{draggerLocaleText.drop}</div>;
return dragActive ? activeElement : unActiveElement;
}, [classPrefix, dragActive]);
}, [classPrefix, dragActive, draggerLocaleText]);

const dragElement = React.useMemo(() => {
let content: React.ReactNode;
Expand Down
19 changes: 12 additions & 7 deletions src/upload/themes/dragger-progress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ export interface DraggerProgressProps {

const DraggerProgress: FC<DraggerProgressProps> = (props) => {
const { file, onUpload, onRemove, display, onTrigger } = props;
const { classPrefix } = useConfig();
const { classPrefix, locale } = useConfig();

const { progress: progressText, infoTable: infoText } = locale.upload;
const reUpload = (e) => {
onRemove?.({ e, file, index: 0 });
onTrigger?.();
Expand Down Expand Up @@ -54,8 +55,12 @@ const DraggerProgress: FC<DraggerProgressProps> = (props) => {
{file?.status !== 'success' && renderUploading()}
{file?.status === 'success' && <CheckCircleFilledIcon />}
</div>
<small className={`${classPrefix}-size-s`}>文件大小:{returnFileSize(file?.size)}</small>
<small className={`${classPrefix}-size-s`}>上传日期:{getCurrentDate()}</small>
<small className={`${classPrefix}-size-s`}>
{infoText.size}{returnFileSize(file?.size)}
</small>
<small className={`${classPrefix}-size-s`}>
{infoText.date}{getCurrentDate()}
</small>
{!['success', 'fail'].includes(file?.status) && (
<div className={`${classPrefix}-upload__dragger-btns`}>
<Button
Expand All @@ -64,20 +69,20 @@ const DraggerProgress: FC<DraggerProgressProps> = (props) => {
className={`${classPrefix}-upload__dragger-progress-cancel`}
onClick={handleRemove}
>
取消上传
{progressText.cancel}
</Button>
<Button theme="primary" variant="text" onClick={onUpload}>
点击上传
{progressText.start}
</Button>
</div>
)}
{showResultOperate && (
<div className="t-upload__dragger-btns">
<Button theme="primary" variant="text" className="t-upload__dragger-progress-cancel" onClick={reUpload}>
重新上传
{progressText.reupload}
</Button>
<Button theme="primary" variant="text" onClick={handleRemove}>
删除
{progressText.delete}
</Button>
</div>
)}
Expand Down
22 changes: 12 additions & 10 deletions src/upload/themes/flow-list/file-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,27 @@ type FileListProps = CommonListProps & Pick<FlowListProps, 'showUploadProgress'

const FileList = (props: FileListProps) => {
const { listFiles, showInitial, renderDragger, showUploadProgress, remove } = props;
const { classPrefix: prefix } = useConfig();
const { classPrefix: prefix, locale } = useConfig();
const UPLOAD_NAME = `${prefix}-upload`;
const { progress: progressText, infoTable: infoText } = locale.upload;

const renderStatus = (file: UploadFile) => {
const STATUS_MAP = {
success: {
icon: <CheckCircleFilledIcon />,
text: '上传成功',
text: progressText.success,
},
fail: {
icon: <ErrorCircleFilledIcon />,
text: '上传失败',
text: progressText.fail,
},
progress: {
icon: <Loading />,
text: `上传中 ${Math.min(file.percent, 99)}%`,
text: `${progressText.uploading} ${Math.min(file.percent, 99)}%`,
},
waiting: {
icon: <TimeFilledIcon />,
text: '待上传',
text: progressText.waiting,
},
};

Expand All @@ -45,10 +47,10 @@ const FileList = (props: FileListProps) => {
<table className={`${UPLOAD_NAME}__flow-table`}>
<thead>
<tr>
<th>文件名</th>
<th>大小</th>
<th>状态</th>
<th>操作</th>
<th>{infoText.name}</th>
<th>{infoText.size}</th>
<th>{infoText.status}</th>
<th>{infoText.operation}</th>
</tr>
</thead>
<tbody>
Expand All @@ -67,7 +69,7 @@ const FileList = (props: FileListProps) => {
className={`${UPLOAD_NAME}__flow-button`}
onClick={(e: MouseEvent<HTMLElement>) => remove({ e, index, file })}
>
删除
{progressText.delete}
</span>
</td>
</tr>
Expand Down
Loading

0 comments on commit 34ba53e

Please sign in to comment.