这个界面跟之前VUE做的一样。并无任何不同之处,只是用react重复实现了一遍。

import React, { useState, useEffect } from 'react';
import { Row, Col, Table, Form, Cascader, Input, Button, Modal, message } from 'antd';
import { FormComponentProps } from 'antd/lib/form';
import http from '../../service'; import './post_category.css'
import { PostCategoryTo } from '../../models/PostCategoryTo';
interface PostCategoryProps extends FormComponentProps { } const PostCategory: React.FC<PostCategoryProps> = (props) => { const scroll = { y: window.innerHeight - 200 };
const columns = [
{ title: '名称', dataIndex: 'name' },
{ title: '路径', dataIndex: 'code' }
];
const rowSelection = {
onChange: (selectedRowKeys: any, rows: any) => {
setSelectedRows(rows)
}
} const { getFieldDecorator } = props.form;
const [loading, setLoading] = useState(false)
const [dataSource, setDataSource] = useState([])
let [selectedRows, setSelectedRows] = useState([])
let [formModel, setFormModel] = useState(new PostCategoryTo())
const fieldNames = { label: 'name', value: 'uid', children: 'children'} useEffect(() => {
loadData()
}, []) function loadData() {
http.url('v1/post-category')
.get()
.json(res => {
setDataSource(Object.assign([], res))
})
} function handleAdd () {
setFormModel(new PostCategoryTo())
props.form.resetFields()
} function handleModify () {
if (selectedRows.length !== 1) {
message.warn('请选择一行数据进行操作')
return
} setFormModel(PostCategoryTo.fromObject(selectedRows[0]))
} function handleDelete () {
if (selectedRows.length === 0) {
message.warn('请选择一行数据进行操作')
return
} Modal.confirm({
title: '请确认你要删除这些项目',
onOk() {
const list = selectedRows.map((it:any) => it.uid)
http.url(`v1/post-category/${list.join(',')}`).delete()
.text(() => {
loadData()
message.success('删除成功')
}).catch(err => message.error(err.message))
},
onCancel() {},
});
} function handleSave() {
props.form.validateFields((err, values) => {
if (!err) {
const param = Object.assign({}, formModel, values)
console.log(param, formModel, values)
if (Array.isArray(values.parentUid)) {
param.parentUid = values.parentUid[values.parentUid.length - 1]
}
setLoading(true) let resp: any = null
if (param.uid) {
resp = http.url(`v1/post-category/${param.uid}`).put(param)
} else {
resp = http.url('v1/post-category').post(param)
} resp.json(res => {
setFormModel(PostCategoryTo.fromObject(res))
loadData()
message.success('保存成功')
})
.finally(() => {
setLoading(false)
})
}
});
} return (
<Row gutter={32}>
<Col span={6}>
<h3>新增分类</h3>
<Form>
<Form.Item label='名称' help='名称是它显示在网页上。'>
{getFieldDecorator('name', {initialValue: formModel.name, rules: [{ required: true, message: '请输入账号' }]})(
<Input id='name' placeholder='请输入名称'></Input>
)}
</Form.Item>
<Form.Item label='路径' help='它通常是地址栏里面的路径,它通常都是小写的,只包含字母、数字和连字符。'>
{getFieldDecorator('code', {initialValue: formModel.code})(
<Input id="code"></Input>
)}
</Form.Item>
<Form.Item label='父分类'>
{getFieldDecorator('parentUid', {initialValue: formModel.parentUid})(
<Cascader fieldNames={fieldNames} options={dataSource} placeholder='选择父分类'/>
)}
</Form.Item>
<Form.Item label='描述' help='默认情况下描述不显示;一些主题可能会显示这一点。'>
{getFieldDecorator('description', {initialValue: formModel.description})(
<Input.TextArea id="description"></Input.TextArea>
)}
</Form.Item>
<div className="button-save">
<Button type='primary' loading={loading} onClick={handleSave}>保存</Button>
</div>
</Form>
</Col>
<Col span={18}>
<div className='toolbar'>
<Button size='small' onClick={handleAdd}>新增</Button>
<Button type='primary' size='small' onClick={handleModify}>修改</Button>
<Button type='danger' size='small' onClick={handleDelete}>删除</Button>
</div>
<Table columns={columns} size="small" rowKey="uid" rowSelection={rowSelection} dataSource={dataSource} pagination={false} scroll={scroll}></Table>
</Col>
</Row>
) } export default Form.create()(PostCategory)

react typescript FunctionComponent antd crud的更多相关文章

  1. 【每天学一点-04】使用脚手架搭建 React+TypeScript+umi.js+Antd 项目

    一.使用脚手架搭建项目框架 1.首先使用脚手架搭建React项目(React+TypeScript+Umi.js) 在控制台输入命令:yarn create @umijs/umi-app 2.引入An ...

  2. react typescript jest config (一)

    1. initialize project create a folder project Now we'll turn this folder into an npm package. npm in ...

  3. react + react-router + less +antd 开发环境

    react + react-router + less +antd 开发环境搭建 1.基于create-reacte-app,需要先安装这个脚手架,然后初始化项目. 2.进入项目目录,首先 npm r ...

  4. react + typescript 学习

    react,前端三大框架之一,也是非常受开发者追捧的一门技术.而 typescript 是 javascript 的超集,主要特点是对 类型 的检查.二者的结合必然是趋势,不,已经是趋势了.react ...

  5. 十九、React UI框架Antd(Ant Design)的使用——及react Antd的使用 button组件 Icon组件 Layout组件 DatePicker日期组件

    一.Antd(Ant Design)的使用:引入全部Css样式 1.1 antd官网: https://ant.design/docs/react/introduce-cn 1.2 React中使用A ...

  6. React + Typescript领域初学者的常见问题和技巧

    React + Typescript领域初学者的常见问题和技巧 创建一个联合类型的常量 Key const NAME = { HOGE: "hoge", FUGA: "f ...

  7. Typescript + React 高仿 Antd 从零到一打造自己的组件库(完整)

    买了张轩老师的课程,感觉很不错,适用于高级进阶,老师讲的通俗易懂,欢迎讨论学习.WX:Jujiu_i

  8. 使用react搭建组件库:react+typescript+storybook

    前期准备 1. 初始化项目 npx create-react-app react-components --template typescript 2. 安装依赖 使用哪种打包方案:webpack/r ...

  9. 前端自动化测试 —— TDD环境配置(React+TypeScript)

    欢迎讨论与指导:) 前言 TDD -- Test-Drive Development是测试驱动开发的意思,是敏捷开发中的一项核心实践和技术,也是一种测试方法论.TDD的原理是在开发功能代码之前,先编写 ...

随机推荐

  1. [ubuntu]android SDK 与Gradle环境的安装与配置|搭建android基础开发/构建环境

    系统环境: linux:ubuntu18 已配置jdk 环境变量 切换到root账户 sudo su 安装Android-sdk (0)准备工作 切换到/usr/local目录: /usr/local ...

  2. springboot2.0+线程池+Jmeter以模拟高并发

    声明:原创在这里https://blog.csdn.net/u011677147/article/details/80271174,在此也谢谢哥们. 1.目录结构 2.BusinessThread.j ...

  3. 解决 ElementTree 无法处理中文

    解决 ElementTree 无法处理中文,UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 76-99: o ...

  4. electron-vue小试身手

    最近一个项目(vue)需求是用硬件来触发web端页面显示以及效果的切换,客户的硬件设备只支持用tcp协议通讯,而我们的前端呢是用不了tcp的,众所周知在浏览器端,我们只能用http/https协议(a ...

  5. java8:(Lambda 表达式,Supplier,@FunctionalInterface,foreach(),Optional,Stream().collect,双冒号,joining,partitioningBy分区,collectingAndThen,filter())

    1.Lambda 表达式: 引导:http://www.cnblogs.com/yulinfeng/p/8452379.html DEMO1: List<String> names1 = ...

  6. .NET制作滚动条

    今天,在工作的时候,刚好做到了滚动条,对这点不是很懂,所以,研究了一下,记录在这里,与大家分享. 对于前台页面,我们就只需要设置数据表的样式:style="overflow: auto; 即 ...

  7. pika常见问题解答(FAQ)

    1 编译安装 Q1: 支持的系统? A1: 目前只支持Linux环境,包括Centos,Ubuntu: 不支持Windowns, Mac Q2: 怎么编译安装? A2: 参考编译安装wiki Q3: ...

  8. 如何在google colab加载kaggle数据

    参考https://medium.com/@yvettewu.dw/tutorial-kaggle-api-google-colaboratory-1a054a382de0 从本地上传到colab上十 ...

  9. URLOS开发基础教程——docker容器的使用方法

    URLOS本是基于docker容器运行,在入门URLOS开发之前,我们首先需要掌握docker的相关基础知识,本篇就以docker容器的基本使用方法为例,快速的让大家对docker有一个全面的印象. ...

  10. Ubuntu下c程序运行原理

    运行环境: 1.Vitural box下安装Ubuntu虚拟机 2.编译运行工具:gcc 3.编辑器:vim 主要过程: 1.利用vim编辑hello.c 2.编译和执行 (1)预处理阶段:使用 -E ...