react-toolkits 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +2 -0
- package/.eslintrc.js +4 -0
- package/.turbo/turbo-build.log +20 -0
- package/CHANGELOG.md +10 -0
- package/dist/512_orange_nobackground-L6MFCL6M.png +0 -0
- package/dist/index.css +230 -0
- package/dist/index.css.map +1 -0
- package/dist/index.d.mts +191 -0
- package/dist/index.esm.js +3152 -0
- package/dist/index.esm.js.map +1 -0
- package/package.json +63 -0
- package/postcss.config.js +9 -0
- package/src/assets/512_orange_nobackground.png +0 -0
- package/src/components/DynamicTags/index.tsx +160 -0
- package/src/components/FilterForm/index.tsx +62 -0
- package/src/components/FormModal/hooks.tsx +48 -0
- package/src/components/FormModal/index.tsx +138 -0
- package/src/components/Highlight/index.tsx +51 -0
- package/src/components/PermissionButton/index.tsx +36 -0
- package/src/components/QueryList/index.tsx +158 -0
- package/src/components/index.ts +27 -0
- package/src/constants/index.ts +3 -0
- package/src/features/permission/components/PermissionList.tsx +129 -0
- package/src/features/permission/hooks/index.ts +140 -0
- package/src/features/permission/index.ts +5 -0
- package/src/features/permission/types/index.ts +33 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/use-fetcher.tsx +102 -0
- package/src/hooks/use-permission.tsx +58 -0
- package/src/index.ts +7 -0
- package/src/layouts/Layout.tsx +81 -0
- package/src/layouts/NavBar.tsx +176 -0
- package/src/layouts/index.ts +6 -0
- package/src/pages/Login/default.tsx +864 -0
- package/src/pages/Login/index.tsx +111 -0
- package/src/pages/index.ts +4 -0
- package/src/pages/permission/RoleDetail.tsx +40 -0
- package/src/pages/permission/RoleList.tsx +226 -0
- package/src/pages/permission/UserList.tsx +248 -0
- package/src/pages/permission/index.tsx +31 -0
- package/src/shims.d.ts +20 -0
- package/src/stores/index.ts +3 -0
- package/src/stores/menu.ts +27 -0
- package/src/stores/queryTrigger.ts +27 -0
- package/src/stores/token.ts +25 -0
- package/src/styles/index.css +5 -0
- package/src/types/index.ts +27 -0
- package/tailwind.config.js +5 -0
- package/tsconfig.json +11 -0
- package/tsup.config.ts +26 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { PlusOutlined } from '@ant-design/icons'
|
|
2
|
+
import type { InputRef } from 'antd'
|
|
3
|
+
import { Input, Space, Tag, theme } from 'antd'
|
|
4
|
+
import type { FC } from 'react'
|
|
5
|
+
import { useEffect, useRef, useState } from 'react'
|
|
6
|
+
|
|
7
|
+
export interface DynamicTagsProps {
|
|
8
|
+
initialTags?: string[]
|
|
9
|
+
addable?: boolean
|
|
10
|
+
removable?: boolean
|
|
11
|
+
// 返回 Promise。如果返回 Promise.resolve(true),则添加; 如果返回 Promise.resolve(false) 或 Promise.reject,则不添加
|
|
12
|
+
addCallback?: (addedTag: string) => Promise<boolean>
|
|
13
|
+
// 返回 Promise。如果返回 Promise.resolve(true),则删除; 如果返回 Promise.resolve(false) 或 Promise.reject,则不删除
|
|
14
|
+
removeCallback?: (removedTag: string) => Promise<boolean>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const DynamicTags: FC<DynamicTagsProps> = props => {
|
|
18
|
+
const { initialTags, addable, removable, addCallback, removeCallback } = props
|
|
19
|
+
const { token } = theme.useToken()
|
|
20
|
+
const [tags, setTags] = useState<string[]>([])
|
|
21
|
+
const [inputVisible, setInputVisible] = useState(false)
|
|
22
|
+
const [inputValue, setInputValue] = useState('')
|
|
23
|
+
const [editInputIndex, setEditInputIndex] = useState(-1)
|
|
24
|
+
const [editInputValue, setEditInputValue] = useState<string>('')
|
|
25
|
+
const inputRef = useRef<InputRef>(null)
|
|
26
|
+
const editInputRef = useRef<InputRef>(null)
|
|
27
|
+
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
setTags(initialTags ?? [])
|
|
30
|
+
}, [initialTags])
|
|
31
|
+
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
if (inputVisible) {
|
|
34
|
+
inputRef.current?.focus()
|
|
35
|
+
}
|
|
36
|
+
}, [inputVisible])
|
|
37
|
+
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
editInputRef.current?.focus()
|
|
40
|
+
}, [inputValue])
|
|
41
|
+
|
|
42
|
+
const handleClose = async (removedTag: string) => {
|
|
43
|
+
const success = await removeCallback?.(removedTag)
|
|
44
|
+
|
|
45
|
+
if (success) {
|
|
46
|
+
const newTags = tags.filter(tag => tag !== removedTag)
|
|
47
|
+
setTags(newTags)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const showInput = () => {
|
|
52
|
+
setInputVisible(true)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
56
|
+
setInputValue(e.target.value)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const handleInputConfirm = async () => {
|
|
60
|
+
if (inputValue && tags.indexOf(inputValue) === -1) {
|
|
61
|
+
const success = await addCallback?.(inputValue)
|
|
62
|
+
|
|
63
|
+
if (success) {
|
|
64
|
+
setTags([...tags, inputValue])
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
setInputVisible(false)
|
|
69
|
+
setInputValue('')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const handleEditInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
73
|
+
setEditInputValue(e.target.value)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const handleEditInputConfirm = () => {
|
|
77
|
+
const newTags = [...tags]
|
|
78
|
+
newTags[editInputIndex] = editInputValue
|
|
79
|
+
setTags(newTags)
|
|
80
|
+
setEditInputIndex(-1)
|
|
81
|
+
setInputValue('')
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const tagInputStyle: React.CSSProperties = {
|
|
85
|
+
width: 78,
|
|
86
|
+
verticalAlign: 'top',
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const tagPlusStyle: React.CSSProperties = {
|
|
90
|
+
background: token.colorBgContainer,
|
|
91
|
+
borderStyle: 'dashed',
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<Space wrap size={[0, 8]}>
|
|
96
|
+
<Space wrap size={[0, 8]}>
|
|
97
|
+
{tags.map((tag, index) => {
|
|
98
|
+
if (editInputIndex === index) {
|
|
99
|
+
return (
|
|
100
|
+
<Input
|
|
101
|
+
ref={editInputRef}
|
|
102
|
+
key={tag}
|
|
103
|
+
size="small"
|
|
104
|
+
style={tagInputStyle}
|
|
105
|
+
value={editInputValue}
|
|
106
|
+
onChange={handleEditInputChange}
|
|
107
|
+
onBlur={handleEditInputConfirm}
|
|
108
|
+
onPressEnter={handleEditInputConfirm}
|
|
109
|
+
/>
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return (
|
|
114
|
+
<Tag
|
|
115
|
+
key={tag}
|
|
116
|
+
closable={removable}
|
|
117
|
+
style={{ userSelect: 'none' }}
|
|
118
|
+
onClose={async e => {
|
|
119
|
+
e.preventDefault()
|
|
120
|
+
await handleClose(tag)
|
|
121
|
+
}}
|
|
122
|
+
>
|
|
123
|
+
<span
|
|
124
|
+
onDoubleClick={e => {
|
|
125
|
+
if (index !== 0) {
|
|
126
|
+
setEditInputIndex(index)
|
|
127
|
+
setEditInputValue(tag)
|
|
128
|
+
e.preventDefault()
|
|
129
|
+
}
|
|
130
|
+
}}
|
|
131
|
+
>
|
|
132
|
+
{tag}
|
|
133
|
+
</span>
|
|
134
|
+
</Tag>
|
|
135
|
+
)
|
|
136
|
+
})}
|
|
137
|
+
</Space>
|
|
138
|
+
{addable &&
|
|
139
|
+
(inputVisible ? (
|
|
140
|
+
<Input
|
|
141
|
+
ref={inputRef}
|
|
142
|
+
type="text"
|
|
143
|
+
size="small"
|
|
144
|
+
style={tagInputStyle}
|
|
145
|
+
value={inputValue}
|
|
146
|
+
onChange={handleInputChange}
|
|
147
|
+
onBlur={handleInputConfirm}
|
|
148
|
+
onPressEnter={handleInputConfirm}
|
|
149
|
+
/>
|
|
150
|
+
) : (
|
|
151
|
+
<Tag style={tagPlusStyle} onClick={showInput}>
|
|
152
|
+
<PlusOutlined />
|
|
153
|
+
添加
|
|
154
|
+
</Tag>
|
|
155
|
+
))}
|
|
156
|
+
</Space>
|
|
157
|
+
)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export default DynamicTags
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { FormInstance, FormProps } from 'antd'
|
|
2
|
+
import { Button, Col, Form, Row, Space, theme } from 'antd'
|
|
3
|
+
import type { PropsWithChildren } from 'react'
|
|
4
|
+
|
|
5
|
+
export interface FilterFormProps<Values>
|
|
6
|
+
extends Pick<
|
|
7
|
+
FormProps<Values>,
|
|
8
|
+
| 'initialValues'
|
|
9
|
+
| 'requiredMark'
|
|
10
|
+
| 'onError'
|
|
11
|
+
| 'onChange'
|
|
12
|
+
| 'onValuesChange'
|
|
13
|
+
| 'onFinish'
|
|
14
|
+
| 'onFinishFailed'
|
|
15
|
+
| 'layout'
|
|
16
|
+
| 'labelCol'
|
|
17
|
+
> {
|
|
18
|
+
form?: FormInstance<Values>
|
|
19
|
+
onReset?: () => void
|
|
20
|
+
confirmText?: React.ReactNode
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const FilterForm = <Values = unknown,>(props: PropsWithChildren<FilterFormProps<Values>>) => {
|
|
24
|
+
const { children, confirmText, form, onReset, ...restProps } = props
|
|
25
|
+
const { token } = theme.useToken()
|
|
26
|
+
|
|
27
|
+
const formStyle = {
|
|
28
|
+
maxWidth: 'none',
|
|
29
|
+
background: token.colorFillAlter,
|
|
30
|
+
borderWidth: token.lineWidth,
|
|
31
|
+
borderStyle: token.lineType,
|
|
32
|
+
borderColor: token.colorBorder,
|
|
33
|
+
borderRadius: token.borderRadiusLG,
|
|
34
|
+
padding: 24,
|
|
35
|
+
marginBottom: 24,
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<Form {...restProps} form={form} autoComplete="off">
|
|
40
|
+
{children && (
|
|
41
|
+
<div style={formStyle}>
|
|
42
|
+
<Row gutter={18}>
|
|
43
|
+
{children}
|
|
44
|
+
<Col flex="auto" />
|
|
45
|
+
<Col flex="auto" span={24} style={{ textAlign: 'right' }}>
|
|
46
|
+
<Space>
|
|
47
|
+
<Button type="primary" htmlType="submit">
|
|
48
|
+
{confirmText || '查询'}
|
|
49
|
+
</Button>
|
|
50
|
+
<Button htmlType="reset" onClick={onReset}>
|
|
51
|
+
重置
|
|
52
|
+
</Button>
|
|
53
|
+
</Space>
|
|
54
|
+
</Col>
|
|
55
|
+
</Row>
|
|
56
|
+
</div>
|
|
57
|
+
)}
|
|
58
|
+
</Form>
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export default FilterForm
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { FormModalProps, FormModalRef, RecursivePartial } from './index'
|
|
2
|
+
import FormModal from './index'
|
|
3
|
+
import { useCallback, useMemo, useRef, useState } from 'react'
|
|
4
|
+
import type { Merge } from 'ts-essentials'
|
|
5
|
+
import { createPortal } from 'react-dom'
|
|
6
|
+
|
|
7
|
+
export type UseFormModalProps<T> = Merge<
|
|
8
|
+
Omit<FormModalProps<T>, 'open' | 'onCancel' | 'closeFn' | 'children' | 'onConfirm'>,
|
|
9
|
+
{
|
|
10
|
+
content: FormModalProps<T>['children']
|
|
11
|
+
onConfirm?: (values: T) => void
|
|
12
|
+
}
|
|
13
|
+
>
|
|
14
|
+
|
|
15
|
+
export function useFormModal<T extends object>(props: UseFormModalProps<T>) {
|
|
16
|
+
const { content, onConfirm, ...restProps } = props
|
|
17
|
+
const [open, setOpen] = useState(false)
|
|
18
|
+
const [title, setTitle] = useState<FormModalProps<T>['title']>()
|
|
19
|
+
const formRef = useRef<FormModalRef>(null)
|
|
20
|
+
|
|
21
|
+
const showModal = (options?: { initialValues?: RecursivePartial<T>; title?: FormModalProps<T>['title'] }) => {
|
|
22
|
+
setTitle(options?.title ?? restProps.title)
|
|
23
|
+
|
|
24
|
+
if (options?.initialValues) {
|
|
25
|
+
formRef.current?.setFieldsValue(options?.initialValues)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
setOpen(true)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const closeModal = useCallback(() => {
|
|
32
|
+
setOpen(false)
|
|
33
|
+
}, [])
|
|
34
|
+
|
|
35
|
+
const Modal = useMemo(() => {
|
|
36
|
+
return (
|
|
37
|
+
<FormModal {...restProps} open={open} closeFn={closeModal} title={title} onConfirm={onConfirm}>
|
|
38
|
+
{content}
|
|
39
|
+
</FormModal>
|
|
40
|
+
)
|
|
41
|
+
}, [title, content, restProps, open, closeModal, onConfirm])
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
Modal: createPortal(Modal, document.body),
|
|
45
|
+
showModal,
|
|
46
|
+
closeModal,
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/* eslint-disable react/jsx-indent */
|
|
2
|
+
import type { FormInstance, FormProps, ModalProps } from 'antd'
|
|
3
|
+
import { Button, Form, Modal } from 'antd'
|
|
4
|
+
import type { ForwardedRef } from 'react'
|
|
5
|
+
import { forwardRef, useId, useImperativeHandle, useRef, useState } from 'react'
|
|
6
|
+
|
|
7
|
+
type RenderChildren<T> = (props: {
|
|
8
|
+
form: FormInstance<T>
|
|
9
|
+
open?: boolean
|
|
10
|
+
closeFn?: VoidFunction
|
|
11
|
+
}) => JSX.Element | JSX.Element[]
|
|
12
|
+
|
|
13
|
+
type ChildrenType<T> = RenderChildren<T> | JSX.Element | JSX.Element[]
|
|
14
|
+
|
|
15
|
+
export type RecursivePartial<T> = T extends object
|
|
16
|
+
? {
|
|
17
|
+
[P in keyof T]?: T[P] extends (infer U)[]
|
|
18
|
+
? RecursivePartial<U>[]
|
|
19
|
+
: T[P] extends object
|
|
20
|
+
? RecursivePartial<T[P]>
|
|
21
|
+
: T[P]
|
|
22
|
+
}
|
|
23
|
+
: any
|
|
24
|
+
|
|
25
|
+
export interface FormModalProps<T>
|
|
26
|
+
extends Pick<ModalProps, 'width' | 'title' | 'open' | 'afterClose' | 'bodyStyle' | 'maskClosable'>,
|
|
27
|
+
Pick<FormProps, 'labelCol' | 'layout' | 'colon'> {
|
|
28
|
+
children?: ChildrenType<T>
|
|
29
|
+
footer?: ModalProps['footer']
|
|
30
|
+
closeFn?: VoidFunction
|
|
31
|
+
initialValues?: RecursivePartial<T>
|
|
32
|
+
onConfirm?: (values: T) => void
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface FormModalRef<T = object> {
|
|
36
|
+
setFieldsValue: (values: RecursivePartial<T>) => void
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const InternalFormModal = <T extends object>(props: FormModalProps<T>, ref: ForwardedRef<FormModalRef<T>>) => {
|
|
40
|
+
const {
|
|
41
|
+
width,
|
|
42
|
+
children,
|
|
43
|
+
title,
|
|
44
|
+
open,
|
|
45
|
+
footer,
|
|
46
|
+
layout,
|
|
47
|
+
labelCol,
|
|
48
|
+
bodyStyle,
|
|
49
|
+
initialValues,
|
|
50
|
+
maskClosable,
|
|
51
|
+
closeFn,
|
|
52
|
+
afterClose,
|
|
53
|
+
onConfirm,
|
|
54
|
+
} = props
|
|
55
|
+
const id = useId()
|
|
56
|
+
const [form] = Form.useForm<T>()
|
|
57
|
+
const formRef = useRef<FormInstance<T>>(null)
|
|
58
|
+
const isRenderProps = typeof children === 'function'
|
|
59
|
+
const [confirmLoading, setConfirmLoading] = useState(false)
|
|
60
|
+
|
|
61
|
+
useImperativeHandle(ref, () => {
|
|
62
|
+
return {
|
|
63
|
+
setFieldsValue(values) {
|
|
64
|
+
formRef.current?.setFieldsValue(values)
|
|
65
|
+
},
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
return (
|
|
70
|
+
<Modal
|
|
71
|
+
destroyOnClose
|
|
72
|
+
bodyStyle={bodyStyle}
|
|
73
|
+
style={{ textAlign: 'start' }}
|
|
74
|
+
width={width}
|
|
75
|
+
open={open}
|
|
76
|
+
title={title}
|
|
77
|
+
forceRender={true}
|
|
78
|
+
getContainer={false}
|
|
79
|
+
maskClosable={maskClosable}
|
|
80
|
+
footer={
|
|
81
|
+
typeof footer === 'object'
|
|
82
|
+
? footer
|
|
83
|
+
: [
|
|
84
|
+
<Button
|
|
85
|
+
key="cancel"
|
|
86
|
+
onClick={() => {
|
|
87
|
+
closeFn?.()
|
|
88
|
+
}}
|
|
89
|
+
>
|
|
90
|
+
取消
|
|
91
|
+
</Button>,
|
|
92
|
+
<Button form={id} key="submit" type="primary" htmlType="submit" loading={confirmLoading}>
|
|
93
|
+
确定
|
|
94
|
+
</Button>,
|
|
95
|
+
]
|
|
96
|
+
}
|
|
97
|
+
afterClose={() => {
|
|
98
|
+
afterClose?.()
|
|
99
|
+
form.resetFields()
|
|
100
|
+
}}
|
|
101
|
+
onCancel={closeFn}
|
|
102
|
+
>
|
|
103
|
+
<Form
|
|
104
|
+
form={form}
|
|
105
|
+
ref={formRef}
|
|
106
|
+
id={id}
|
|
107
|
+
autoComplete="off"
|
|
108
|
+
labelAlign="right"
|
|
109
|
+
labelWrap={true}
|
|
110
|
+
layout={layout}
|
|
111
|
+
initialValues={initialValues}
|
|
112
|
+
labelCol={
|
|
113
|
+
labelCol || {
|
|
114
|
+
flex: !layout || layout === 'horizontal' ? '120px' : '0',
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
onFinish={async values => {
|
|
118
|
+
try {
|
|
119
|
+
setConfirmLoading(true)
|
|
120
|
+
await onConfirm?.(values)
|
|
121
|
+
closeFn?.()
|
|
122
|
+
form.resetFields()
|
|
123
|
+
} finally {
|
|
124
|
+
setConfirmLoading(false)
|
|
125
|
+
}
|
|
126
|
+
}}
|
|
127
|
+
>
|
|
128
|
+
{isRenderProps ? children({ form, open, closeFn }) : children}
|
|
129
|
+
</Form>
|
|
130
|
+
</Modal>
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const FormModal = forwardRef(InternalFormModal) as <T extends object>(
|
|
135
|
+
props: FormModalProps<T> & { ref?: ForwardedRef<FormModalRef<T>> },
|
|
136
|
+
) => React.ReactElement
|
|
137
|
+
|
|
138
|
+
export default FormModal
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { PropsWithChildren, ReactNode } from 'react'
|
|
2
|
+
import { useEffect, useState } from 'react'
|
|
3
|
+
import { flushSync } from 'react-dom'
|
|
4
|
+
import { createRoot } from 'react-dom/client'
|
|
5
|
+
|
|
6
|
+
const splitByTags = (str: string) => {
|
|
7
|
+
const regex = /(<[^>]*>)/
|
|
8
|
+
return str.split(regex).filter(part => part !== '')
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function renderToString(node: ReactNode): Promise<string> {
|
|
12
|
+
const container = document.createElement('div')
|
|
13
|
+
const root = createRoot(container)
|
|
14
|
+
|
|
15
|
+
return new Promise(resolve => {
|
|
16
|
+
setTimeout(() => {
|
|
17
|
+
flushSync(() => {
|
|
18
|
+
root.render(node)
|
|
19
|
+
})
|
|
20
|
+
resolve(container.innerHTML)
|
|
21
|
+
})
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface HighlightTextsProps extends PropsWithChildren {
|
|
26
|
+
texts: Array<string | number>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const Highlight = (props: HighlightTextsProps) => {
|
|
30
|
+
const { texts, children } = props
|
|
31
|
+
const [htmlString, setHtmlString] = useState<string>('')
|
|
32
|
+
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
renderToString(children).then(str => {
|
|
35
|
+
const result = splitByTags(str)
|
|
36
|
+
|
|
37
|
+
for (const text of texts) {
|
|
38
|
+
for (let index = 0; index < result.length; index++) {
|
|
39
|
+
// TODO: 忽略 HTML tag
|
|
40
|
+
result[index] = result[index].replace(String(text), `<span style='color: #DC143C;'>${text}</span>`)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
setHtmlString(result.join(''))
|
|
45
|
+
})
|
|
46
|
+
}, [children, texts])
|
|
47
|
+
|
|
48
|
+
return <p dangerouslySetInnerHTML={{ __html: htmlString }}></p>
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export default Highlight
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { usePermission } from '@/hooks'
|
|
2
|
+
import type { ButtonProps } from 'antd'
|
|
3
|
+
import { Button, Tooltip } from 'antd'
|
|
4
|
+
import type { FC, PropsWithChildren } from 'react'
|
|
5
|
+
|
|
6
|
+
export interface PermissionButtonProps extends Omit<ButtonProps, 'disabled'> {
|
|
7
|
+
code: string
|
|
8
|
+
showLoading?: boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const PermissionButton: FC<PropsWithChildren<PermissionButtonProps>> = props => {
|
|
12
|
+
const { children, code, showLoading, ...restProps } = props
|
|
13
|
+
const { accessible, isValidating } = usePermission(code)
|
|
14
|
+
|
|
15
|
+
if (isValidating) {
|
|
16
|
+
return (
|
|
17
|
+
<Button loading={showLoading} disabled={!showLoading} {...restProps}>
|
|
18
|
+
{children}
|
|
19
|
+
</Button>
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (!accessible) {
|
|
24
|
+
return (
|
|
25
|
+
<Tooltip defaultOpen={false} title="无权限,请联系管理员进行授权">
|
|
26
|
+
<Button disabled {...restProps}>
|
|
27
|
+
{children}
|
|
28
|
+
</Button>
|
|
29
|
+
</Tooltip>
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return <Button {...restProps}>{children}</Button>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export default PermissionButton
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { useFetcher, usePermission } from '@/hooks'
|
|
2
|
+
import { useQueryTriggerStore } from '@/stores'
|
|
3
|
+
import type { ListResponse, PaginationParams } from '@/types'
|
|
4
|
+
import type { FormInstance, FormProps } from 'antd'
|
|
5
|
+
import { Form, Result, Spin, Table } from 'antd'
|
|
6
|
+
import type { TableProps } from 'antd/es/table'
|
|
7
|
+
import type { AxiosRequestConfig } from 'axios'
|
|
8
|
+
import type { ReactNode } from 'react'
|
|
9
|
+
import { useCallback, useEffect, useState } from 'react'
|
|
10
|
+
import useSWRMutation from 'swr/mutation'
|
|
11
|
+
import FilterForm from '../FilterForm'
|
|
12
|
+
|
|
13
|
+
export type QueryListKey = Omit<AxiosRequestConfig, 'data' | 'params'>
|
|
14
|
+
|
|
15
|
+
export interface QueryListProps<Item, Values>
|
|
16
|
+
extends Pick<TableProps<Item>, 'columns' | 'rowKey' | 'tableLayout' | 'expandable' | 'rowSelection' | 'bordered'>,
|
|
17
|
+
Pick<FormProps<Values>, 'initialValues' | 'labelCol'> {
|
|
18
|
+
// 由于表单的值和分页数据是封装在组件内部的,不便于在组件外部构造 swr key,所以组件内部的 useSWRMutation hook 使用的 key 是排除了 arg 字段的。
|
|
19
|
+
// 因此 swr 的缓存并未发挥作用,同一个 key 的所有页面共享缓存。
|
|
20
|
+
swrKey: QueryListKey
|
|
21
|
+
confirmText?: ReactNode
|
|
22
|
+
code?: string
|
|
23
|
+
renderForm?: (form: FormInstance<Values>) => ReactNode
|
|
24
|
+
// 把表单的值和分页数据转换成请求参数
|
|
25
|
+
transformArg?: (arg: Values & PaginationParams) => unknown
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const QueryList = <Item extends object, Values = NonNullable<unknown>>(props: QueryListProps<Item, Values>) => {
|
|
29
|
+
const { code, confirmText, labelCol, swrKey, renderForm, transformArg, initialValues, ...tableProps } = props
|
|
30
|
+
const { accessible, isValidating } = usePermission(code)
|
|
31
|
+
const [form] = Form.useForm<Values>()
|
|
32
|
+
const setTrigger = useQueryTriggerStore(state => state.setTrigger)
|
|
33
|
+
|
|
34
|
+
const [paginationData, setPaginationData] = useState<PaginationParams>({
|
|
35
|
+
page: 1,
|
|
36
|
+
perPage: 10,
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
const fetcher = useFetcher()
|
|
40
|
+
|
|
41
|
+
const { data, isMutating, trigger } = useSWRMutation(
|
|
42
|
+
swrKey,
|
|
43
|
+
async (
|
|
44
|
+
key,
|
|
45
|
+
{
|
|
46
|
+
arg,
|
|
47
|
+
}: {
|
|
48
|
+
arg?: Partial<PaginationParams>
|
|
49
|
+
},
|
|
50
|
+
) => {
|
|
51
|
+
const newPaginationData = {
|
|
52
|
+
page: arg?.page ?? paginationData.page ?? 1,
|
|
53
|
+
perPage: arg?.perPage ?? paginationData.perPage ?? 10,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
setPaginationData(newPaginationData)
|
|
57
|
+
|
|
58
|
+
const values = form.getFieldsValue()
|
|
59
|
+
|
|
60
|
+
const fetcherArg = {
|
|
61
|
+
...values,
|
|
62
|
+
...newPaginationData,
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return fetcher<ListResponse<Item>>({
|
|
66
|
+
...key,
|
|
67
|
+
// TODO: 兼容 params 与 data
|
|
68
|
+
params: typeof transformArg === 'function' ? transformArg(fetcherArg) : fetcherArg,
|
|
69
|
+
})
|
|
70
|
+
},
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
const onFinish = async () => {
|
|
74
|
+
await trigger({ page: 1 })
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const onReset = useCallback(async () => {
|
|
78
|
+
try {
|
|
79
|
+
form.resetFields()
|
|
80
|
+
await form.validateFields()
|
|
81
|
+
await trigger({ page: 1 })
|
|
82
|
+
} catch (_) {
|
|
83
|
+
console.log('表单校验失败')
|
|
84
|
+
}
|
|
85
|
+
}, [form, trigger])
|
|
86
|
+
|
|
87
|
+
const onPaginationChange = useCallback(
|
|
88
|
+
async (currentPage: number, currentSize: number) => {
|
|
89
|
+
await trigger({
|
|
90
|
+
page: currentPage,
|
|
91
|
+
perPage: currentSize,
|
|
92
|
+
})
|
|
93
|
+
},
|
|
94
|
+
[trigger],
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
useEffect(() => {
|
|
98
|
+
setTrigger(swrKey, trigger)
|
|
99
|
+
}, [swrKey, setTrigger, trigger])
|
|
100
|
+
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
;(async () => {
|
|
103
|
+
try {
|
|
104
|
+
await form.validateFields()
|
|
105
|
+
await trigger()
|
|
106
|
+
} catch (_) {
|
|
107
|
+
form.resetFields()
|
|
108
|
+
}
|
|
109
|
+
})()
|
|
110
|
+
}, [form, trigger])
|
|
111
|
+
|
|
112
|
+
if (isValidating) {
|
|
113
|
+
return (
|
|
114
|
+
<Spin
|
|
115
|
+
style={{
|
|
116
|
+
display: 'flex',
|
|
117
|
+
justifyContent: 'center',
|
|
118
|
+
alignItems: 'center',
|
|
119
|
+
height: 200,
|
|
120
|
+
}}
|
|
121
|
+
/>
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (!accessible) {
|
|
126
|
+
return <Result status={403} subTitle="无权限,请联系管理员进行授权" />
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return (
|
|
130
|
+
<>
|
|
131
|
+
<FilterForm<Values>
|
|
132
|
+
initialValues={initialValues}
|
|
133
|
+
form={form}
|
|
134
|
+
labelCol={labelCol}
|
|
135
|
+
confirmText={confirmText}
|
|
136
|
+
onFinish={onFinish}
|
|
137
|
+
onReset={onReset}
|
|
138
|
+
>
|
|
139
|
+
{renderForm?.(form)}
|
|
140
|
+
</FilterForm>
|
|
141
|
+
<Table
|
|
142
|
+
{...tableProps}
|
|
143
|
+
dataSource={data?.List}
|
|
144
|
+
loading={isMutating}
|
|
145
|
+
pagination={{
|
|
146
|
+
showSizeChanger: true,
|
|
147
|
+
showQuickJumper: true,
|
|
148
|
+
current: paginationData.page,
|
|
149
|
+
pageSize: paginationData.perPage,
|
|
150
|
+
total: data?.Total,
|
|
151
|
+
onChange: onPaginationChange,
|
|
152
|
+
}}
|
|
153
|
+
/>
|
|
154
|
+
</>
|
|
155
|
+
)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export default QueryList
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { DynamicTagsProps } from './DynamicTags'
|
|
2
|
+
import DynamicTags from './DynamicTags'
|
|
3
|
+
import type { FilterFormProps } from './FilterForm'
|
|
4
|
+
import FilterForm from './FilterForm'
|
|
5
|
+
import type { FormModalProps, FormModalRef } from './FormModal'
|
|
6
|
+
import FormModal from './FormModal'
|
|
7
|
+
import type { UseFormModalProps } from './FormModal/hooks'
|
|
8
|
+
import { useFormModal } from './FormModal/hooks'
|
|
9
|
+
import type { HighlightTextsProps } from './Highlight'
|
|
10
|
+
import Highlight from './Highlight'
|
|
11
|
+
import type { PermissionButtonProps } from './PermissionButton'
|
|
12
|
+
import PermissionButton from './PermissionButton'
|
|
13
|
+
import type { QueryListKey, QueryListProps } from './QueryList'
|
|
14
|
+
import QueryList from './QueryList'
|
|
15
|
+
|
|
16
|
+
export { FormModal, PermissionButton, DynamicTags, QueryList, FilterForm, Highlight, useFormModal }
|
|
17
|
+
export type {
|
|
18
|
+
DynamicTagsProps,
|
|
19
|
+
FilterFormProps,
|
|
20
|
+
UseFormModalProps,
|
|
21
|
+
FormModalProps,
|
|
22
|
+
FormModalRef,
|
|
23
|
+
QueryListProps,
|
|
24
|
+
QueryListKey,
|
|
25
|
+
HighlightTextsProps,
|
|
26
|
+
PermissionButtonProps,
|
|
27
|
+
}
|