Skip to content

Commit c9c2304

Browse files
committed
feat: use normal header field in popup
1 parent 76c6ca5 commit c9c2304

File tree

2 files changed

+27
-82
lines changed

2 files changed

+27
-82
lines changed

src/pages/popup/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import isDarkMode from '@/share/pages/is-dark-mode';
1313
import Rules from './rules';
1414

1515
const basicStyle = css`
16-
min-width: 340px;
17-
min-height: 440px;
16+
min-width: 380px;
17+
min-height: 460px;
1818
height: 100vh;
1919
width: 100vw;
2020
justify-content: stretch;

src/pages/popup/rules/quick-edit.tsx

Lines changed: 25 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { IconEdit, IconItalic, IconPlusCircle } from '@douyinfe/semi-icons';
2-
import { Button, Form, Input, Tabs } from '@douyinfe/semi-ui';
1+
import { IconEdit } from '@douyinfe/semi-icons';
2+
import { Button, Form, Input } from '@douyinfe/semi-ui';
33
import { css } from '@emotion/css';
44
import { useLatest } from 'ahooks';
5-
import { type ReactNode, useCallback, useEffect, useState } from 'react';
5+
import { type ReactNode, useCallback } from 'react';
6+
import HeaderField from '@/share/components/header-field';
67
import Modal from '@/share/components/modal';
78
import { RULE_TYPE } from '@/share/core/constant';
89
import type { Rule } from '@/share/core/types';
@@ -11,77 +12,6 @@ import usePref from '@/share/hooks/use-pref';
1112
import Api from '@/share/pages/api';
1213
import { Toast } from '@/share/pages/toast';
1314

14-
interface HeaderQuickEditProps {
15-
defaultValue: Record<string, string>;
16-
onChange: (v: Record<string, string>) => void;
17-
}
18-
19-
const HeaderQuickEdit = ({ defaultValue, onChange }: HeaderQuickEditProps) => {
20-
const [innerValue, setInnerValue] = useState<Array<[string, string]>>([]);
21-
22-
useEffect(() => {
23-
setInnerValue(Object.entries(defaultValue));
24-
}, []);
25-
26-
const handleChange = (processValue: (v: Array<[string, string]>) => void) => {
27-
setInnerValue(prev => {
28-
const res = [...prev];
29-
processValue(res);
30-
onChange(Object.fromEntries(res.filter(x => Boolean(x[0]))));
31-
return res;
32-
});
33-
};
34-
35-
const handleItemChange = (index: number, keyIndex: number, value: string) => {
36-
handleChange(res => {
37-
const newItem: [string, string] = [...res[index]];
38-
newItem[keyIndex] = value;
39-
res[index] = newItem;
40-
});
41-
};
42-
43-
return (
44-
<Tabs
45-
defaultActiveKey="0"
46-
collapsible
47-
onTabClose={key => handleChange(res => res.splice(Number(key), 1))}
48-
tabBarExtraContent={
49-
<Button
50-
onClick={() => handleChange(res => res.push(['', '']))}
51-
icon={<IconPlusCircle />}
52-
/>
53-
}
54-
size="small"
55-
type="card"
56-
keepDOM
57-
>
58-
{innerValue.map(([name, value], index) => {
59-
return (
60-
<Tabs.TabPane
61-
key={index}
62-
closable
63-
itemKey={String(index)}
64-
tab={name || <IconItalic />}
65-
>
66-
<Form.Slot label={t('headerName')}>
67-
<Input
68-
value={name}
69-
onChange={v => handleItemChange(index, 0, v)}
70-
/>
71-
</Form.Slot>
72-
<Form.Slot label={t('headerValue')}>
73-
<Input
74-
value={value}
75-
onChange={v => handleItemChange(index, 1, v)}
76-
/>
77-
</Form.Slot>
78-
</Tabs.TabPane>
79-
);
80-
})}
81-
</Tabs>
82-
);
83-
};
84-
8515
interface QuickEditProps {
8616
rule: Rule;
8717
}
@@ -93,7 +23,9 @@ const modalCls = css`
9323
bottom: 0;
9424
width: 100%;
9525
.semi-modal-body {
96-
overflow: hidden;
26+
max-height: calc(90vh - 90px);
27+
overflow: auto;
28+
scrollbar-width: thin;
9729
}
9830
> .semi-modal-content {
9931
border-bottom-left-radius: 0;
@@ -132,12 +64,25 @@ const QuickEdit = ({ rule }: QuickEditProps) => {
13264
newRule.ruleType,
13365
)
13466
) {
135-
const defaultValue = newRule.headers || {};
67+
const defaultValue = Object.entries(newRule.headers || {}).map(
68+
([name, value]) => ({
69+
name,
70+
value,
71+
}),
72+
);
13673
content = (
137-
<HeaderQuickEdit
138-
defaultValue={defaultValue}
139-
onChange={v => (newRule.headers = v)}
140-
/>
74+
<Form
75+
initValues={{ header: defaultValue }}
76+
onValueChange={({ header }) =>
77+
(newRule.headers = Object.fromEntries(
78+
header
79+
.filter((x: any) => Boolean(x.name))
80+
.map((x: any) => [x.name, x.value]),
81+
))
82+
}
83+
>
84+
<HeaderField field="header" type={undefined} />
85+
</Form>
14186
);
14287
}
14388

0 commit comments

Comments
 (0)