diff --git a/admin-web/mock/admin.js b/admin-web/mock/admin.js index 45ab135f8..aa30f8d3d 100644 --- a/admin-web/mock/admin.js +++ b/admin-web/mock/admin.js @@ -44,8 +44,8 @@ function getDictionaryText(req, res) { return res.json(resultBody(values)); } -function getDictionaryList(req, res) { - return res.json(dictionaryList); +function getDictionaryTree(req, res) { + return res.json(dictionaryList); } export default { @@ -54,7 +54,5 @@ export default { 'GET /admin-api/admins/resource/tree': getResourceTree, 'GET /admin-api/admins/role/page': getQueryRole, 'GET /admin-api/admins/admin/page': getQueryRole, - 'GET /admin-api/admins/dictionary/getList': getDictionaryKeys, - 'GET /admin-api/admins/dictionary/queryText': getDictionaryText, - // 'GET /admin-api/admins/data_dict/list': getDictionaryList, + 'GET /admin-api/admins/data_dict/tree': getDictionaryTree, }; diff --git a/admin-web/mock/geographic/dictionary-list.json b/admin-web/mock/geographic/dictionary-list.json index d60bc1f04..efd1f6197 100644 --- a/admin-web/mock/geographic/dictionary-list.json +++ b/admin-web/mock/geographic/dictionary-list.json @@ -3,20 +3,17 @@ "message": "", "data": [ { - "id": 1, "enumValue": "gender", - "value": "1", - "displayName": "男", - "sort": 1, - "memo": "性别 - 男" - }, - { - "id": 2, - "enumValue": "gender", - "value": "2", - "displayName": "女", - "sort": 2, - "memo": "性别 - 女" + "values": [ + { + "displayName": "男", + "value": 1 + }, + { + "displayName": "女", + "value": 2 + } + ] } ] -} \ No newline at end of file +} diff --git a/admin-web/src/components/Dictionary/DictionaryContext.js b/admin-web/src/components/Dictionary/DictionaryContext.js new file mode 100644 index 000000000..08220f13f --- /dev/null +++ b/admin-web/src/components/Dictionary/DictionaryContext.js @@ -0,0 +1,6 @@ +import React from 'react'; + +// 字典全局的 Context,会提前初始化工作。 +const DictionaryContext = React.createContext({}); + +export default DictionaryContext; diff --git a/admin-web/src/components/Dictionary/DictionarySelect.d.ts b/admin-web/src/components/Dictionary/DictionarySelect.d.ts index 40dbda604..b3df84c75 100644 --- a/admin-web/src/components/Dictionary/DictionarySelect.d.ts +++ b/admin-web/src/components/Dictionary/DictionarySelect.d.ts @@ -1,13 +1,9 @@ import * as React from 'react'; import { Select } from 'antd'; -export interface DictionaryObject { - text?: string; - value?: string | number | boolean; -} - export interface IDictionarySelectProps extends Select { - list?: DictionaryObject[]; + dicKey?: string; + defaultValue?: string | number | boolean; } export default class DictionarySelectD extends React.Component {} diff --git a/admin-web/src/components/Dictionary/DictionarySelect.js b/admin-web/src/components/Dictionary/DictionarySelect.js index 29211d229..629737c2e 100644 --- a/admin-web/src/components/Dictionary/DictionarySelect.js +++ b/admin-web/src/components/Dictionary/DictionarySelect.js @@ -1,20 +1,32 @@ import React, { PureComponent } from 'react'; import { Select } from 'antd'; +import DictionaryContext from './DictionaryContext'; export default class DictionarySelect extends PureComponent { - renderOptions() { - const { list } = this.props; - return list.map(item => { - return ( - - {item.text} - - ); - }); + renderSelect(children) { + return ; } render() { - const options = this.renderOptions(); - return ; + const { dicKey } = this.props; + return ( + + {context => { + const dicValues = context[dicKey]; + let nodes = []; + if (dicValues) { + nodes = Object.keys(dicValues).map(value => { + const text = dicValues[value]; + return ( + + {text} + + ); + }); + } + return this.renderSelect(nodes); + }} + + ); } } diff --git a/admin-web/src/components/Dictionary/DictionarySelect.md b/admin-web/src/components/Dictionary/DictionarySelect.md index 447bab237..dc3d4a7aa 100644 --- a/admin-web/src/components/Dictionary/DictionarySelect.md +++ b/admin-web/src/components/Dictionary/DictionarySelect.md @@ -7,18 +7,15 @@ subtitle: 描述列表 ## API -### DescriptionList +### DictionarySelect | 参数 | 说明 | 类型 | 默认值 | |----------|------------------------------------------|-------------|-------| -| list | 数据列表 | DictionObject[] | [] | - -### DictionObject - -| 参数 | 说明 | 类型 | 默认值 | -|----------|------------------------------------------|-------------|-------| -| text | 显示的文字 | string | - | -| value | 选择后的值 | string number boolean | - | - +| dicKey | 字典key值 | string[] | [] | +| defaultValue | 来自 antd Select组件 | string、number、boolean | [] | +### Demo +```jsx harmony + +``` diff --git a/admin-web/src/components/Dictionary/DictionaryText.d.ts b/admin-web/src/components/Dictionary/DictionaryText.d.ts new file mode 100644 index 000000000..a107dd9a9 --- /dev/null +++ b/admin-web/src/components/Dictionary/DictionaryText.d.ts @@ -0,0 +1,9 @@ +import * as React from 'react'; +import { Select } from 'antd'; + +export interface IDictionaryTextProps extends Select { + dicKey?: string; + dicValue?: string | number | boolean | Array; +} + +export default class DictionaryText extends React.Component {} diff --git a/admin-web/src/components/Dictionary/DictionaryText.js b/admin-web/src/components/Dictionary/DictionaryText.js new file mode 100644 index 000000000..9ee99f2df --- /dev/null +++ b/admin-web/src/components/Dictionary/DictionaryText.js @@ -0,0 +1,21 @@ +import React, { PureComponent } from 'react'; +import DictionaryContext from './DictionaryContext'; + +export default class DictionaryText extends PureComponent { + componentDidMount() {} + + render() { + const { dicKey, dicValue } = this.props; + return ( + + {context => { + const dicValues = context[dicKey]; + if (dicValues) { + return dicValues[dicValue]; + } + return null; + }} + + ); + } +} diff --git a/admin-web/src/pages/Dictionary/DictionaryValueText.md b/admin-web/src/components/Dictionary/DictionaryText.md similarity index 52% rename from admin-web/src/pages/Dictionary/DictionaryValueText.md rename to admin-web/src/components/Dictionary/DictionaryText.md index 2c2e442fa..0750889ca 100644 --- a/admin-web/src/pages/Dictionary/DictionaryValueText.md +++ b/admin-web/src/components/Dictionary/DictionaryText.md @@ -1,5 +1,5 @@ --- -title: DictionaryValueText +title: DictionaryText subtitle: 获取字典 value 显示值 --- @@ -9,11 +9,11 @@ subtitle: 获取字典 value 显示值 | 参数 | 说明 | 类型 | 默认值 | |----------|------------------------------------------|-------------|-------| -| dataKey | 字典的key | string | [] | -| value | 显示的值 | string number | [] | +| dicKey | 字典的key | string | [] | +| dicValue | value值 | string、number、boolean | [] | ### Demo ```jsx harmony - + ``` diff --git a/admin-web/src/layouts/BasicLayout.js b/admin-web/src/layouts/BasicLayout.js index 1a5a1a849..574788052 100644 --- a/admin-web/src/layouts/BasicLayout.js +++ b/admin-web/src/layouts/BasicLayout.js @@ -16,6 +16,7 @@ import Exception403 from '../pages/Exception/403'; import PageLoading from '@/components/PageLoading'; import SiderMenu from '@/components/SiderMenu'; import getPageTitle from '@/utils/getPageTitle'; +import DictionaryContext from '@/components/Dictionary/DictionaryContext'; import styles from './BasicLayout.less'; // lazy load SettingDrawer @@ -68,6 +69,10 @@ class BasicLayout extends React.Component { type: 'menu/getMenuData', payload: { routes, authority }, }); + dispatch({ + type: 'dictionaryContext/tree', + payload: {}, + }); } getContext() { @@ -81,10 +86,17 @@ class BasicLayout extends React.Component { getUrlsContext() { const { urlsData } = this.props; return { - ...urlsData, + urls: { + ...urlsData, + }, }; } + getDictionaryContext() { + const { dicTreeMap } = this.props; + return dicTreeMap; + } + getRouteAuthority = (pathname, routeData) => { const routes = routeData.slice(); // clone @@ -178,7 +190,7 @@ class BasicLayout extends React.Component { /> }> - {children} + {children}