Skip to content

Commit

Permalink
types: 类型抽离
Browse files Browse the repository at this point in the history
  • Loading branch information
xkfe committed Aug 5, 2024
1 parent 82f8d8e commit e8efd7a
Show file tree
Hide file tree
Showing 19 changed files with 216 additions and 154 deletions.
2 changes: 1 addition & 1 deletion packages/components/Grid/components/GridItem.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts" name="GridItem">
import type { Ref } from 'vue'
import { computed, inject, ref, useAttrs, watch } from 'vue'
import type { BreakPoint, Responsive } from '../interface/index'
import type { BreakPoint, Responsive } from '../../../types'
interface Props {
offset?: number
Expand Down
2 changes: 1 addition & 1 deletion packages/components/Grid/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
useSlots,
watch,
} from 'vue'
import type { BreakPoint } from './interface/index'
import type { BreakPoint } from '../../types'
interface Props {
cols?: number | Record<BreakPoint, number>
Expand Down
2 changes: 1 addition & 1 deletion packages/components/ProTable/components/ColSetting.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { ref } from 'vue'
import type { ColumnProps } from '../interface'
import type { ColumnProps } from '../../../types'
defineOptions({ name: 'ColSetting' })
defineProps<{ colSetting: ColumnProps[] }>()
Expand Down
12 changes: 1 addition & 11 deletions packages/components/ProTable/components/Pagination.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
<script setup lang="ts">
interface Pageable {
pageNum: number
pageSize: number
total: number
}
interface PaginationProps {
pageable: Pageable
handleSizeChange: (size: number) => void
handleCurrentChange: (currentPage: number) => void
}
import type { PaginationProps } from '../../../types'
defineOptions({ name: 'Pagination' })
defineProps<PaginationProps>()
Expand Down
2 changes: 1 addition & 1 deletion packages/components/ProTable/components/TableColumn.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="tsx" name="TableColumn">
import { inject, ref, useSlots } from 'vue'
import type { ColumnProps, HeaderRenderScope, RenderScope } from '../interface'
import type { ColumnProps, HeaderRenderScope, RenderScope } from '../../../types'
import { filterEnum, formatValue, handleProp, handleRowAccordingToProp } from '../../../utils'
defineProps<{ column: ColumnProps }>()
Expand Down
19 changes: 1 addition & 18 deletions packages/components/ProTable/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,11 @@ import Sortable from 'sortablejs'
import SearchForm from '../SearchForm/index.vue'
import { useTable } from '../../hooks/useTable'
import { useSelection } from '../../hooks/useSelection'
import type { BreakPoint } from '../Grid/interface'
import { generateUUID, handleProp } from '../../utils'
import type { ColumnProps, ProTableProps, TypeProps } from '../../types'
import Pagination from './components/Pagination.vue'
import ColSetting from './components/ColSetting.vue'
import TableColumn from './components/TableColumn.vue'
import type { ColumnProps, TypeProps } from './interface'
export interface ProTableProps {
columns: ColumnProps[] // 列配置项 ==> 必传
data?: any[] // 静态 table data 数据,若存在则不会使用 requestApi 返回的 data ==> 非必传
requestApi?: (params: any) => Promise<any> // 请求表格数据的 api ==> 非必传
requestAuto?: boolean // 是否自动执行请求 api ==> 非必传(默认为true)
requestError?: (params: any) => void // 表格 api 请求错误监听 ==> 非必传
dataCallback?: (data: any) => any // 返回数据的回调函数,可以对数据进行处理 ==> 非必传
title?: string // 表格标题 ==> 非必传
pagination?: boolean // 是否需要分页组件 ==> 非必传(默认为true)
initParam?: any // 初始化请求参数 ==> 非必传(默认为{})
border?: boolean // 是否带有纵向边框 ==> 非必传(默认为true)
toolButton?: ('refresh' | 'setting' | 'search')[] | boolean // 是否显示表格功能按钮 ==> 非必传(默认为true)
rowKey?: string // 行数据的 Key,用来优化 Table 的渲染,当表格数据多选时,所指定的 id ==> 非必传(默认为 id)
searchCol?: number | Record<BreakPoint, number> // 表格搜索项 每列占比配置 ==> 非必传 { xs: 1, sm: 2, md: 2, lg: 3, xl: 4 }
}
// 接受父组件参数,配置默认值
const props = withDefaults(defineProps<ProTableProps>(), {
Expand Down
86 changes: 0 additions & 86 deletions packages/components/ProTable/interface/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { computed, inject, ref } from 'vue'
import type { ColumnProps } from '../../ProTable/interface'
import type { ColumnProps } from '../../../types'
import { handleProp } from '../../../utils'
interface SearchFormItem {
Expand Down
3 changes: 1 addition & 2 deletions packages/components/SearchForm/index.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<script setup lang="ts">
import { computed, ref } from 'vue'
import { ArrowDown, ArrowUp, Delete, Search } from '@element-plus/icons-vue'
import type { ColumnProps } from '../ProTable/interface'
import type { BreakPoint } from '../Grid/interface'
import type { BreakPoint, ColumnProps } from '../../types'
import Grid from '../Grid/index.vue'
import GridItem from '../Grid/components/GridItem.vue'
import SearchFormItem from './components/SearchFormItem.vue'
Expand Down
35 changes: 15 additions & 20 deletions packages/hooks/interface/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
export namespace Table {
export interface Pageable {
pageNum: number
pageSize: number
total: number
import type { Pageable } from '../../types'

export interface StateProps {
tableData: any[]
pageable: Pageable
searchParam: {
[key: string]: any
}
export interface StateProps {
tableData: any[]
pageable: Pageable
searchParam: {
[key: string]: any
}
searchInitParam: {
[key: string]: any
}
totalParam: {
[key: string]: any
}
icon?: {
[key: string]: any
}
searchInitParam: {
[key: string]: any
}
totalParam: {
[key: string]: any
}
icon?: {
[key: string]: any
}
}
6 changes: 3 additions & 3 deletions packages/hooks/useTable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { computed, reactive, toRefs } from 'vue'
import type { Table } from './interface'
import type { StateProps } from './interface'

/**
* @description table 页面操作方法封装
Expand All @@ -9,7 +9,7 @@ import type { Table } from './interface'
* @param {Function} dataCallBack 对后台返回的数据进行处理的方法 (非必传)
*/
export function useTable(api?: (params: any) => Promise<any>, initParam: object = {}, isPageable: boolean = true, dataCallBack?: (data: any) => any, requestError?: (error: any) => void) {
const state = reactive<Table.StateProps>({
const state = reactive<StateProps>({
// 表格数据
tableData: [],
// 分页数据
Expand Down Expand Up @@ -74,7 +74,7 @@ export function useTable(api?: (params: any) => Promise<any>, initParam: object
const updatedTotalParam = () => {
state.totalParam = {}
// 处理查询参数,可以给查询参数加自定义前缀操作
const nowSearchParam: Table.StateProps['searchParam'] = {}
const nowSearchParam: StateProps['searchParam'] = {}
// 防止手动清空输入框携带参数(这里可以自定义查询参数前缀)
for (const key in state.searchParam) {
// 某些情况下参数为 false/0 也应该携带参数
Expand Down
4 changes: 2 additions & 2 deletions packages/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { App } from 'vue'
import Table from './components/ProTable/index.vue'

export const GeekerTable = Object.assign(Table, {
export const GeekerTable = {
install: (app: App) => {
app.component('GeekerTable', Table)
},
})
}

export default GeekerTable
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type BreakPoint = 'xs' | 'sm' | 'md' | 'lg' | 'xl'

export interface Responsive {
span?: number
offset?: number
}
export type BreakPoint = 'xs' | 'sm' | 'md' | 'lg' | 'xl'

export interface Responsive {
span?: number
offset?: number
}
37 changes: 37 additions & 0 deletions packages/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { ComponentPublicInstance } from 'vue'
import type ProTable from '../components/ProTable/index.vue'
import type { ProTableProps } from './table-props'
import type { ColumnProps, EnumProps, FieldNamesProps, HeaderRenderScope, RenderScope, TypeProps } from './table-column'
import type { SearchProps, SearchRenderScope, SearchType } from './search-props'
import type { BreakPoint, Responsive } from './grid'
import type { Pageable, PaginationProps } from './pagination'

type ProTableInstance = Omit<InstanceType<typeof ProTable>, keyof ComponentPublicInstance | keyof ProTableProps>

export type {
// table-props
ProTableProps,

// table-column
ColumnProps,
EnumProps,
FieldNamesProps,
HeaderRenderScope,
RenderScope,
TypeProps,

// search-props
SearchProps,
SearchRenderScope,
SearchType,

// grid
BreakPoint,
Responsive,

// pagination
PaginationProps,
Pageable,

ProTableInstance,
}
11 changes: 11 additions & 0 deletions packages/types/pagination.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface Pageable {
pageNum: number
pageSize: number
total: number
}

export interface PaginationProps {
pageable: Pageable
handleSizeChange: (size: number) => void
handleCurrentChange: (currentPage: number) => void
}
37 changes: 37 additions & 0 deletions packages/types/search-props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { Ref, VNode } from 'vue'
import type { EnumProps } from './table-column'
import type { BreakPoint, Responsive } from './grid'

export type SearchType =
| 'input'
| 'input-number'
| 'select'
| 'select-v2'
| 'tree-select'
| 'cascader'
| 'date-picker'
| 'time-picker'
| 'time-select'
| 'switch'
| 'slider'

export interface SearchRenderScope {
searchParam: { [key: string]: any }
placeholder: string
clearable: boolean
options: EnumProps[]
data: EnumProps[]
}

export type SearchProps = {
el?: SearchType // 当前项搜索框的类型
label?: string // 当前项搜索框的 label
props?: any // 搜索项参数,根据 element plus 官方文档来传递,该属性所有值会透传到组件
key?: string // 当搜索项 key 不为 prop 属性时,可通过 key 指定
tooltip?: string // 搜索提示
order?: number // 搜索项排序(从大到小)
span?: number // 搜索项所占用的列数,默认为 1 列
offset?: number // 搜索字段左侧偏移列数
defaultValue?: string | number | boolean | any[] | Ref<any> // 搜索项默认值
render?: (scope: SearchRenderScope) => VNode // 自定义搜索内容渲染(tsx语法)
} & Partial<Record<BreakPoint, Responsive>>
Loading

0 comments on commit e8efd7a

Please sign in to comment.