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
shenshaokai
jinfa-platform
Commits
c9f5d648
Commit
c9f5d648
authored
Apr 11, 2022
by
rainbowmorel@163.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
地址选择器 新增输入功能正则表达式
parent
f5e92d17
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
76 additions
and
4 deletions
+76
-4
AddressDrawer.tsx
src/components/AddressDrawer/AddressDrawer.tsx
+51
-4
throttleFn.ts
src/utils/throttleFn.ts
+25
-0
No files found.
src/components/AddressDrawer/AddressDrawer.tsx
View file @
c9f5d648
import
{
debounceFn
,
throttleFn
}
from
'@/utils/throttleFn'
;
import
{
useDebounce
}
from
'@umijs/hooks'
;
import
{
Button
,
Col
,
Drawer
,
Form
,
FormInstance
,
Input
,
Radio
,
Row
,
Select
,
Space
}
from
'antd'
;
import
React
,
{
useContext
,
useEffect
,
useState
}
from
'react'
;
import
AddressForm
from
'./AddressForm'
;
...
...
@@ -46,14 +48,42 @@ function AddressDrawer(props: AddressDrawerProps) {
const
[
addrForm
]
=
Form
.
useForm
(
formInstance
);
const
[
addrList
,
setAddrList
]
=
useState
([]);
const
handleInputChangeHooks
=
debounceFn
((
str
)
=>
{
const
val
:
string
=
str
const
reg
=
/.+
?(
省|市|自治区|自治州|镇|县|区
)
/g
;
const
maths
=
val
.
match
(
reg
);
if
(
maths
&&
maths
.
length
>=
3
)
{
//至少 拥有省市区 才进行处理
const
slice
:
string
=
maths
[
maths
.
length
-
1
];
const
addressOther
:
string
=
val
.
slice
(
val
.
indexOf
(
slice
)
+
slice
.
length
,
val
.
length
)
const
otherSplit
=
addressOther
.
split
(
" "
);
let
propValue
=
{
provinceName
:
tryGetMatchValue
(
maths
,
0
),
cityName
:
tryGetMatchValue
(
maths
,
1
),
districtName
:
tryGetMatchValue
(
maths
,
2
),
streetName
:
tryGetMatchValue
(
maths
,
3
),
address
:
tryGetMatchValue
(
otherSplit
,
0
),
receiverName
:
tryGetMatchValue
(
otherSplit
,
1
),
phone
:
tryGetMatchValue
(
otherSplit
,
2
),
fullAddress
:
tryGetMatchValue
(
maths
,
0
)
+
tryGetMatchValue
(
maths
,
1
)
+
tryGetMatchValue
(
maths
,
2
)
+
tryGetMatchValue
(
maths
,
3
)
+
tryGetMatchValue
(
otherSplit
,
0
)
}
onChange
(
propValue
)
}
},
1000
);
useEffect
(()
=>
{
renderAddressList
().
then
(
data
=>
{
if
(
addr
)
{
// props 更新 内部状态更新 用于FormItem 的操作
setValue
(
addr
)
let
targetValue
=
`
${
addr
?.
fullAddress
??
''
}
$
{
addr
?.
shipperName
?
addr
?.
shipperName
:
addr
?.
receiverName
??
''
}
$
{
addr
?.
phone
??
''
}
`
setValue(targetValue)
} else if (showDefault) {
// 如果没有默认值,且设置了 showDefault
const
target
=
data
.
find
(
v
=>
v
.
isDefault
===
1
)
const target = data.find(v
=> v.isDefault ===
1)
setValue(target)
onChange(target)
}
...
...
@@ -96,7 +126,7 @@ function AddressDrawer(props: AddressDrawerProps) {
form={addrForm} />
}
function
renderAddressFormBtnGroup
()
{
const renderAddressFormBtnGroup = () =>
{
if (showForm) {
return <Button type='primary' onClick={sumbitAddressForm}>提交</Button>
} else {
...
...
@@ -104,11 +134,28 @@ function AddressDrawer(props: AddressDrawerProps) {
}
}
const handleInputChange = (e) => {
const targetValue = e.target.value;
setValue(targetValue)
handleInputChangeHooks(targetValue)
}
const tryGetMatchValue = (maths, i) => {
try {
return maths[i] || ''
} catch {
return ""
}
}
return (
<AddressRaioContextProvider value={addrList}>
<Row gutter={10}>
<Col span={20}>
<
Input
.
TextArea
rows=
{
rows
}
disabled=
{
true
}
value=
{
`${value?.fullAddress ?? ''} ${value?.shipperName ? value?.shipperName : value?.receiverName ?? ''} ${value?.phone ?? ''}`
}
/>
<Input.TextArea rows={rows} disabled={disabled}
value={value}
onChange={handleInputChange}
/>
</Col>
<Col span={4}>
...
...
src/utils/throttleFn.ts
0 → 100644
View file @
c9f5d648
export
const
throttleFn
=
(
fn
,
delay
)
=>
{
let
valid
=
true
return
function
(...
args
)
{
if
(
!
valid
)
{
return
false
;
}
valid
=
false
;
setTimeout
(()
=>
{
fn
(...
args
)
valid
=
true
},
delay
);
}
}
let
timer
=
null
;
export
const
debounceFn
=
(
func
,
delay
)
=>
{
return
function
(...
args
)
{
if
(
timer
)
{
clearTimeout
(
timer
)
}
timer
=
setTimeout
(()
=>
{
func
(...
args
)
},
delay
);
}
}
\ No newline at end of file
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