Skip to content

Commit dc20046

Browse files
committed
feat(modal): support modal locale
1 parent 5cd5c43 commit dc20046

File tree

7 files changed

+131
-24
lines changed

7 files changed

+131
-24
lines changed

src/configProvider/demos/basic.tsx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
11
import React, { useState } from 'react';
22
import { Radio, Space } from 'antd';
3-
import { BlockHeader, ConfigProvider, Copy, enUS, Input, zhCN } from 'dt-react-component';
3+
import {
4+
BlockHeader,
5+
Button,
6+
ConfigProvider,
7+
Copy,
8+
enUS,
9+
Input,
10+
Modal,
11+
zhCN,
12+
} from 'dt-react-component';
413

514
export default function Basic() {
615
const [locale, setLocale] = useState(zhCN);
716

17+
const info = () => {
18+
Modal.info({
19+
title: 'This is a notification message',
20+
content: (
21+
<div>
22+
<p>some messages...some messages...</p>
23+
<p>some messages...some messages...</p>
24+
</div>
25+
),
26+
onOk() {},
27+
});
28+
};
29+
30+
const handleDelete = () => {
31+
Modal.delete({
32+
title: 'This is a delete message',
33+
content: 'some messages...some messages...',
34+
});
35+
};
36+
837
return (
938
<Space direction="vertical" size="large">
1039
<Radio.Group
@@ -31,6 +60,13 @@ export default function Basic() {
3160
<h3>Input.Match 组件</h3>
3261
<Input.Match />
3362
</div>
63+
<div>
64+
<h3>Modal.method 组件</h3>
65+
<Button onClick={info} style={{ marginRight: 12 }}>
66+
Info
67+
</Button>
68+
<Button onClick={handleDelete}>Delete</Button>
69+
</div>
3470
</Space>
3571
</ConfigProvider>
3672
</Space>

src/configProvider/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22

33
import { Locale, LocaleContext } from '../locale/useLocale';
4+
import { changeConfirmLocale } from '../modal/locale';
45

56
const ConfigProvider = ({ locale, children }: { locale: Locale; children: React.ReactNode }) => {
7+
useEffect(() => {
8+
const clearLocale = changeConfirmLocale(locale?.Modal);
9+
return clearLocale;
10+
}, [locale]);
11+
612
return <LocaleContext.Provider value={{ locale }}>{children}</LocaleContext.Provider>;
713
};
814

src/locale/en-US.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ const localeValues: Locale = {
4646
front: 'Head match',
4747
tail: 'Tail match',
4848
},
49+
Modal: {
50+
okText: 'Ok',
51+
cancelText: 'Cancel',
52+
notYetText: 'Not Now',
53+
},
4954
NotFound: {
5055
description: 'Sorry, the page you visited does not exist',
5156
},

src/locale/useLocale.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export interface Locale {
3333
NotFound: {
3434
description: string;
3535
};
36+
Modal: {
37+
okText: string;
38+
cancelText: string;
39+
notYetText: string;
40+
};
3641
SpreadSheet: {
3742
description: string;
3843
copy: string;

src/locale/zh-CN.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ const localeValues: Locale = {
4242
front: '头部匹配',
4343
tail: '尾部匹配',
4444
},
45+
Modal: {
46+
okText: '确定',
47+
cancelText: '取消',
48+
notYetText: '暂不',
49+
},
4550
NotFound: {
4651
description: '抱歉,您访问的页面不存在',
4752
},

src/modal/index.tsx

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import { CheckFilled, CloseFilled, InformationFilled, WarningFilled } from '@dti
33
import { Modal as AntdModal, ModalFuncProps } from 'antd';
44
import { ModalFunc, ModalStaticFunctions } from 'antd/lib/modal/confirm';
55

6+
import { getRunTimeLocale, ModalLocale } from './locale';
67
import InternalModal from './modal';
78

8-
const OK_TEXT = '确认';
9-
109
type ModalType = typeof InternalModal &
1110
ModalStaticFunctions & {
1211
useModal: typeof AntdModal.useModal;
@@ -17,31 +16,42 @@ type ModalType = typeof InternalModal &
1716

1817
const { useModal, info, success, error, warning, confirm, destroyAll, config } = AntdModal;
1918

20-
const customProps: Record<
19+
const getCustomProps = (
20+
locale: ModalLocale
21+
): Record<
2122
| keyof Pick<ModalStaticFunctions, 'info' | 'success' | 'error' | 'warning' | 'confirm'>
2223
| 'delete',
2324
ModalFuncProps
24-
> = {
25-
info: { icon: <InformationFilled color="#1D78FF" />, okText: OK_TEXT },
26-
success: { icon: <CheckFilled color="#11D7B2" />, okText: OK_TEXT },
27-
error: {
28-
icon: <CloseFilled color="#F96C5B" />,
29-
okText: OK_TEXT,
30-
okButtonProps: { danger: true },
31-
},
32-
warning: { icon: <WarningFilled color="#FBB310" />, okText: OK_TEXT },
33-
confirm: { icon: <WarningFilled color="#FBB310" />, okText: OK_TEXT, cancelText: '取消' },
34-
delete: {
35-
icon: <CloseFilled color="#F96C5B" />,
36-
okButtonProps: { danger: true },
37-
okText: OK_TEXT,
38-
cancelText: '暂不',
39-
},
25+
> => {
26+
return {
27+
info: { icon: <InformationFilled color="#1D78FF" />, okText: locale.okText },
28+
success: { icon: <CheckFilled color="#11D7B2" />, okText: locale.okText },
29+
error: {
30+
icon: <CloseFilled color="#F96C5B" />,
31+
okText: locale.okText,
32+
okButtonProps: { danger: true },
33+
},
34+
warning: { icon: <WarningFilled color="#FBB310" />, okText: locale.okText },
35+
confirm: {
36+
icon: <WarningFilled color="#FBB310" />,
37+
okText: locale.okText,
38+
cancelText: locale.cancelText,
39+
},
40+
delete: {
41+
icon: <CloseFilled color="#F96C5B" />,
42+
okButtonProps: { danger: true },
43+
okText: locale.okText,
44+
cancelText: locale.notYetText,
45+
},
46+
};
4047
};
4148

42-
function withCustomIcon(fn: ModalFunc, type: keyof typeof customProps) {
43-
return (props: ModalFuncProps) =>
44-
fn({ ...customProps[type], wrapClassName: 'dtc-modal', ...props });
49+
function withCustomIcon(fn: ModalFunc, type: keyof ReturnType<typeof getCustomProps>) {
50+
return (props: ModalFuncProps) => {
51+
const locale = getRunTimeLocale();
52+
const customProps = getCustomProps(locale);
53+
return fn({ ...customProps[type], wrapClassName: 'dtc-modal', ...props });
54+
};
4555
}
4656

4757
const Modal = InternalModal as unknown as ModalType;

src/modal/locale.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import defaultLocale from '../locale/zh-CN';
2+
3+
export interface ModalLocale {
4+
okText: string;
5+
cancelText: string;
6+
notYetText: string;
7+
}
8+
9+
let runtimeLocale: ModalLocale = {
10+
...(defaultLocale.Modal as ModalLocale),
11+
};
12+
13+
let localeList: ModalLocale[] = [];
14+
15+
const generateLocale = () =>
16+
localeList.reduce<ModalLocale>(
17+
(merged, locale) => ({ ...merged, ...locale }),
18+
defaultLocale.Modal!
19+
);
20+
21+
export function changeConfirmLocale(newLocale?: ModalLocale) {
22+
if (newLocale) {
23+
const cloneLocale = { ...newLocale };
24+
localeList.push(cloneLocale);
25+
runtimeLocale = generateLocale();
26+
27+
return () => {
28+
localeList = localeList.filter((locale) => locale !== cloneLocale);
29+
runtimeLocale = generateLocale();
30+
};
31+
}
32+
33+
runtimeLocale = {
34+
...(defaultLocale.Modal as ModalLocale),
35+
};
36+
}
37+
38+
export const getRunTimeLocale = () => {
39+
return runtimeLocale;
40+
};

0 commit comments

Comments
 (0)