Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: local provider not working when confirming modal #1300

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/semi-ui/locale/localeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { changeConfirmLocale } from '../modal/local';
import LocaleContext from './context';
import DefaultLocale from './source/zh_CN';
import { Locale } from './interface';
Expand All @@ -22,6 +23,17 @@ export default class LocaleProvider extends Component<LocaleProviderProps> {
constructor(props: LocaleProviderProps) {
super(props);
this.state = {};
changeConfirmLocale(props.locale?.Modal);
}

componentDidUpdate(prevProps: Readonly<LocaleProviderProps>): void {
if (prevProps.locale?.Modal !== this.props.locale?.Modal) {
changeConfirmLocale(this.props.locale.Modal);
}
}

componentWillUnmount(): void {
changeConfirmLocale();
}

render() {
Expand Down
8 changes: 4 additions & 4 deletions packages/semi-ui/modal/_story/modal.stories.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState } from 'react';
import en_GB from '../../locale/source/en_GB';

import { Select, Modal, Button, Tooltip, Popover, ConfigProvider, Tag, Space } from '../../index';
import { Select, Modal, Button, Tooltip, Popover, ConfigProvider, Tag, Space, LocaleProvider } from '../../index';
import CollapsibleInModal from './CollapsibleInModal';
import DynamicContextDemo from './DynamicContext';

Expand Down Expand Up @@ -88,7 +88,7 @@ function info() {
}

function error() {
Modal.error({ title: 'Unfortunately, there is an error', content: 'bla bla bla...' });
Modal.error({ title: 'Unfortunately, there is an error', content: 'bla bla bla...', okText: '好的' });
}

function warning() {
Expand All @@ -100,13 +100,13 @@ function confirm() {
}

export const ConfirmModal = () => (
<div>
<LocaleProvider locale={en_GB}>
<Button onClick={info}>Info</Button>
<Button onClick={success}>Success</Button>
<Button onClick={error}>Error</Button>
<Button onClick={warning}>Warning</Button>
<Button onClick={confirm}>Confirm</Button>
</div>
</LocaleProvider>
);

ConfirmModal.story = {
Expand Down
12 changes: 11 additions & 1 deletion packages/semi-ui/modal/confirm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { destroyFns, ModalReactProps } from './Modal';
import ConfirmModal from './ConfirmModal';
import { getConfirmLocale } from './local';

import '@douyinfe/semi-foundation/modal/modal.scss';
import { IconAlertCircle, IconAlertTriangle, IconHelpCircle, IconInfoCircle, IconTickCircle } from '@douyinfe/semi-icons';
Expand Down Expand Up @@ -36,7 +37,16 @@ export default function confirm<T>(props: ConfirmProps) {


function render(renderProps: ConfirmProps) {
ReactDOM.render(<ConfirmModal {...renderProps} motion={props.motion}/>, div);
const runtimeLocale = getConfirmLocale();
ReactDOM.render(
<ConfirmModal
okText={runtimeLocale.confirm}
cancelText={runtimeLocale.cancel}
{...renderProps}
motion={props.motion}
/>,
div
);
}

function close() {
Expand Down
27 changes: 27 additions & 0 deletions packages/semi-ui/modal/local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import defaultLocale from '../locale/source/zh_CN';

export interface ModalLocale {
confirm: string;
cancel: string
}

let runtimeLocale: ModalLocale = {
...defaultLocale.Modal,
};

export function changeConfirmLocale(newLocale?: ModalLocale) {
if (newLocale) {
runtimeLocale = {
...runtimeLocale,
...newLocale,
};
} else {
runtimeLocale = {
...defaultLocale.Modal,
};
}
}

export function getConfirmLocale() {
return runtimeLocale;
}