chore: Update Locale context (#22762)

* Upgrade locale context

* fix logic

* fix lint

* add context ignore in test case
This commit is contained in:
二货机器人 2020-03-30 22:54:12 +08:00 committed by GitHub
parent dc4d98a46a
commit cefb7b29f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 32 deletions

View File

@ -1,6 +1,6 @@
import * as React from 'react'; import * as React from 'react';
import * as PropTypes from 'prop-types';
import defaultLocaleData from './default'; import defaultLocaleData from './default';
import LocaleContext from './context';
export interface LocaleReceiverProps { export interface LocaleReceiverProps {
componentName?: string; componentName?: string;
@ -21,17 +21,13 @@ export default class LocaleReceiver extends React.Component<LocaleReceiverProps>
componentName: 'global', componentName: 'global',
}; };
static contextTypes = { static contextType = LocaleContext;
antLocale: PropTypes.object,
};
context: LocaleReceiverContext;
getLocale() { getLocale() {
const { componentName, defaultLocale } = this.props; const { componentName, defaultLocale } = this.props;
const locale: object | Function = const locale: object | Function =
defaultLocale || (defaultLocaleData as LocaleInterface)[componentName || 'global']; defaultLocale || (defaultLocaleData as LocaleInterface)[componentName || 'global'];
const { antLocale } = this.context; const antLocale = this.context;
const localeFromContext = componentName && antLocale ? antLocale[componentName] : {}; const localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
return { return {
...(typeof locale === 'function' ? locale() : locale), ...(typeof locale === 'function' ? locale() : locale),
@ -40,7 +36,7 @@ export default class LocaleReceiver extends React.Component<LocaleReceiverProps>
} }
getLocaleCode() { getLocaleCode() {
const { antLocale } = this.context; const antLocale = this.context;
const localeCode = antLocale && antLocale.locale; const localeCode = antLocale && antLocale.locale;
// Had use LocaleProvide but didn't set locale // Had use LocaleProvide but didn't set locale
if (antLocale && antLocale.exist && !localeCode) { if (antLocale && antLocale.exist && !localeCode) {
@ -50,6 +46,6 @@ export default class LocaleReceiver extends React.Component<LocaleReceiverProps>
} }
render() { render() {
return this.props.children(this.getLocale(), this.getLocaleCode(), this.context.antLocale); return this.props.children(this.getLocale(), this.getLocaleCode(), this.context);
} }
} }

View File

@ -0,0 +1,6 @@
import { createContext } from 'react';
import { Locale } from '.';
const LocaleContext = createContext<(Partial<Locale> & { exist?: boolean }) | undefined>(undefined);
export default LocaleContext;

View File

@ -1,5 +1,4 @@
import * as React from 'react'; import * as React from 'react';
import * as PropTypes from 'prop-types';
import warning from '../_util/warning'; import warning from '../_util/warning';
import { ModalLocale, changeConfirmLocale } from '../modal/locale'; import { ModalLocale, changeConfirmLocale } from '../modal/locale';
@ -10,6 +9,7 @@ import { PopconfirmLocale } from '../popconfirm';
import { UploadLocale } from '../upload/interface'; import { UploadLocale } from '../upload/interface';
import { TransferLocale } from '../transfer'; import { TransferLocale } from '../transfer';
import { PickerLocale as DatePickerLocale } from '../date-picker/generatePicker'; import { PickerLocale as DatePickerLocale } from '../date-picker/generatePicker';
import LocaleContext from './context';
export const ANT_MARK = 'internalMark'; export const ANT_MARK = 'internalMark';
@ -43,10 +43,6 @@ export default class LocaleProvider extends React.Component<LocaleProviderProps,
locale: {}, locale: {},
}; };
static childContextTypes = {
antLocale: PropTypes.object,
};
constructor(props: LocaleProviderProps) { constructor(props: LocaleProviderProps) {
super(props); super(props);
changeConfirmLocale(props.locale && props.locale.Modal); changeConfirmLocale(props.locale && props.locale.Modal);
@ -58,15 +54,6 @@ export default class LocaleProvider extends React.Component<LocaleProviderProps,
); );
} }
getChildContext() {
return {
antLocale: {
...this.props.locale,
exist: true,
},
};
}
componentDidUpdate(prevProps: LocaleProviderProps) { componentDidUpdate(prevProps: LocaleProviderProps) {
const { locale } = this.props; const { locale } = this.props;
if (prevProps.locale !== locale) { if (prevProps.locale !== locale) {
@ -79,6 +66,10 @@ export default class LocaleProvider extends React.Component<LocaleProviderProps,
} }
render() { render() {
return this.props.children; const { locale, children } = this.props;
return (
<LocaleContext.Provider value={{ ...locale, exist: true }}>{children}</LocaleContext.Provider>
);
} }
} }

View File

@ -6,10 +6,7 @@ function getFileName(filePath) {
return filePath.slice(filePath.lastIndexOf(path.sep) + 1); return filePath.slice(filePath.lastIndexOf(path.sep) + 1);
} }
$('lib') $('lib').isDirectory().hasFile('index.js').hasFile('index.d.ts');
.isDirectory()
.hasFile('index.js')
.hasFile('index.d.ts');
$('lib/*') $('lib/*')
.filter( .filter(
@ -27,9 +24,7 @@ $('lib/*')
.hasFile('index.d.ts') .hasFile('index.d.ts')
.hasDirectory('style'); .hasDirectory('style');
$('lib/*/style') $('lib/*/style').hasFile('css.js').hasFile('index.js');
.hasFile('css.js')
.hasFile('index.js');
// locale // locale
const filterLocaleFile = filePath => { const filterLocaleFile = filePath => {
@ -40,7 +35,8 @@ const filterLocaleFile = filePath => {
!fileName.endsWith('.map') && !fileName.endsWith('.map') &&
!fileName.endsWith('style') && !fileName.endsWith('style') &&
!fileName.includes('-') && !fileName.includes('-') &&
!fileName.endsWith('LocaleReceiver.js') !fileName.endsWith('LocaleReceiver.js') &&
!fileName.endsWith('context.js')
); );
}; };
const localeFiles = $('lib/locale/*').filter(filterLocaleFile); const localeFiles = $('lib/locale/*').filter(filterLocaleFile);