Skip to content

Commit

Permalink
chore: hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
BANG88 committed Dec 2, 2018
1 parent c7968d1 commit cce261f
Show file tree
Hide file tree
Showing 5 changed files with 382 additions and 345 deletions.
3 changes: 2 additions & 1 deletion components/locale-provider/demo/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import esES from '../es_ES';
import enUS from '../en_US';
import ruRU from '../ru_RU';
import zhCN from '../zh_CN';

const maxDate = new Date(2018, 11, 3, 22, 0);
const minDate = new Date(2015, 7, 6, 8, 30);
Expand Down Expand Up @@ -76,7 +77,7 @@ export default class LocaleProviderExample extends React.Component<any, any> {
{
value: '中国',
label: '中国',
language: undefined,
language: zhCN,
},
{
value: 'English',
Expand Down
15 changes: 10 additions & 5 deletions components/locale-provider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@ export interface Locale {

export type LocaleValue = Locale & { locale: string };
export interface LocaleProviderProps {
value: Locale & { locale: string };
value?: LocaleValue;
children?: React.ReactNode;
}

export const LocaleProvider = (props: LocaleProviderProps) => {
return <LocaleContext.Provider {...props} />;
export const LocaleProvider = (props: LocaleProviderProps = {}) => {
const locale = deepmerge(en_US, props.value || {});
return (
<LocaleContext.Provider value={locale}>
{props.children}
</LocaleContext.Provider>
);
};

export const useLocale = (locale: Partial<Locale> = {}) => {
const l = useContext(LocaleContext);
return deepmerge(l, locale);
const currentLocale = useContext(LocaleContext);
return deepmerge(currentLocale, locale);
};

export default LocaleProvider;
4 changes: 2 additions & 2 deletions components/provider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as React from 'react';
import Portal from '../portal/portal';
import { LocaleProvider } from '../locale-provider';
import { LocaleProvider, LocaleValue } from '../locale-provider';
import { ThemeProvider, Theme } from '../style';
export interface ProviderProps {
locale?: any;
locale?: LocaleValue;
theme?: Theme;
}
export default class Provider extends React.Component<ProviderProps> {
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@
"qrcode.react": "^0.8.0",
"raf": "^3.4.0",
"rc-form": "2.2.6",
"react": "^16.7.0-alpha.2",
"react-copy-to-clipboard": "^5.0.0",
"react-document-title": "^2.0.1",
"react-dom": "^16.7.0-alpha.2",
"react-github-button": "^0.1.9",
"react-intl": "^2.2.3",
"react-native": "npm:@brunolemos/react-native",
"react": "^16.7.0-alpha.2",
"react-native": "^0.57.7",
"react-native-mocker": "^0.0.12",
"react-navigation": "^2.18.2",
"react-test-renderer": "^16.7.0-alpha.2",
Expand Down Expand Up @@ -154,5 +154,8 @@
"typings": "lib/index.d.ts",
"pre-commit": [
"lint-staged"
]
],
"resolutions": {
"react": "^16.7.0-alpha.2"
}
}
Loading

0 comments on commit cce261f

Please sign in to comment.