22 * @description 教育信息组件
33 */
44
5- import React , { memo } from 'react'
5+ import React , { Fragment , memo , useState , useCallback } from 'react'
66
77import { Form , Input , Select , Button , DatePicker } from 'antd'
8+ import { MinusCircleOutlined } from '@ant-design/icons'
89import 'moment/locale/zh-cn'
910import locale from 'antd/es/date-picker/locale/zh_CN'
1011import moment from 'moment'
@@ -18,7 +19,9 @@ const layout = {
1819}
1920
2021const SelfEducation : React . FC = ( ) => {
21- const finished = ( values : any ) => {
22+ const [ formCountArr , setFormCountArr ] = useState < string [ ] > ( [ ] )
23+
24+ const finished = useCallback ( ( values : any , item : string ) => {
2225 if ( values . eduTime && values . eduTime . length > 0 ) {
2326 const startTime = moment ( values . eduTime [ 0 ] ) . format ( 'YYYY-MM' )
2427 const endTime = moment ( values . eduTime [ 1 ] ) . format ( 'YYYY-MM' )
@@ -28,44 +31,61 @@ const SelfEducation: React.FC = () => {
2831 eduTime : [ startTime , endTime ] ,
2932 }
3033 )
31- console . log ( values , forMatInfo )
34+ console . log ( forMatInfo )
3235 }
33- }
34- return (
35- < div className = { styles [ 'self-education' ] } >
36- < Form { ...layout } name = "basic" onFinish = { finished } >
37- < Form . Item label = "学校" name = "school" rules = { [ { required : true , message : '学校不能为空' } ] } >
38- < Input />
39- </ Form . Item >
36+ } , [ ] )
4037
41- < Form . Item label = "专业" name = "profession" rules = { [ { required : true , message : '专业不能为空' } ] } >
42- < Input />
43- </ Form . Item >
38+ const addEduForm = useCallback ( ( ) => {
39+ setFormCountArr ( [ ... formCountArr , ` ${ Math . random ( ) } ${ Date . now ( ) } ` ] )
40+ } , [ formCountArr ] )
4441
45- < Form . Item label = "学历" name = "eduLevel" rules = { [ { required : true , message : '学历不能为空' } ] } >
46- < Select >
47- { eduOption . map ( ( item ) => (
48- < Select . Option value = { item . value } key = { item . id } >
49- { item . name }
50- </ Select . Option >
51- ) ) }
52- </ Select >
53- </ Form . Item >
42+ const delForm = useCallback ( ( index : number ) => {
43+ setFormCountArr ( formCountArr . filter ( ( item , k ) => k !== index ) )
44+ } , [ formCountArr ] )
5445
55- < Form . Item
56- name = "eduTime"
57- label = "在校时间"
58- rules = { [ { type : 'array' as const , required : true , message : '时间不能为空!' } ] }
59- >
60- < DatePicker . RangePicker locale = { locale } picker = "month" />
61- </ Form . Item >
62-
63- < Form . Item >
64- < Button type = "primary" htmlType = "submit" >
65- 完成
66- </ Button >
67- </ Form . Item >
68- </ Form >
46+ return (
47+ < div className = { styles [ 'self-education' ] } >
48+ {
49+ formCountArr . map ( ( item , index , arr ) => (
50+ < Fragment key = { item } >
51+ { arr . length > 1 && < MinusCircleOutlined className = { styles [ 'del-btn' ] } onClick = { ( ) => delForm ( index ) } /> }
52+ < Form { ...layout } name = { item } onFinish = { ( values ) => finished ( values , item ) } >
53+ < Form . Item label = "学校" name = "school" rules = { [ { required : true , message : '学校不能为空' } ] } >
54+ < Input />
55+ </ Form . Item >
56+
57+ < Form . Item label = "专业" name = "profession" rules = { [ { required : true , message : '专业不能为空' } ] } >
58+ < Input />
59+ </ Form . Item >
60+
61+ < Form . Item label = "学历" name = "eduLevel" rules = { [ { required : true , message : '学历不能为空' } ] } >
62+ < Select >
63+ { eduOption . map ( ( item ) => (
64+ < Select . Option value = { item . value } key = { item . id } >
65+ { item . name }
66+ </ Select . Option >
67+ ) ) }
68+ </ Select >
69+ </ Form . Item >
70+
71+ < Form . Item
72+ name = "eduTime"
73+ label = "在校时间"
74+ rules = { [ { type : 'array' as const , required : true , message : '时间不能为空!' } ] }
75+ >
76+ < DatePicker . RangePicker locale = { locale } picker = "month" />
77+ </ Form . Item >
78+
79+ < Form . Item >
80+ < Button type = "primary" htmlType = "submit" >
81+ 完成
82+ </ Button >
83+ </ Form . Item >
84+ </ Form >
85+ </ Fragment >
86+ ) )
87+ }
88+ < Button type = "dashed" block style = { { background : 'transparent' , color : '#e5e5e5' } } onClick = { addEduForm } > 新增</ Button >
6989 </ div >
7090 )
7191}
0 commit comments