# react-admin-template
**Repository Path**: faithXiao/react-admin-template
## Basic Information
- **Project Name**: react-admin-template
- **Description**: 基于react的中后台项目模板,封装常用业务组件,包含基本的按钮,标签,表单,表格,提示框,弹出层,树形选择器,图片裁剪,跑马灯等通用业务组件。
- **Primary Language**: JavaScript
- **License**: MIT
- **Default Branch**: master
- **Homepage**: http://42.193.144.245/react-admin-template
- **GVP Project**: No
## Statistics
- **Stars**: 9
- **Forks**: 2
- **Created**: 2021-03-05
- **Last Updated**: 2025-03-20
## Categories & Tags
**Categories**: Uncategorized
**Tags**: React, 中后台模板, UI组件
## README
# react-admin-template
- [react-admin-template](#react-admin-template)
- [安装](#安装)
- [路由管理](#路由管理)
- [业务组件](#业务组件)
- [Alert确认提示框](#alert确认提示框)
- [Toask轻提示](#toask轻提示)
- [Loading加载框](#loading加载框)
- [Dialog弹出层](#dialog弹出层)
- [Button按钮](#button按钮)
- [Tag标签](#tag标签)
- [Table表格](#table表格)
- [Pagination分页器](#pagination分页器)
- [Form](#form)
- [input输入框](#input输入框)
- [radio单选框](#radio单选框)
- [checkbox复选框](#checkbox复选框)
- [select选择框](#select选择框)
- [switch开关](#switch开关)
- [timePicker时间选择器](#timepicker时间选择器)
- [calendar日历选择器](#calendar日历选择器)
- [upload文件上传](#upload文件上传)
- [Form表单](#form表单)
- [表单验证](#表单验证)
- [Tree树形展开](#tree树形展开)
- [Crop图片裁剪](#crop图片裁剪)
- [Tooltip提示](#tooltip提示)
- [Carousel跑马灯](#carousel跑马灯)
## 安装
```
# 克隆项目
git clone https://gitee.com/faithXiao/react-admin-template.git
# 进入项目目录
cd react-admin-template
# 安装依赖
npm install
# 启动服务
npm run start
# 线上地址
http://42.193.144.245/react-admin-template
```
## 路由管理
```
src/router/index.js
import React from 'react'
import Layout from '../views/layout'
const routes = [
{
path: "/", //路径
redirect: '/home', //重定向地址
hidden: true //侧边栏隐藏
},
{
path: "/home",
redirect: '/home/index',
component: Layout, //公共页面组件(头部,侧边栏,页面主体)布局
name: 'home', //页面名称
children: [
{
path: "index",
component: React.lazy(() => import(/* webpackChunkName: "home" */ '../views/home/index')), //页面主体
name: 'home'
}
]
},
{
path: "/another",
component: Layout,
name: '一级',
children: [
{
path: 'a',
component: React.lazy(() => import(/* webpackChunkName: "another-a" */ '../views/another/a/index')),
name: '二级',
children: [
{
path: '1',
component: React.lazy(() => import(/* webpackChunkName: "another-a-1" */ '../views/another/a/a1/index')),
name: '三级-1'
},
{
path: '2',
component: React.lazy(() => import(/* webpackChunkName: "another-a-2" */ '../views/another/a/a2/index')),
name: '三级-2'
}
]
},
{
path: 'b',
component: React.lazy(() => import(/* webpackChunkName: "another-b" */ '../views/another/b/index')),
name: '二级'
},
{
path: 'params/:id',
component: React.lazy(() => import(/* webpackChunkName: "another" */ '../views/another/params/index')),
name: '二级(路由参数)',
hidden: true
}
]
},
{
path: "/404", //404页面
component: React.lazy(() => import(/* webpackChunkName: "404" */ '../views/404/index')),
name: '404',
hidden: true
},
{
path: "*", //url地址全部匹配不成功重定向404
redirect: '/404',
hidden: true
}
]
```
## 业务组件
### Alert确认提示框
```
import { Alert } from '@/component/index'
let alert = Alert()
alert.showAlert({
title: '提示', //标题
message: 'Alert', //提示消息
confirm: () => { //确认回调
},
cancel: () => { //取消,关闭回调
}
})
```
### Toask轻提示
```
import { Toask } from '@/component/index'
let toask = Toask()
toask.showToask({
type: 'success', 类型:success info warning error
title: 'success', 提示标题
duration: 1500 显示时长ms,若为0不自动关闭,有"x"关闭按钮可手动关闭
})
```
### Loading加载框
```
import { Loading } from '@/component/index'
let loading = Loading()
loading.showLoading('加载中...') //显示
setTimeout(() => {
loading.hideLoading() //关闭
}, 1500)
```
### Dialog弹出层
```
import { Dialog } from '@/component/index'
constructor(props){
super(props)
this.state = {
showDialog: false
}
}
open(){
this.setState({
showDialog: true
})
}
close(){
this.setState({
showDialog: false
})
}
render(){
return(
)
}
```
### Button按钮
```
import { Button } from '@/component/index'
render(){
return(
)
}
```
### Tag标签
```
import { Tag } from '@/component/index'
render(){
return(
标签
)
}
```
### Table表格
```
import { Table } from '@/component/index'
constructor(props){
super(props)
this.state = {
data: [
{
id: 1,
name: '张三',
sex: '男',
age: '18',
address: '广东'
},
{
id: 2,
name: '李四',
sex: '女',
age: '20',
address: '上海'
}
],
loading: false,
multipleSelectList: []
}
this.tableRef = React.createRef()
}
getItem(item, index){
console.log(item, index)
}
multipleSelect(value){
this.setState(state => {
state.multipleSelectList = value
return state
})
}
clearSelection(){ //清除表格多选
this.tableRef.current.clearSelection()
}
render(){
return(
//展开行在表格数据动态更新的时候不会自动消失(数据添加,删除,分页切换等),需调用clearExpand()手动清除
{ //返回数组每一项
return ...
//注:展开节点文字对齐方式会继承table对齐方式
}
}
/>
{
return new Promise(resolve => { //返回一个Promise对象
setTimeout(() => {
resolve(
...
)
}, 1000)
})
}
}
/>
//checkbox选择框,设置type="selection"
{ //返回当前操作的数据项item,下标index
return(
)
}
}
/>
)
}
```
### Pagination分页器
```
import { Pagination } from '@/component/index'
currentChange(e){ //e: 页码
console.log(e)
}
render(){
return(
)
}
```
### Form
```
import { Form } from '@/component/index' //Form是一个包含所有表单组件的对象,使用表单组件前需引入该对象
```
#### input输入框
```
constructor(props){
super(props)
this.state = {
value: ''
}
}
onChange(value){
console.log(value)
this.setstate({
value
})
}
render(){
return(
)
}
```
#### radio单选框
```
constructor(props){
super(props)
this.state = {
value: ''
}
}
onChange(value){
console.log(value)
this.setstate({
value
})
}
render(){
return(
)
}
```
#### checkbox复选框
```
constructor(props){
super(props)
this.state = {
value: []
}
}
onChange(value){
console.log(value)
this.setstate({
value
})
}
render(){
return(
)
}
```
#### select选择框
```
constructor(props){
super(props)
this.state = {
value: '' //若开启多选,则value应为数组[]
}
}
onChange(value){
console.log(value)
this.setstate({
value
})
}
render(){
return(
)
}
```
#### switch开关
```
constructor(props){
super(props)
this.state = {
value: ''
}
}
onChange(value){
console.log(value)
this.setstate({
value
})
}
render(){
return(
)
}
```
#### timePicker时间选择器
```
constructor(props){
super(props)
this.state = {
value: ''
}
}
onChange(value){
console.log(value)
this.setstate({
value
})
}
render(){
return(
)
}
```
#### calendar日历选择器
```
constructor(props){
super(props)
this.state = {
value: '' //日期对象new Date() 或 ''
}
}
onChange(value){
console.log(value)
this.setstate({
value
})
}
render(){
return(
)
}
```
#### upload文件上传
```
constructor(props){
super(props)
this.uploadRef = React.createRef()
}
onChange(file){
console.log(file)
}
render(){
return(
)
}
```
#### Form表单
在Form表单组件中,每一个表单域由一个 组件构成,表单域中可以放置各种类型的表单控件,包括 input、select、checkbox、radio、switch、calendar、timePicker、upload等
```
constructor(props){
super(props)
this.state = {
value: ''
}
}
onChange(value){
console.log(value)
this.setstate({
value
})
}
render(){
return(
)
}
```
##### 表单验证
```
constructor(props){
super(props)
this.state = {
inputValue: '',
passwordValue: ''
}
this.rules = { //验证对象
text(value){ //方法名称对应Form.item中的prop属性
if(value === ''){ //验证规则
return '不能为空' //验证不通过提示信息
}
return true //验证通过需返回true
},
password(value){
if(value === ''){
return '不能为空'
}else if(value.length < 6){
return '长度需大于6'
}
return true
}
}
this.rulesRef = React.createRef()
}
inputChange(value){
this.setstate({
inputValue: value
})
}
passwordChange(value){
this.setstate({
passwordValue: value
})
}
submit(){
this.rulesRef.current.validate(valid => { //调用表单验证判断方法
if(valid){
console.log('验证通过')
}
})
}
clearValidate(){
this.rulesRef.current.clearValidate() //清除表单验证提示
}
render(){
return(
)
}
```
### Tree树形展开
```
import { Tree } from '@/component/index'
constructor(props){
super(props)
this.state = {
//checked:0: 选中,1:半选中,2: 未选中
//只需给叶子节点设置checked属性选中状态,父级节点根据子节点状态更新自身选择状态
//叶子节点checked(0: 选中,2: 未选中),父级节点在checkboxChange回调中根据子节点状态,返回其自身的checked(0: 选中,1: 不定选,2: 未选中)
data: [
{
name: '一级-1',
children: [
{
name: '二级-1',
children: [
{
name: '三级-1',
checked: 0
},
{
name: '三级-2',
checked: 2
}
]
},
{
name: '二级-2',
checked: 2
}
]
},
{
name: '一级-2',
children: [
{
name: '二级-1',
checked: 0
},
{
name: '二级-2',
checked: 2
}
]
},
{
name: '一级-3',
checked: 2
}
]
}
this.replaceProps = {
label: 'name',
children: 'children',
checkedType: 'checked'
}
}
getNode = node => {
console.log(node)
}
checkboxChange = data => {
console.log(data)
}
render(){
return (
)
}
```
### Crop图片裁剪
```
import { Crop } from '@/component/index'
import img from '@/assets/images/crop.jpg'
constructor(props){
super(props)
this.state = {
cropResult: '',
img
}
}
getCorpImg = blob => {
let fileReader = new FileReader()
fileReader.readAsDataURL(blob)
fileReader.onload = e => {
this.setState({
cropResult: e.target.result
})
}
}
render(){
return(
{
this.state.cropResult
?
裁剪结果:
:
null
}
)
}
```
### Tooltip提示
```
import { Tooltip } from '@/component/index'
render(){
return(
bottom-left
bottom-center
bottom-right
)
}
```
### Carousel跑马灯
```
import { Carousel } from '@/component/index'
render(){
let arr = [1, 2, 3, 4, 5]
return (
{
console.log(current)
}
}
>
{
return arr.map((item, index) => {
return (
{ item }
)
})
}
}
/>
{
return (
)
}
}
/>
{
return (
)
}
}
/>
{
return { console.log(index) } }>
}
}
/>
{/* */} //轮播组件内置一套箭头、指示点分页展示组件,可省略自定义的渲染函数
{/* */}
{/* */}
)
}
```
### Progress进度条
```
import React, { useState } from "react"
import { Progress, Button } from '@/component/index'
export default function App(props) {
const [percent, setPresent] = useState(50)
return (
)
}
```