Skip to content

Commit 0ac1bd0

Browse files
committed
feat(Form): Form组件
1 parent 09dc547 commit 0ac1bd0

File tree

11 files changed

+150
-13
lines changed

11 files changed

+150
-13
lines changed

example/examples/ios/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,4 +583,4 @@ SPEC CHECKSUMS:
583583

584584
PODFILE CHECKSUM: 36745b97236db17730b2d687b78fe071003a4a52
585585

586-
COCOAPODS: 1.11.3
586+
COCOAPODS: 1.11.2

example/examples/src/routes.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import {
2-
StackNavigationProp,
3-
StackNavigationOptions,
4-
} from '@react-navigation/stack';
1+
import {StackNavigationProp, StackNavigationOptions} from '@react-navigation/stack';
52

63
type ModalStackNavigation = StackNavigationProp<{}>;
74

@@ -178,8 +175,7 @@ export const stackPageData: Routes[] = [
178175
component: require('./routes/Result').default,
179176
params: {
180177
title: 'Result 结果页',
181-
description:
182-
'在整张页面中组织插画、图标、文字等内容,向用户反馈操作结果。',
178+
description: '在整张页面中组织插画、图标、文字等内容,向用户反馈操作结果。',
183179
},
184180
},
185181
{
@@ -339,8 +335,7 @@ export const stackPageData: Routes[] = [
339335
component: require('./routes/SpeedDial').default,
340336
params: {
341337
title: 'SpeedDial 悬浮标记',
342-
description:
343-
'SpeedDial 悬浮标记组件按下时,浮动动作按钮可以以快速显示标记的形式显示指定相关动作。',
338+
description: 'SpeedDial 悬浮标记组件按下时,浮动动作按钮可以以快速显示标记的形式显示指定相关动作。',
344339
},
345340
},
346341
{
@@ -498,4 +493,12 @@ export const stackPageData: Routes[] = [
498493
description: '图片查看',
499494
},
500495
},
496+
{
497+
name: 'Form',
498+
component: require('./routes/Form').default,
499+
params: {
500+
title: 'form',
501+
description: 'form',
502+
},
503+
},
501504
];
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
import {Form} from '@uiw/react-native';
3+
import {ComProps} from '../../routes';
4+
5+
export interface FormDemoProps extends ComProps {}
6+
7+
const FormDemo = () => {
8+
const items = [{type: 'input', label: 'name'}];
9+
return (
10+
<React.Fragment>
11+
<Form formDatas={items} />
12+
</React.Fragment>
13+
);
14+
};
15+
16+
export default FormDemo;

packages/core/src/Form/form.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React, { FC } from 'react';
2+
import FormItems from './formItems';
3+
import { Provider } from './store';
4+
import { FormProps } from './types';
5+
6+
const Form: FC<FormProps> = (props) => {
7+
const { formDatas } = props;
8+
9+
return (
10+
<Provider>
11+
<FormItems formDatas={formDatas} />
12+
</Provider>
13+
);
14+
};
15+
16+
export default Form;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React, { FC } from 'react';
2+
import { FormProps } from './types';
3+
import { useFormContext } from './store';
4+
import Input from '../Input';
5+
6+
const FormItems: FC<FormProps> = ({ formDatas = [] }) => {
7+
const {
8+
state: { formValues },
9+
dispatch,
10+
} = useFormContext();
11+
12+
const change = (label: string, value: any) => {
13+
dispatch({
14+
formValues: { ...formValues, [label]: value },
15+
});
16+
};
17+
18+
const _render = () => {
19+
return formDatas.map((v: any) => {
20+
if (v.type === 'input') {
21+
return <Input onChangeText={(value) => change(v.label, value)} />;
22+
}
23+
});
24+
};
25+
return <React.Fragment>{_render()}</React.Fragment>;
26+
};
27+
28+
export default FormItems;

packages/core/src/Form/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Form from './form';
2+
3+
export default Form;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React, { createContext, useContext, PropsWithChildren, FC, useReducer } from 'react';
2+
3+
export interface InitialState {
4+
formValues: any; // 表单数据
5+
}
6+
7+
export interface CreateContext {
8+
state: Partial<InitialState>;
9+
dispatch: React.Dispatch<InitialState>;
10+
}
11+
12+
export const initialState = {
13+
formValues: {},
14+
};
15+
16+
const Context = createContext<CreateContext>({
17+
state: initialState,
18+
dispatch: () => null,
19+
});
20+
21+
const reducer = (state: Partial<InitialState>, action: Partial<InitialState>) => {
22+
return {
23+
...state,
24+
...action,
25+
};
26+
};
27+
28+
const Provider: FC<PropsWithChildren> = ({ children }) => {
29+
const [state, dispatch] = useReducer(reducer, initialState);
30+
return <Context.Provider value={{ state, dispatch }}>{children}</Context.Provider>;
31+
};
32+
33+
function useFormContext() {
34+
const { state, dispatch } = useContext(Context);
35+
return { ...state, state, dispatch };
36+
}
37+
38+
export { Context, reducer, Provider, useFormContext };
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
interface FormProps {
2+
formDatas?: FormItemsProps[];
3+
}
4+
5+
interface FormItemsProps {
6+
label: string;
7+
type: string;
8+
}
9+
10+
export type { FormProps, FormItemsProps };

packages/core/src/Form/utils/is.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const opt = Object.prototype.toString;
2+
3+
export function isArray(obj: any): obj is any[] {
4+
return opt.call(obj) === '[object Array]';
5+
}
6+
7+
export function isObject(obj: any): obj is { [key: string]: any } {
8+
return opt.call(obj) === '[object Object]';
9+
}
10+
11+
export function isString(obj: any): obj is string {
12+
return opt.call(obj) === '[object String]';
13+
}
14+
15+
export function isNumber(obj: any): obj is number {
16+
return opt.call(obj) === '[object Number]' && obj === obj; // eslint-disable-line
17+
}
18+
19+
export function isRegExp(obj: any) {
20+
return opt.call(obj) === '[object RegExp]';
21+
}

packages/core/src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ export { default as ActionBar } from './ActionBar';
118118
export * from './ActionBar';
119119
export { default as ImageViewer } from './ImageViewer';
120120
export * from './ImageViewer';
121+
export { default as Form } from './Form';
122+
export * from './Form';
121123
/**
122124
* Typography
123125
*/

0 commit comments

Comments
 (0)