update 种子模版挪动位置
This commit is contained in:
@@ -1,153 +0,0 @@
|
||||
import React, { Component } from 'react'
|
||||
import { Button, Tabs } from 'antd'
|
||||
import { ComponentDynamic, Container } from 'components'
|
||||
import { isEqual, merge } from 'lodash'
|
||||
|
||||
const tabs = [
|
||||
{
|
||||
title: '标题',
|
||||
component: () => import('./tab'),
|
||||
show: true,
|
||||
},
|
||||
]
|
||||
|
||||
export default class index extends Component {
|
||||
state = {
|
||||
actived: '0',
|
||||
loading: true,
|
||||
record: null,
|
||||
saveDisabled: true,
|
||||
saving: false,
|
||||
}
|
||||
|
||||
// 子表单实例集合
|
||||
children = []
|
||||
|
||||
// 整合提交数据
|
||||
formData = {}
|
||||
|
||||
/**
|
||||
* 阻止外部组件引发的渲染,提升性能
|
||||
* 可自行添加渲染条件
|
||||
* [必要]
|
||||
* @param {*} props
|
||||
* @param {*} state
|
||||
* @returns
|
||||
*/
|
||||
shouldComponentUpdate(props, state) {
|
||||
return !isEqual(this.state, state)
|
||||
}
|
||||
|
||||
/**
|
||||
* DOM加载完成钩子,可在此获取详细数据赋值到record
|
||||
*/
|
||||
componentDidMount() {}
|
||||
|
||||
/**
|
||||
* 接收到所有子组件已加载完成,并启用提交按钮
|
||||
*/
|
||||
call(child, index) {
|
||||
this.children[index] = child
|
||||
if (this.children.filter(p => p).length === tabs.filter(p => p.show).length) {
|
||||
this.setState({ saveDisabled: false })
|
||||
}
|
||||
}
|
||||
|
||||
async onSubmit() {
|
||||
for (const child of this.children) {
|
||||
try {
|
||||
const data = await child.getData()
|
||||
merge(this.formData, data)
|
||||
} catch (e) {
|
||||
return e
|
||||
}
|
||||
}
|
||||
|
||||
//#region 提交数据
|
||||
this.setState({ saving: true })
|
||||
this.setState({ saving: false })
|
||||
//#endregion
|
||||
}
|
||||
|
||||
render() {
|
||||
const { id } = this.props
|
||||
|
||||
const { actived, loading, record, saveDisabled, saving } = this.state
|
||||
|
||||
return (
|
||||
<div className="yo-form-page">
|
||||
<div className="yo-form-page-layout">
|
||||
{/* 底部工具栏(需放在前面) */}
|
||||
<div className="yo-form-page--bar yo-form-page--bar--with-tab">
|
||||
<Container mode="fluid">
|
||||
<div className="yo-form-page--bar-inner">
|
||||
<span></span>
|
||||
<span>
|
||||
<Button onClick={() => window.closeContentWindow()}>
|
||||
取消
|
||||
</Button>
|
||||
<Button
|
||||
disabled={saveDisabled}
|
||||
loading={saving}
|
||||
type="primary"
|
||||
onClick={() => this.onSubmit()}
|
||||
>
|
||||
保存
|
||||
</Button>
|
||||
</span>
|
||||
</div>
|
||||
</Container>
|
||||
</div>
|
||||
{/* 顶部信息栏,不需要时刻删除 */}
|
||||
<div className="yo-form-page--header" style={{ paddingBottom: 0 }}></div>
|
||||
<div className="yo-tab-external-mount">
|
||||
<Tabs
|
||||
activeKey={actived}
|
||||
animated={false}
|
||||
onChange={activeKey => {
|
||||
this.setState({ actived: activeKey })
|
||||
}}
|
||||
>
|
||||
{tabs.map(
|
||||
(tab, i) =>
|
||||
tab.show && (
|
||||
<Tabs.TabPane
|
||||
key={i}
|
||||
forceRender={false}
|
||||
tab={tab.title}
|
||||
></Tabs.TabPane>
|
||||
)
|
||||
)}
|
||||
</Tabs>
|
||||
<div className="yo-tab-external-mount-content">
|
||||
{tabs.map((tab, i) => {
|
||||
if (tab.show) {
|
||||
return (
|
||||
<div
|
||||
key={i}
|
||||
className={[
|
||||
actived == i
|
||||
? 'yo-tab-external-tabpane-active'
|
||||
: 'yo-tab-external-tabpane-inactive',
|
||||
'yo-tab-external-tabpane',
|
||||
].join(' ')}
|
||||
>
|
||||
<ComponentDynamic
|
||||
is={tab.component}
|
||||
id={id}
|
||||
record={record}
|
||||
loading={loading}
|
||||
onRef={child => this.call(child, i)}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
return <></>
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
import React, { Component } from 'react'
|
||||
import ReactDOM from 'react-dom'
|
||||
import { Anchor, Card, Col, Divider, Row, Spin } from 'antd'
|
||||
import { AntIcon, ComponentDynamic, Container } from 'components'
|
||||
import { isEqual, merge } from 'lodash'
|
||||
|
||||
const parts = [
|
||||
{
|
||||
// title: '标题',
|
||||
component: () => import('./part'),
|
||||
},
|
||||
]
|
||||
|
||||
export default class index extends Component {
|
||||
// 子表单实例集合
|
||||
children = []
|
||||
|
||||
// 整合提交数据
|
||||
formData = {}
|
||||
|
||||
// 锚点挂载DOM
|
||||
container = window
|
||||
|
||||
/**
|
||||
* 阻止外部组件引发的渲染,提升性能
|
||||
* 可自行添加渲染条件
|
||||
* [必要]
|
||||
* @param {*} props
|
||||
* @param {*} state
|
||||
* @returns
|
||||
*/
|
||||
shouldComponentUpdate(props, state) {
|
||||
return !isEqual(this.state, state) || this.props.loading !== props.loading
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载完成,通知父级组件并传递自身
|
||||
*/
|
||||
call(child, index) {
|
||||
this.children[index] = child
|
||||
if (this.children.filter(p => p).length === parts.length) {
|
||||
const { onRef } = this.props
|
||||
if (onRef) onRef(this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从下级组件获取表单数据,并传递给更上级组件
|
||||
* [异步,必要]
|
||||
* @returns
|
||||
*/
|
||||
async getData() {
|
||||
for (const child of this.children) {
|
||||
const data = await child.getData()
|
||||
merge(this.formData, data)
|
||||
}
|
||||
return this.formData
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置锚点容器
|
||||
* [非必要]
|
||||
* @param {*} container
|
||||
*/
|
||||
setContainer = container => {
|
||||
this.container = (ReactDOM.findDOMNode(container) || {}).parentNode
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染
|
||||
* 当前渲染结构已完善,非必要可以不用修改
|
||||
* [必要]
|
||||
* @returns
|
||||
*/
|
||||
render() {
|
||||
const { id, loading } = this.props
|
||||
|
||||
return (
|
||||
<Container mode="fluid" ref={this.setContainer}>
|
||||
<Row gutter={16}>
|
||||
<Col flex="1">
|
||||
<br />
|
||||
<div className="yo-adorn--house-top" />
|
||||
<Card className="yo-form-page--body">
|
||||
{parts.map((item, i) => (
|
||||
<React.Fragment key={i}>
|
||||
<section id={`form-${i}-${id}`}>
|
||||
{item.title && <h5>{item.title}</h5>}
|
||||
<Spin
|
||||
spinning={loading}
|
||||
indicator={<AntIcon type="loading" />}
|
||||
wrapperClassName={loading && 'h-400-min'}
|
||||
>
|
||||
{!loading && (
|
||||
<ComponentDynamic
|
||||
is={item.component}
|
||||
{...this.props}
|
||||
onRef={child => this.call(child, i)}
|
||||
/>
|
||||
)}
|
||||
</Spin>
|
||||
</section>
|
||||
{i < parts.length - 1 && <Divider />}
|
||||
</React.Fragment>
|
||||
))}
|
||||
</Card>
|
||||
</Col>
|
||||
{/* 锚点,如果不需要可以删除以下节点 */}
|
||||
<Col flex="240px">
|
||||
<Anchor
|
||||
getContainer={() => this.container}
|
||||
offsetTop={24}
|
||||
targetOffset={100}
|
||||
wrapperStyle={{ backgroundColor: 'transparent' }}
|
||||
onClick={e => e.preventDefault()}
|
||||
>
|
||||
{parts.map((part, i) => (
|
||||
<Anchor.Link key={i} href={`#form-${i}-${id}`} title={part.title} />
|
||||
))}
|
||||
</Anchor>
|
||||
</Col>
|
||||
</Row>
|
||||
</Container>
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
import React, { Component } from 'react'
|
||||
import { Form, Spin } from 'antd'
|
||||
import { AntIcon } from 'components'
|
||||
import { cloneDeep, isEqual } from 'lodash'
|
||||
|
||||
const initialValues = {}
|
||||
|
||||
const layout = {
|
||||
labelCol: { flex: '150px' },
|
||||
wrapperCol: { flex: '1' },
|
||||
}
|
||||
|
||||
export default class part extends Component {
|
||||
state = {
|
||||
loading: true,
|
||||
codes: {},
|
||||
options: {},
|
||||
}
|
||||
|
||||
// 表单实例
|
||||
form = React.createRef()
|
||||
|
||||
// 初始化数据
|
||||
record = {}
|
||||
|
||||
/**
|
||||
* 阻止外部组件引发的渲染,提升性能
|
||||
* 可自行添加渲染条件
|
||||
* [必要]
|
||||
* @param {*} props
|
||||
* @param {*} state
|
||||
* @returns
|
||||
*/
|
||||
shouldComponentUpdate(props, state) {
|
||||
return !isEqual(this.state, state)
|
||||
}
|
||||
|
||||
/**
|
||||
* DOM加载完成钩子,绑定数据
|
||||
*/
|
||||
componentDidMount() {
|
||||
this.fillData({
|
||||
record: this.props.record,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载完成,通知父级组件并传递自身
|
||||
*/
|
||||
call() {
|
||||
const { onRef } = this.props
|
||||
if (onRef) onRef(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充数据
|
||||
* 可以在设置this.record之后对其作出数据结构调整
|
||||
* [异步,必要]
|
||||
* @param {*} params
|
||||
*/
|
||||
async fillData(params) {
|
||||
this.record = cloneDeep(params.record)
|
||||
//#region 从后端转换成前段所需格式
|
||||
//#endregion
|
||||
this.form.current.setFieldsValue(this.record)
|
||||
|
||||
this.setState({ loading: false })
|
||||
this.call()
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
* 可以对postData进行数据结构调整
|
||||
* [异步,必要]
|
||||
* @returns
|
||||
*/
|
||||
async getData() {
|
||||
const form = this.form.current
|
||||
|
||||
const valid = await form.validateFields()
|
||||
if (valid) {
|
||||
const postData = form.getFieldsValue()
|
||||
//#region 从前段转换后端所需格式
|
||||
//#endregion
|
||||
return postData
|
||||
}
|
||||
}
|
||||
|
||||
//#region 自定义方法
|
||||
/**
|
||||
* 表单change事件处理,包括了所有字段的change
|
||||
* [异步,非必要]
|
||||
* @param {*} changedValues
|
||||
* @param {*} allValues
|
||||
*/
|
||||
async onValuesChange(changedValues, allValues) {}
|
||||
//#endregion
|
||||
|
||||
render() {
|
||||
const { loading } = this.state
|
||||
|
||||
return (
|
||||
<Spin spinning={loading} indicator={<AntIcon type="loading" />}>
|
||||
<Form
|
||||
initialValues={initialValues}
|
||||
ref={this.form}
|
||||
{...layout}
|
||||
onValuesChange={(changedValues, allValues) =>
|
||||
this.onValuesChange(changedValues, allValues)
|
||||
}
|
||||
></Form>
|
||||
</Spin>
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,169 +0,0 @@
|
||||
import React, { Component } from 'react'
|
||||
import ReactDOM from 'react-dom'
|
||||
import { Anchor, Button, Card, Col, Divider, Row, Spin } from 'antd'
|
||||
import { AntIcon, ComponentDynamic, Container } from 'components'
|
||||
import { isEqual, merge } from 'lodash'
|
||||
|
||||
const parts = [
|
||||
{
|
||||
// title: '标题',
|
||||
component: () => import('./part'),
|
||||
},
|
||||
]
|
||||
|
||||
export default class index extends Component {
|
||||
state = {
|
||||
loading: true,
|
||||
record: null,
|
||||
saveDisabled: true,
|
||||
saving: false,
|
||||
}
|
||||
|
||||
// 子表单实例集合
|
||||
children = []
|
||||
|
||||
// 整合提交数据
|
||||
formData = {}
|
||||
|
||||
// 锚点挂载DOM
|
||||
container = window
|
||||
|
||||
/**
|
||||
* 阻止外部组件引发的渲染,提升性能
|
||||
* 可自行添加渲染条件
|
||||
* [必要]
|
||||
* @param {*} props
|
||||
* @param {*} state
|
||||
* @returns
|
||||
*/
|
||||
shouldComponentUpdate(props, state) {
|
||||
return !isEqual(this.state, state)
|
||||
}
|
||||
|
||||
/**
|
||||
* DOM加载完成钩子,可在此获取详细数据赋值到record
|
||||
*/
|
||||
componentDidMount() {}
|
||||
|
||||
call(child, index) {
|
||||
this.children[index] = child
|
||||
if (this.children.filter(p => p).length === tabs.filter(p => p.show).length) {
|
||||
this.setState({ saveDisabled: false })
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交
|
||||
* [异步,必要]
|
||||
* @returns
|
||||
*/
|
||||
async onSubmit() {
|
||||
for (const child of this.children) {
|
||||
try {
|
||||
const data = await child.getData()
|
||||
merge(this.formData, data)
|
||||
} catch (e) {
|
||||
return e
|
||||
}
|
||||
}
|
||||
|
||||
//#region 提交数据
|
||||
this.setState({ saving: true })
|
||||
this.setState({ saving: false })
|
||||
//#endregion
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置锚点容器
|
||||
* [非必要]
|
||||
* @param {*} container
|
||||
*/
|
||||
setContainer = container => {
|
||||
this.container = (ReactDOM.findDOMNode(container) || {}).parentNode
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染
|
||||
* 当前渲染结构已完善,非必要可以不用修改
|
||||
* [必要]
|
||||
* @returns
|
||||
*/
|
||||
render() {
|
||||
const { id } = this.props
|
||||
|
||||
const { loading, record, saveDisabled, saving } = this.state
|
||||
|
||||
return (
|
||||
<div className="yo-form-page">
|
||||
<Container mode="fluid" ref={this.setContainer}>
|
||||
<Row gutter={16}>
|
||||
<Col flex="1">
|
||||
<br />
|
||||
<div className="yo-adorn--house-top" />
|
||||
<Card className="yo-form-page--body">
|
||||
{parts.map((item, i) => (
|
||||
<React.Fragment key={i}>
|
||||
<section id={`form-${i}-${id}`}>
|
||||
{item.title && <h5>{item.title}</h5>}
|
||||
<Spin
|
||||
spinning={loading}
|
||||
indicator={<AntIcon type="loading" />}
|
||||
wrapperClassName={loading && 'h-400-min'}
|
||||
>
|
||||
{!loading && (
|
||||
<ComponentDynamic
|
||||
is={item.component}
|
||||
id={id}
|
||||
record={record}
|
||||
onRef={child => this.call(child, i)}
|
||||
/>
|
||||
)}
|
||||
</Spin>
|
||||
</section>
|
||||
{i < parts.length - 1 && <Divider />}
|
||||
</React.Fragment>
|
||||
))}
|
||||
</Card>
|
||||
</Col>
|
||||
{/* 锚点,如果不需要可以删除以下节点 */}
|
||||
<Col flex="240px">
|
||||
<Anchor
|
||||
getContainer={() => this.container}
|
||||
offsetTop={24}
|
||||
targetOffset={100}
|
||||
wrapperStyle={{ backgroundColor: 'transparent' }}
|
||||
onClick={e => e.preventDefault()}
|
||||
>
|
||||
{parts.map((part, i) => (
|
||||
<Anchor.Link
|
||||
key={i}
|
||||
href={`#form-${i}-${id}`}
|
||||
title={part.title}
|
||||
/>
|
||||
))}
|
||||
</Anchor>
|
||||
</Col>
|
||||
</Row>
|
||||
</Container>
|
||||
<div className="yo-form-page--bar">
|
||||
<Container mode="fluid">
|
||||
<div className="yo-form-page--bar-inner">
|
||||
<span></span>
|
||||
<span>
|
||||
<Button onClick={() => window.closeContentWindow()}>取消</Button>
|
||||
<Button
|
||||
disabled={saveDisabled}
|
||||
loading={saving}
|
||||
type="primary"
|
||||
onClick={() => this.onSubmit()}
|
||||
>
|
||||
保存
|
||||
</Button>
|
||||
</span>
|
||||
</div>
|
||||
</Container>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
import React, { Component } from 'react'
|
||||
import { Form, Spin } from 'antd'
|
||||
import { AntIcon } from 'components'
|
||||
import { cloneDeep, isEqual } from 'lodash'
|
||||
|
||||
const initialValues = {}
|
||||
|
||||
const layout = {
|
||||
labelCol: { flex: '150px' },
|
||||
wrapperCol: { flex: '1' },
|
||||
}
|
||||
|
||||
export default class part extends Component {
|
||||
state = {
|
||||
loading: true,
|
||||
codes: {},
|
||||
options: {},
|
||||
}
|
||||
|
||||
// 表单实例
|
||||
form = React.createRef()
|
||||
|
||||
// 初始化数据
|
||||
record = {}
|
||||
|
||||
/**
|
||||
* 阻止外部组件引发的渲染,提升性能
|
||||
* 可自行添加渲染条件
|
||||
* [必要]
|
||||
* @param {*} props
|
||||
* @param {*} state
|
||||
* @returns
|
||||
*/
|
||||
shouldComponentUpdate(props, state) {
|
||||
return !isEqual(this.state, state)
|
||||
}
|
||||
|
||||
/**
|
||||
* DOM加载完成钩子,绑定数据
|
||||
*/
|
||||
componentDidMount() {
|
||||
this.fillData({
|
||||
record: this.props.record,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载完成,通知父级组件并传递自身
|
||||
*/
|
||||
call() {
|
||||
const { onRef } = this.props
|
||||
if (onRef) onRef(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充数据
|
||||
* 可以在设置this.record之后对其作出数据结构调整
|
||||
* [异步,必要]
|
||||
* @param {*} params
|
||||
*/
|
||||
async fillData(params) {
|
||||
this.record = cloneDeep(params.record)
|
||||
//#region 从后端转换成前段所需格式
|
||||
//#endregion
|
||||
this.form.current.setFieldsValue(this.record)
|
||||
|
||||
this.setState({ loading: false })
|
||||
this.call()
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
* 可以对postData进行数据结构调整
|
||||
* [异步,必要]
|
||||
* @returns
|
||||
*/
|
||||
async getData() {
|
||||
const form = this.form.current
|
||||
|
||||
const valid = await form.validateFields()
|
||||
if (valid) {
|
||||
const postData = form.getFieldsValue()
|
||||
//#region 从前段转换后端所需格式
|
||||
//#endregion
|
||||
return postData
|
||||
}
|
||||
}
|
||||
|
||||
//#region 自定义方法
|
||||
/**
|
||||
* 表单change事件处理,包括了所有字段的change
|
||||
* [异步,非必要]
|
||||
* @param {*} changedValues
|
||||
* @param {*} allValues
|
||||
*/
|
||||
async onValuesChange(changedValues, allValues) {}
|
||||
//#endregion
|
||||
|
||||
render() {
|
||||
const { loading } = this.state
|
||||
|
||||
return (
|
||||
<Spin spinning={loading} indicator={<AntIcon type="loading" />}>
|
||||
<Form
|
||||
initialValues={initialValues}
|
||||
ref={this.form}
|
||||
{...layout}
|
||||
onValuesChange={(changedValues, allValues) =>
|
||||
this.onValuesChange(changedValues, allValues)
|
||||
}
|
||||
></Form>
|
||||
</Spin>
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
import React, { Component } from 'react'
|
||||
import { Form, Spin } from 'antd'
|
||||
import { AntIcon } from 'components'
|
||||
import { api } from 'common/api'
|
||||
|
||||
const initialValues = {}
|
||||
|
||||
export default class form extends Component {
|
||||
state = {
|
||||
// 加载状态
|
||||
loading: true,
|
||||
}
|
||||
|
||||
// 表单实例
|
||||
form = React.createRef()
|
||||
|
||||
// 初始化数据
|
||||
record = {}
|
||||
|
||||
/**
|
||||
* mount后回调
|
||||
*/
|
||||
componentDidMount() {
|
||||
this.props.created && this.props.created(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充数据
|
||||
* 可以在设置this.record之后对其作出数据结构调整
|
||||
* [异步,必要]
|
||||
* @param {*} params
|
||||
*/
|
||||
async fillData(params) {
|
||||
const state = { loading: false }
|
||||
//#region 从后端转换成前段所需格式,也可以在此处调用获取详细数据接口
|
||||
if (params.id) {
|
||||
this.record = (await api).data
|
||||
}
|
||||
//#endregion
|
||||
this.form.current.setFieldsValue(this.record)
|
||||
|
||||
this.setState(state)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
* 可以对postData进行数据结构调整
|
||||
* [异步,必要]
|
||||
* @returns
|
||||
*/
|
||||
async getData() {
|
||||
const form = this.form.current
|
||||
|
||||
const valid = await form.validateFields()
|
||||
if (valid) {
|
||||
const postData = form.getFieldsValue()
|
||||
if (this.record) {
|
||||
postData.id = this.record.id
|
||||
}
|
||||
//#region 从前段转换后端所需格式
|
||||
//#endregion
|
||||
return postData
|
||||
}
|
||||
}
|
||||
|
||||
//#region 自定义方法
|
||||
//#endregion
|
||||
|
||||
render() {
|
||||
return (
|
||||
<Form initialValues={initialValues} ref={this.form} className="yo-form">
|
||||
<Spin spinning={this.state.loading} indicator={<AntIcon type="loading" />}>
|
||||
<div className="yo-form-group"></div>
|
||||
</Spin>
|
||||
</Form>
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,235 +0,0 @@
|
||||
import React, { Component } from 'react'
|
||||
import { Button, Card, Form, Input, message as Message, Popconfirm } from 'antd'
|
||||
import { AntIcon, Auth, Container, ModalForm, QueryTable, QueryTableActions } from 'components'
|
||||
import { api } from 'common/api'
|
||||
import auth from 'components/authorized/handler'
|
||||
import { isEqual } from 'lodash'
|
||||
import getDictData from 'util/dic'
|
||||
import { toCamelCase } from 'util/format'
|
||||
import FormBody from './form'
|
||||
|
||||
/**
|
||||
* 注释段[\/**\/]为必须要改
|
||||
*/
|
||||
|
||||
/**
|
||||
* 配置页面所需接口函数
|
||||
*/
|
||||
const apiAction = {
|
||||
page: api /**/,
|
||||
add: api /**/,
|
||||
edit: api /**/,
|
||||
delete: api /**/,
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于弹窗标题
|
||||
* [必要]
|
||||
*/
|
||||
const name = '/**/'
|
||||
|
||||
/**
|
||||
* 统一配置权限标识
|
||||
* [必要]
|
||||
*/
|
||||
const authName = '/**/'
|
||||
|
||||
export default class index extends Component {
|
||||
state = {
|
||||
codes: {},
|
||||
}
|
||||
|
||||
// 表格实例
|
||||
table = React.createRef()
|
||||
|
||||
// 新增窗口实例
|
||||
addForm = React.createRef()
|
||||
// 编辑窗口实例
|
||||
editForm = React.createRef()
|
||||
|
||||
columns = []
|
||||
|
||||
/**
|
||||
* 构造函数,在渲染前动态添加操作字段等
|
||||
* @param {*} props
|
||||
*/
|
||||
constructor(props) {
|
||||
super(props)
|
||||
|
||||
const flag = auth({ [authName]: [['edit'], ['delete']] })
|
||||
|
||||
if (flag) {
|
||||
this.columns.push({
|
||||
title: '操作',
|
||||
width: 150,
|
||||
dataIndex: 'actions',
|
||||
render: (text, { id }) => (
|
||||
<QueryTableActions>
|
||||
<Auth auth={{ [authName]: 'edit' }}>
|
||||
<a onClick={() => this.onOpen(this.editForm, id)}>编辑</a>
|
||||
</Auth>
|
||||
<Auth auth={{ [authName]: 'delete' }}>
|
||||
<Popconfirm
|
||||
placement="topRight"
|
||||
title="是否确认删除"
|
||||
onConfirm={() => this.onDelete(id)}
|
||||
>
|
||||
<a>删除</a>
|
||||
</Popconfirm>
|
||||
</Auth>
|
||||
</QueryTableActions>
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 阻止外部组件引发的渲染,提升性能
|
||||
* 可自行添加渲染条件
|
||||
* [必要]
|
||||
* @param {*} props
|
||||
* @param {*} state
|
||||
* @returns
|
||||
*/
|
||||
shouldComponentUpdate(props, state) {
|
||||
return !isEqual(this.state, state)
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载字典数据,之后开始加载表格数据
|
||||
* 如果必须要加载字典数据,可直接对表格设置autoLoad=true
|
||||
*/
|
||||
componentDidMount() {
|
||||
const { onLoading, onLoadData } = this.table.current
|
||||
onLoading()
|
||||
getDictData(/**/).then(codes => {
|
||||
this.setState({ codes }, () => {
|
||||
onLoadData()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用加载数据接口,可在调用前对query进行处理
|
||||
* [异步,必要]
|
||||
* @param {*} params
|
||||
* @param {*} query
|
||||
* @returns
|
||||
*/
|
||||
loadData = async (params, query) => {
|
||||
const { data } = await apiAction.page({
|
||||
...params,
|
||||
...query,
|
||||
})
|
||||
return data
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定字典数据
|
||||
* @param {*} code
|
||||
* @param {*} name
|
||||
* @returns
|
||||
*/
|
||||
bindCodeValue(code, name) {
|
||||
name = toCamelCase(name)
|
||||
const codes = this.state.codes[name]
|
||||
if (codes) {
|
||||
const c = codes.find(p => p.code == code)
|
||||
if (c) {
|
||||
return c.value
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开新增/编辑弹窗
|
||||
* @param {*} modal
|
||||
* @param {*} id
|
||||
*/
|
||||
onOpen(modal, id) {
|
||||
modal.current.open({ id })
|
||||
}
|
||||
|
||||
/**
|
||||
* 对表格上的操作进行统一处理
|
||||
* [异步]
|
||||
* @param {*} action
|
||||
* @param {*} successMessage
|
||||
*/
|
||||
async onAction(action, successMessage) {
|
||||
const { onLoading, onLoaded, onReloadData } = this.table.current
|
||||
onLoading()
|
||||
try {
|
||||
if (action) {
|
||||
await action
|
||||
}
|
||||
if (successMessage) {
|
||||
Message.success(successMessage)
|
||||
}
|
||||
onReloadData()
|
||||
} catch {
|
||||
onLoaded()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
* @param {*} id
|
||||
*/
|
||||
onDelete(id) {
|
||||
this.onAction(apiAction.delete({ id }), '删除成功')
|
||||
}
|
||||
|
||||
//#region 自定义方法
|
||||
//#endregion
|
||||
|
||||
render() {
|
||||
return (
|
||||
<Container mode="fluid">
|
||||
<br />
|
||||
<Card bordered={false}>
|
||||
<QueryTable
|
||||
ref={this.table}
|
||||
autoLoad={false}
|
||||
loadData={this.loadData}
|
||||
columns={this.columns}
|
||||
query={<Auth auth={{ [authName]: 'page' }}></Auth>}
|
||||
operator={
|
||||
<Auth auth={{ [authName]: 'add' }}>
|
||||
<Button
|
||||
icon={<AntIcon type="plus" />}
|
||||
onClick={() => this.onOpen(this.addForm)}
|
||||
>
|
||||
新增{name}
|
||||
</Button>
|
||||
</Auth>
|
||||
}
|
||||
/>
|
||||
</Card>
|
||||
|
||||
<Auth auth={{ [authName]: 'add' }}>
|
||||
<ModalForm
|
||||
title={`新增${name}`}
|
||||
action={apiAction.add}
|
||||
ref={this.addForm}
|
||||
onSuccess={() => this.table.current.onReloadData()}
|
||||
>
|
||||
<FormBody />
|
||||
</ModalForm>
|
||||
</Auth>
|
||||
|
||||
<Auth auth={{ [authName]: 'edit' }}>
|
||||
<ModalForm
|
||||
title={`编辑${name}`}
|
||||
action={apiAction.edit}
|
||||
ref={this.editForm}
|
||||
onSuccess={() => this.table.current.onReloadData()}
|
||||
>
|
||||
<FormBody />
|
||||
</ModalForm>
|
||||
</Auth>
|
||||
</Container>
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user