Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
jinfa-platform
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
黄庭坚
jinfa-platform
Commits
0a5d4c23
Commit
0a5d4c23
authored
May 25, 2021
by
XieZhiXiong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 添加 数组操作 hook
parent
5369269d
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
117 additions
and
0 deletions
+117
-0
useSpliceArray.ts
src/hooks/useSpliceArray.ts
+117
-0
No files found.
src/hooks/useSpliceArray.ts
0 → 100644
View file @
0a5d4c23
/*
* @Author: XieZhiXiong
* @Date: 2021-05-24 14:43:29
* @LastEditors: XieZhiXiong
* @LastEditTime: 2021-05-24 16:40:13
* @Description: 对数组的常用操作,eg. 增、删、改,并返回操作后的数组
*/
import
{
useState
,
useReducer
}
from
'react'
;
export
type
HandleType
<
P
>
=
{
/**
* 删除元素
*/
delete
:
(
index
:
number
,
len
:
number
,
item
?:
P
)
=>
void
,
/**
* 替换元素
*/
replace
:
(
index
:
number
,
item
:
P
)
=>
void
,
/**
* 插入元素
*/
insert
:
(
index
:
number
,
item
:
P
)
=>
void
,
/**
* 自定义处理数组方法
*/
custom
:
(
fn
:
(
oldArr
:
P
[])
=>
P
[])
=>
void
,
}
function
reducer
(
state
,
action
)
{
const
{
type
,
index
,
len
,
item
,
fn
,
}
=
action
;
switch
(
type
)
{
case
'delete'
:
{
const
newArr
=
[...
state
];
if
(
item
)
{
newArr
.
splice
(
index
,
len
,
item
);
}
else
{
newArr
.
splice
(
index
,
len
);
}
return
newArr
;
}
case
'replace'
:
{
const
newArr
=
[...
state
];
newArr
.
splice
(
index
,
1
,
item
);
return
newArr
;
}
case
'insert'
:
{
const
newArr
=
[...
state
];
newArr
.
splice
(
index
,
0
,
item
);
return
newArr
;
}
case
'custom'
:
{
let
newArr
=
[]
if
(
fn
)
{
newArr
=
fn
([...
state
]);
}
return
newArr
;
}
default
:
throw
new
Error
();
}
}
const
useSpliceArray
=
<
T
>
(
defaultArr
:
T
[]):
[
Arr
:
T
[],
handle
:
HandleType
<
T
>
]
=>
{
const
[
arr
,
setArr
]
=
useState
(
defaultArr
||
[]);
const
[
state
,
dispatch
]
=
useReducer
(
reducer
,
defaultArr
||
[]);
const
handleDelete
=
(
index
:
number
,
len
:
number
,
item
?:
T
)
=>
{
dispatch
({
type
:
'delete'
,
index
,
len
,
item
,
});
};
const
handleReplace
=
(
index
:
number
,
item
:
T
)
=>
{
dispatch
({
type
:
'replace'
,
index
,
item
,
});
};
const
handleInsert
=
(
index
:
number
,
item
:
T
)
=>
{
dispatch
({
type
:
'insert'
,
index
,
item
,
});
};
const
handleCustom
=
(
fn
:
(
oldArr
:
T
[])
=>
T
[])
=>
{
dispatch
({
type
:
'custom'
,
fn
,
});
};
return
[
state
,
{
delete
:
handleDelete
,
replace
:
handleReplace
,
insert
:
handleInsert
,
custom
:
handleCustom
,
},
];
}
export
default
useSpliceArray
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment