Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ConfigProvider to replace LocaleProvider
- Loading branch information
Showing
49 changed files
with
417 additions
and
399 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import ActionSheet from './ActionSheet'; | ||
import LocaleReceiver from '../locale-receiver'; | ||
import ConfigReceiver from '../config-receiver'; | ||
|
||
export default LocaleReceiver('ActionSheet')(ActionSheet); | ||
export default ConfigReceiver('ActionSheet')(ActionSheet); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import Alert from './Alert'; | ||
import LocaleReceiver from '../locale-receiver'; | ||
import ConfigReceiver from '../config-receiver'; | ||
|
||
export default LocaleReceiver('Alert')(Alert); | ||
export default ConfigReceiver('Alert')(Alert); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React, { PureComponent, createContext, Context } from 'react'; | ||
import { ConfigProviderProps, Locale } from './PropsType'; | ||
import setTheme from './setTheme'; | ||
import setPrimaryColor from './setPrimaryColor'; | ||
|
||
const defaultConfig: ConfigProviderProps = { | ||
locale: {} as any as Locale, | ||
theme: 'light', | ||
primary: '#00bc70', | ||
}; | ||
|
||
export const LocaleContext: Context<Locale> = createContext(defaultConfig.locale); | ||
export const ConfigContext: Context<ConfigProviderProps> = createContext(defaultConfig); | ||
|
||
let runTimeLocale: Locale; | ||
const changeRunTimeLocale = (locale: Locale) => { | ||
runTimeLocale = locale; | ||
}; | ||
|
||
export const getRunTimeLocale = () => runTimeLocale; | ||
|
||
export default class ConfigProvider extends PureComponent<ConfigProviderProps, {}> { | ||
static defaultProps = defaultConfig; | ||
|
||
render() { | ||
const { children, locale, theme, primary } = this.props; | ||
changeRunTimeLocale(locale); | ||
setTheme(theme); | ||
setPrimaryColor(primary); | ||
|
||
return ( | ||
<ConfigContext.Provider value={{ locale, theme, primary }}> | ||
{React.Children.only(children)} | ||
</ConfigContext.Provider> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import localeCN from './locale/zh_CN'; | ||
|
||
export type Locale = typeof localeCN; | ||
export type Theme = 'dark' | 'light'; | ||
export type Primary = string; | ||
|
||
export interface ConfigProviderProps { | ||
locale: Locale; | ||
theme: Theme; | ||
primary: Primary; | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# ConfigProvider 全局配置 | ||
|
||
|
||
|
||
## 基本用法 | ||
```jsx | ||
import { useState } from 'react'; | ||
import { Cell, ConfigProvider, Button, SearchBar, Modal, Keyboard, Radio } from 'zarm'; | ||
import enUS from 'zarm/config-provider/locale/en_US'; | ||
import zhCN from 'zarm/config-provider/locale/zh_CN'; | ||
|
||
const Demo = () => { | ||
const [lang, setLang] = useState(GlobalContext.lang); | ||
const [theme, setTheme] = useState(GlobalContext.theme); | ||
const [primary, setPrimary] = useState(GlobalContext.primary); | ||
|
||
const locale = lang === 'enUS' ? enUS : zhCN; | ||
|
||
const show = (key) => { | ||
if (key === 'alert') { | ||
Modal.alert({ | ||
title: '警告', | ||
content: '这里是警告信息', | ||
shape: 'radius' | ||
}); | ||
} else { | ||
Modal.confirm({ | ||
title: '确认信息', | ||
content: '你确定要这样做吗?', | ||
shape: 'radius' | ||
}) | ||
} | ||
}; | ||
|
||
return ( | ||
<ConfigProvider locale={locale} primary={primary} theme={theme}> | ||
<> | ||
<Cell | ||
title="切换语言" | ||
description={ | ||
<Radio.Group | ||
compact | ||
type="button" | ||
value={lang} | ||
onChange={setLang} | ||
> | ||
<Radio value="zhCN">中文</Radio> | ||
<Radio value="enUS">EN</Radio> | ||
</Radio.Group> | ||
} | ||
/> | ||
|
||
<Cell | ||
title="切换品牌色" | ||
description={ | ||
<ul className="colors"> | ||
{ | ||
['#00bc70', '#1890ff', '#f5222d', '#fa541b', '#13c2c2', '#2f54ec', '#712fd1'].map((color, index) => { | ||
return ( | ||
<li | ||
key={+index} | ||
style={{ backgroundColor: color }} | ||
onClick={() => setPrimary(color)} | ||
/> | ||
); | ||
}) | ||
} | ||
</ul> | ||
} | ||
/> | ||
|
||
<Cell | ||
title="切换主题" | ||
description={ | ||
<Radio.Group | ||
compact | ||
type="button" | ||
value={theme} | ||
onChange={(value) => setTheme(value)} | ||
> | ||
<Radio value="light">默认主题</Radio> | ||
<Radio value="dark">暗黑主题</Radio> | ||
</Radio.Group> | ||
} | ||
/> | ||
|
||
<SearchBar /> | ||
<Keyboard /> | ||
|
||
<Cell | ||
description={ | ||
<Button size="xs" onClick={() => show('alert')}>开启</Button> | ||
} | ||
> | ||
警告框 | ||
</Cell> | ||
|
||
<Cell | ||
description={ | ||
<Button size="xs" onClick={() => show('confirm')}>开启</Button> | ||
} | ||
> | ||
确认框 | ||
</Cell> | ||
</> | ||
</ConfigProvider> | ||
); | ||
} | ||
|
||
ReactDOM.render(<Demo />, mountNode); | ||
``` | ||
|
||
|
||
|
||
## API | ||
|
||
| 属性 | 类型 | 默认值 | 说明 | | ||
| :--- | :--- | :--- | :--- | | ||
| locale | Object | - | 语言包配置,默认为中文,语言包可到 zarm/lib/locale-provider/locale 目录下寻找 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import ConfigProvider from './ConfigProvider'; | ||
|
||
export default ConfigProvider; |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Color from 'color'; | ||
|
||
const setPrimaryColor = (color) => { | ||
document.documentElement.style.setProperty('--theme-primary', color); | ||
document.documentElement.style.setProperty('--theme-primary-dark', Color(color).darken(0.05)); | ||
document.documentElement.style.setProperty('--button-primary-shadow-color', Color(color).alpha(0.5).lighten(0.0)); | ||
}; | ||
|
||
export default setPrimaryColor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import ConfigReceiver from './ConfigReceiver'; | ||
|
||
export default ConfigReceiver; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import Confirm from './Confirm'; | ||
import LocaleReceiver from '../locale-receiver'; | ||
import ConfigReceiver from '../config-receiver'; | ||
|
||
export default LocaleReceiver('Confirm')(Confirm); | ||
export default ConfigReceiver('Confirm')(Confirm); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import DatePickerView from './DatePickerView'; | ||
import LocaleReceiver from '../locale-receiver'; | ||
import ConfigReceiver from '../config-receiver'; | ||
|
||
export default LocaleReceiver('DatePickerView')(DatePickerView); | ||
export default ConfigReceiver('DatePickerView')(DatePickerView); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import DatePicker from './DatePicker'; | ||
import LocaleReceiver from '../locale-receiver'; | ||
import ConfigReceiver from '../config-receiver'; | ||
|
||
export default LocaleReceiver('DatePicker')(DatePicker); | ||
export default ConfigReceiver('DatePicker')(DatePicker); |
Oops, something went wrong.