@@ -930,6 +930,240 @@ const Demo = () => (
930
930
ReactDOM .render (< Demo / > , _mount_);
931
931
```
932
932
933
+ ### 可编辑
934
+
935
+ 利用 render 属性, 传递自定义组件实现
936
+
937
+ <!-- rehype:bgWhite=true&codeSandbox=true&codePen=true-->
938
+ ``` jsx
939
+ import ReactDOM from ' react-dom' ;
940
+ import { Table , Button , Input , Select } from ' uiw' ;
941
+
942
+ const columns = (onChange ) => [
943
+ {
944
+ title: ' 姓名' ,
945
+ ellipsis: true ,
946
+ key: ' name' ,
947
+ render : (text ,key , rowData ) => {
948
+ return < EditInput
949
+ value= {text}
950
+ onChange= {(value )=> {
951
+ onChange ({... rowData, name: value}, rowData .id )
952
+ }}
953
+ / >
954
+ }
955
+ },
956
+ {
957
+ title: ' 性别' ,
958
+ key: ' gender' ,
959
+ render : (text ,key , rowData ) => {
960
+ return < EditInput
961
+ value= {text}
962
+ component= " Select"
963
+ onChange= {(value )=> {
964
+ onChange ({... rowData, gender: value}, rowData .id )
965
+ }}
966
+ / >
967
+ }
968
+ },
969
+ {
970
+ title: ' 年龄' ,
971
+ key: ' age' ,
972
+ render : (text ,key , rowData ) => {
973
+ return < EditInput
974
+ value= {text}
975
+ type= " number"
976
+ onChange= {(value )=> {
977
+ onChange ({... rowData, age: value}, rowData .id )
978
+ }}
979
+ / >
980
+ }
981
+ },
982
+ {
983
+ title: ' 地址' ,
984
+ key: ' info' ,
985
+ render : (text ,key , rowData ) => {
986
+ return < EditInput
987
+ value= {text}
988
+ onChange= {(value )=> {
989
+ onChange ({... rowData, info: value}, rowData .id )
990
+ }}
991
+ / >
992
+ }
993
+ },
994
+ ];
995
+ const dataSource = [
996
+ { id: 1 , gender: " 男" , name: ' 邓紫棋' , age: ' 12' , info: ' 又名G.E.M.,原名邓诗颖,1991年8月16日生于中国上海,中国香港创作型女' },
997
+ { id: 2 , gender: " 男" , name: ' 李易峰' , age: ' 32' , info: ' 1987年5月4日出生于四川成都,中国内地男演员、流行乐歌手、影视制片人' , },
998
+ { id: 3 , gender: " 男" , name: ' 范冰冰' , age: ' 23' , info: ' 1981年9月16日出生于山东青岛,中国影视女演员、制片人、流行乐女歌手' , },
999
+ { id: 4 , gender: " 男" , name: ' 杨幂' , age: ' 34' , info: ' 1986年9月12日出生于北京市,中国内地影视女演员、流行乐歌手、影视制片人。' },
1000
+ { id: 5 , gender: " 男" , name: ' Angelababy' , age: ' 54' , info: ' 1989年2月28日出生于上海市,华语影视女演员、时尚模特。' , },
1001
+ { id: 6 , gender: " 男" , name: ' 唐嫣' , age: ' 12' , info: ' 1983年12月6日出生于上海市,毕业于中央戏剧学院表演系本科班' , },
1002
+ { id: 7 , gender: " 男" , name: ' 吴亦凡' , age: ' 4' , info: ' 1990年11月06日出生于广东省广州市,华语影视男演员、流行乐歌手。' , },
1003
+ ];
1004
+ const EditInput = ({edit, value, type, onChange, component, ... other}) => {
1005
+ const [isEdit , setIsEdit ] = React .useState (edit);
1006
+ const inputRef = React .useRef ();
1007
+ React .useEffect (()=> {
1008
+ isEdit && inputRef .current .focus ()
1009
+ }, [isEdit])
1010
+ if (isEdit) {
1011
+ if (component === " Select" ) {
1012
+ return < Select
1013
+ ref= {inputRef}
1014
+ defaultValue= {value}
1015
+ onBlur= {e => {
1016
+ onChange (e .target .value )
1017
+ setIsEdit (false )
1018
+ }}
1019
+ >
1020
+ < Select .Option value= " 男" > 男< / Select .Option >
1021
+ < Select .Option value= " 女" > 女< / Select .Option >
1022
+ < / Select>
1023
+ }
1024
+ return < Input
1025
+ {... other}
1026
+ defaultValue= {value}
1027
+ ref= {inputRef}
1028
+ type= {type || " text" }
1029
+ onBlur= {(e )=> {
1030
+ onChange (e .target .value )
1031
+ setIsEdit (false )
1032
+ }}
1033
+ / >
1034
+ }
1035
+ return < div onClick= {()=> {
1036
+ setIsEdit (true )
1037
+ }}> {value}< / div>
1038
+ }
1039
+ const Demo = () => {
1040
+ const [data , setData ] = React .useState (dataSource)
1041
+ const onChange = (value , id ) => {
1042
+ setData (data .map (it => it .id === id ? value : it))
1043
+ }
1044
+ return < div>
1045
+ < Table columns= {columns (onChange)} data= {data} / >
1046
+ < / div>
1047
+ };
1048
+ ReactDOM .render (< Demo / > , _mount_);
1049
+ ```
1050
+
1051
+ ### 可编辑行
1052
+
1053
+ 利用 Form 组件和 render 属性, 实现编辑行效果
1054
+
1055
+ <!-- rehype:bgWhite=true&codeSandbox=true&codePen=true-->
1056
+ ``` jsx
1057
+ import ReactDOM from ' react-dom' ;
1058
+ import { Table , Button , Input , Select , Form , Notify } from ' uiw' ;
1059
+
1060
+ const columns = (actived , setChange , fields ) => {
1061
+ return [
1062
+ {
1063
+ title: ' 姓名' ,
1064
+ ellipsis: true ,
1065
+ key: ' name' ,
1066
+ render : (text ,key , rowData ) => {
1067
+ if (rowData .id === actived) return fields .name
1068
+ return text
1069
+ }
1070
+ },
1071
+ {
1072
+ title: ' 性别' ,
1073
+ key: ' gender' ,
1074
+ render : (text ,key , rowData ) => {
1075
+ if (rowData .id === actived) return fields .gender
1076
+ return text
1077
+ }
1078
+ },
1079
+ {
1080
+ title: ' 年龄' ,
1081
+ key: ' age' ,
1082
+ render : (text ,key , rowData ) => {
1083
+ if (rowData .id === actived) return fields .age
1084
+ return text
1085
+ }
1086
+ },
1087
+ {
1088
+ key: ' id' ,
1089
+ render : (id , key , rowData )=> {
1090
+ const flag = id === actived
1091
+ if (flag){
1092
+ return <>
1093
+ < Button type= " primary" htmlType= " submit" > Save< / Button>
1094
+ < Button type= " danger" onClick= {()=> setChange ({})}> Cancel< / Button>
1095
+ < / >
1096
+ }
1097
+ return < Button type= " primary" onClick= {()=> setChange (rowData)} disabled= {actived !== undefined && ! flag}> Edit< / Button>
1098
+ }
1099
+ }
1100
+ ]
1101
+ };
1102
+ const dataSource = [
1103
+ { id: 1 , gender: " 男" , name: ' 邓紫棋' , age: ' 12' , },
1104
+ { id: 2 , gender: " 男" , name: ' 李易峰' , age: ' 32' , },
1105
+ { id: 3 , gender: " 男" , name: ' 范冰冰' , age: ' 23' , },
1106
+ { id: 4 , gender: " 男" , name: ' 杨幂' , age: ' 34' ,},
1107
+ { id: 5 , gender: " 男" , name: ' Angelababy' , age: ' 54' , },
1108
+ { id: 6 , gender: " 男" , name: ' 唐嫣' , age: ' 12' , },
1109
+ { id: 7 , gender: " 男" , name: ' 吴亦凡' , age: ' 4' , },
1110
+ ];
1111
+ const Demo = () => {
1112
+ const [data , setData ] = React .useState (dataSource)
1113
+ const [itemRowData , setItemRowData ] = React .useState ({})
1114
+ const [id , setId ] = React .useState ()
1115
+
1116
+ const form = React .useRef ()
1117
+ const setChange = (rowData ) => {
1118
+ setId (rowData .id )
1119
+ setItemRowData (rowData)
1120
+ form .current .setFields (rowData)
1121
+ }
1122
+
1123
+ return < Form
1124
+ ref= {form}
1125
+ onSubmit= {({ current }) => {
1126
+ if (JSON .stringify (itemRowData) === JSON .stringify (current)) {
1127
+ Notify .error ({
1128
+ title: ' 提交失败!' ,
1129
+ description: ` 表单提交内容并没有修改!` ,
1130
+ });
1131
+ return
1132
+ }
1133
+ setData (data .map (it => it .id === current .id ? current : it))
1134
+ setId (undefined )
1135
+ Notify .success ({
1136
+ title: ' 提交成功!' ,
1137
+ description: ' 数据已经更新'
1138
+ });
1139
+ }}
1140
+ fields= {{
1141
+ name: {
1142
+ children: < Input placeholder= " 请输入姓名" / >
1143
+ },
1144
+ age: {
1145
+ children: < Input placeholder= " 请输入年龄" type= " number" / >
1146
+ },
1147
+ gender: {
1148
+ children: < Select>
1149
+ < Select .Option value= " 男" > 男< / Select .Option >
1150
+ < Select .Option value= " 女" > 女< / Select .Option >
1151
+ < / Select>
1152
+ }
1153
+ }}
1154
+ >
1155
+ {({ fields, state, canSubmit }) => {
1156
+ return (
1157
+ < div>
1158
+ < Table columns= {columns (id, setChange, fields)} data= {data} / >
1159
+ < / div>
1160
+ )
1161
+ }}
1162
+ < / Form>
1163
+ };
1164
+ ReactDOM .render (< Demo / > , _mount_);
1165
+ ```
1166
+
933
1167
## Props
934
1168
935
1169
### Table
0 commit comments