Skip to content

Commit 4ee89be

Browse files
committed
fix: fix input index issue.
1 parent f27c836 commit 4ee89be

File tree

6 files changed

+34
-14
lines changed

6 files changed

+34
-14
lines changed

core/src/Input.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@ export interface InputProps extends React.InputHTMLAttributes<HTMLInputElement>
1313

1414
export const Input: FC<PropsWithChildren<InputProps>> = memo((props) => {
1515
const ref = useRef<InputProps>();
16-
const { fields = {}, extra = {}, dispatch } = useStore();
16+
const { fields = {}, extra = {}, $$index = {}, dispatch } = useStore();
1717
const { name, rename, visible = true, children, ...elmProps } = props;
1818
useEffect(() => {
1919
if (ref.current !== props && name) {
2020
ref.current = { ...props };
2121
dispatch({
22+
$$index: { ...$$index, [name]: elmProps.index || 0 },
2223
extra: {
2324
...extra,
2425
[name]: children || null,
2526
},
2627
fields: {
2728
...fields,
28-
[name]: visible ? <input {...elmProps} name={rename || name} /> : null
29+
[name]: visible ? <input {...elmProps} name={rename || name} /> : null
2930
},
3031
});
3132
}

core/src/store.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ export interface StoreContextValue<Tag extends BlockTagType> extends InitialStat
66

77
export type BlockTagType = keyof JSX.IntrinsicElements;
88

9+
export type Index = Record<string, number>;
910
export type Fields = React.ReactElement<HTMLInputElement & { index?: number; }>;
1011
export type Buttons = React.ReactElement<HTMLButtonElement & { index?: number; }>;
1112
export type Blocks<Tag extends BlockTagType = 'div'> = React.ReactElement<Tag & { index?: number; }>;
1213

1314
export interface RenderStateProps<T extends BlockTagType = 'div'> {
15+
$$index?: Record<string, number>;
1416
fields?: Record<string, Fields | null>;
1517
buttons?: Record<string, Buttons | null>;
1618
blocks?: Record<string, Blocks<T> | null>;
@@ -33,6 +35,7 @@ export interface InitialState<Tag extends BlockTagType = 'div'> extends RenderSt
3335
}
3436

3537
export const initialState: InitialState = {
38+
index: {},
3639
fields: {},
3740
buttons: {},
3841
blocks: {},
@@ -51,24 +54,25 @@ export function reducer(state: InitialState, action: Partial<RenderStateProps>):
5154
const result = {
5255
...state,
5356
...action,
57+
$$index: { ...state.$$index, ...action.$$index },
5458
fields: { ...state.fields, ...action.fields },
5559
buttons: { ...state.buttons, ...action.buttons },
5660
blocks: { ...state.blocks, ...action.blocks },
5761
extra: { ...state.extra, ...action.extra },
5862
}
59-
const fieldsArray = Object.keys(result.fields).map((key, index) => ({
63+
const fieldsArray = Object.keys(result.fields).map((key) => ({
6064
name: key,
61-
index: result.fields[key]?.props?.index || 0,
65+
index: result.fields[key]?.props?.index || (result.$$index || {})[key] || 0,
6266
children: result.fields[key],
6367
}));
64-
const buttonsArray = Object.keys(result.buttons).map((key, index) => ({
68+
const buttonsArray = Object.keys(result.buttons).map((key) => ({
6569
name: key,
66-
index: result.buttons[key]?.props?.index || 0,
70+
index: result.buttons[key]?.props?.index || (result.$$index || {})[key] || 0,
6771
children: result.buttons[key],
6872
}));
69-
const blocksArray = Object.keys(result.blocks).map((key, index) => ({
73+
const blocksArray = Object.keys(result.blocks).map((key) => ({
7074
name: key,
71-
index: result.blocks[key]?.props?.index || 0,
75+
index: result.blocks[key]?.props?.index || (result.$$index || {})[key] || 0,
7276
children: result.blocks[key],
7377
}));
7478
return { ...result, data: { ...result.data, fields: fieldsArray, buttons: buttonsArray, blocks: blocksArray }};

pages/base/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,13 @@ const Demo = () => (
9393
<Logo>
9494
<LoginLogo />
9595
</Logo>
96+
<Username rename="subtitle" visible={false} index={0}>
97+
欢迎登录页面
98+
</Username>
9699
<Username index={3} />
100+
<Username rename="username_rule" visible={false} index={4}>
101+
用户名规则
102+
</Username>
97103
<Password index={1} />
98104
<Input name="phone" index={2} placeholder="Phone number">
99105
<img src={imgSrc} height={38} />

pages/base/src/control/Password.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { FC } from 'react';
22
import { Input, InputProps } from 'react-login-page';
33

44
export const Password: FC<InputProps> = (props) => {
5-
const { name, ...elmProps } = props;
6-
return <Input type="password" placeholder="Password" {...elmProps} name="password" rename={name} />;
5+
const { name, rename, ...elmProps } = props;
6+
const nameProps = { name: 'email', rename: name };
7+
if (rename) {
8+
nameProps.name = rename;
9+
}
10+
return <Input type="password" placeholder="Password" {...elmProps} {...nameProps} />;
711
};

pages/base/src/control/Username.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { FC } from 'react';
22
import { Input, InputProps } from 'react-login-page';
33

44
export const Username: FC<InputProps> = (props) => {
5-
const { name, ...elmProps } = props;
6-
return <Input placeholder="Username" spellCheck={false} {...elmProps} name="username" rename={name} />;
5+
const { name, rename, ...elmProps } = props;
6+
const nameProps = { name: 'username', rename: name };
7+
if (rename) {
8+
nameProps.name = rename;
9+
}
10+
return <Input placeholder="Username" spellCheck={false} {...elmProps} {...nameProps} />;
711
};

pages/base/src/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ const RenderLogin = () => {
2929
{blocks.logo} {blocks.title}
3030
</header>
3131
{fields.sort((a, b) => a.index - b.index).map((item, idx) => {
32-
if (!item.children) return null;
33-
return <label key={item.name + idx}>{item.children}{extra[item.name as keyof typeof extra]}</label>
32+
const extraLabel = extra[item.name as keyof typeof extra];
33+
if (!item.children && extraLabel) return <div key={idx}>{extraLabel}</div>;
34+
return <label key={item.name + idx}>{item.children}{extraLabel}</label>
3435
})}
3536
<section>
3637
{buttons.sort((a, b) => a.index - b.index).map((item, idx) => {

0 commit comments

Comments
 (0)