Skip to content

Commit 2949b1a

Browse files
authored
feat: support locale prop (#13)
* feat: support `locale` field * test: add unit test * 国际化 * Revert "国际化" This reverts commit 8353b77.
1 parent 0675e8d commit 2949b1a

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

packages/antd-record-hotkey-input/src/RecordShortcutInput.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,25 @@ function RecordShortcutInput(props: RecordShortcutInputProps, ref: React.Forward
5555
formatShortcut = defaultFormatShortcut,
5656
onConfirm,
5757
recordOption,
58+
locale,
5859
...restProps
5960
} = props;
60-
const t = useIntl().getMessage;
61-
6261
const ctxDisabled = React.useContext(DisabledContext);
6362
const mergedDisabled = disabled ?? ctxDisabled;
6463

6564
const { getPrefixCls } = React.useContext(ConfigContext);
6665

66+
const intl = useIntl();
67+
const t = React.useCallback<(typeof intl)['getMessage']>(
68+
(id, defaultMessage) => {
69+
const _id = id.replace(/^ShortcutInput\./, '') as keyof typeof locale;
70+
// eslint-disable-next-line eqeqeq
71+
if (locale?.[_id] != null) return locale[_id];
72+
return intl.getMessage(id, defaultMessage);
73+
},
74+
[intl, locale],
75+
);
76+
6777
const [value, setValue] = useMergedState<string>('', {
6878
value: propValue,
6979
defaultValue: propDefaultValue,

packages/antd-record-hotkey-input/src/__tests__/input.spec.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,16 @@ describe('RecordShortcutInput', () => {
133133

134134
expect(getByRole('textbox')).toHaveAttribute('placeholder', 'Double click to edit');
135135
});
136+
137+
it('custom locale', () => {
138+
const { getByRole } = render(
139+
<RecordShortcutInput
140+
locale={{
141+
placeholder: 'foo',
142+
}}
143+
/>,
144+
);
145+
expect(getByRole('textbox')).toHaveAttribute('placeholder', 'foo');
146+
});
136147
});
137148
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import en_US from './en_US';
2+
3+
export type LocaleType = typeof en_US;
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export default {
1+
import type { LocaleType } from './type';
2+
3+
const locale: LocaleType = {
24
ShortcutInput: {
35
edit: '编辑',
46
clear: '清除',
@@ -7,3 +9,5 @@ export default {
79
recordingPlaceholder: '按任意组合键',
810
},
911
};
12+
13+
export default locale;

packages/antd-record-hotkey-input/src/type.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { InputProps } from 'antd';
22
import type { Options } from 'react-use-record-hotkey';
3+
import type { LocaleType } from './locale/type';
34

45
type MaybeFunction<T> = T | ((isRecording: boolean) => T);
56

@@ -31,4 +32,8 @@ export interface RecordShortcutInputProps extends RealInputProps, ShortcutStanda
3132
* @since 1.2.0
3233
*/
3334
recordOption?: Omit<Options, 'onClean' | 'onConfirm'>;
35+
/**
36+
* @since 1.3.0
37+
*/
38+
locale?: Partial<LocaleType['ShortcutInput']>;
3439
}

0 commit comments

Comments
 (0)