Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(release): v1.3.5 #43

Merged
merged 4 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions .dumi/metadata/apis/docs_apiDemos_Form.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
},
"tags": {
"description": "Error prompt class name.",
"localKey": "API.form.global.props.form.share.errorClass"
"localKey": "API.form.global.props.form.share.errorClass",
"version": "1.3.4"
}
},
"labelPosition": {
Expand Down Expand Up @@ -66,7 +67,8 @@
},
"tags": {
"description": "Label class name",
"localKey": "API.form.global.props.form.share.labelClass"
"localKey": "API.form.global.props.form.share.labelClass",
"version": "1.3.4"
}
},
"formItemStyle": {
Expand All @@ -88,7 +90,8 @@
},
"tags": {
"description": "Form item class",
"localKey": "API.form.global.props.form.share.formItemClass"
"localKey": "API.form.global.props.form.share.formItemClass",
"version": "1.3.4"
}
},
"trigger": {
Expand All @@ -115,6 +118,18 @@
"description": "Form item content style, supports object nesting writing method"
}
},
"contentClassName": {
"defaultValue": null,
"name": "contentClassName",
"type": {
"name": "string"
},
"tags": {
"description": "Content area style class name",
"localKey": "API.form.global.props.form.share.contentClass",
"version": "1.3.4"
}
},
"fullWidth": {
"defaultValue": {
"value": "false"
Expand Down Expand Up @@ -197,7 +212,8 @@
},
"tags": {
"description": "Form container class name.",
"localKey": "API.form.global.props.form.formClass"
"localKey": "API.form.global.props.form.formClass",
"version": "1.3.4"
}
},
"direction": {
Expand Down
21 changes: 18 additions & 3 deletions .dumi/metadata/apis/docs_apiDemos_FormItem.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@
},
"tags": {
"description": "Error prompt class name.",
"localKey": "API.form.global.props.form.share.errorClass"
"localKey": "API.form.global.props.form.share.errorClass",
"version": "1.3.4"
}
},
"labelPosition": {
Expand Down Expand Up @@ -172,7 +173,8 @@
},
"tags": {
"description": "Label class name",
"localKey": "API.form.global.props.form.share.labelClass"
"localKey": "API.form.global.props.form.share.labelClass",
"version": "1.3.4"
}
},
"formItemStyle": {
Expand All @@ -194,7 +196,8 @@
},
"tags": {
"description": "Form item class",
"localKey": "API.form.global.props.form.share.formItemClass"
"localKey": "API.form.global.props.form.share.formItemClass",
"version": "1.3.4"
}
},
"trigger": {
Expand All @@ -221,6 +224,18 @@
"description": "Form item content style, supports object nesting writing method"
}
},
"contentClassName": {
"defaultValue": null,
"name": "contentClassName",
"type": {
"name": "string"
},
"tags": {
"description": "Content area style class name",
"localKey": "API.form.global.props.form.share.contentClass",
"version": "1.3.4"
}
},
"fullWidth": {
"defaultValue": {
"value": "false"
Expand Down
21 changes: 18 additions & 3 deletions .dumi/metadata/apis/docs_apiDemos_useForm.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
},
"tags": {
"description": "Error prompt class name.",
"localKey": "API.form.global.props.form.share.errorClass"
"localKey": "API.form.global.props.form.share.errorClass",
"version": "1.3.4"
}
},
"labelPosition": {
Expand Down Expand Up @@ -90,7 +91,8 @@
},
"tags": {
"description": "Label class name",
"localKey": "API.form.global.props.form.share.labelClass"
"localKey": "API.form.global.props.form.share.labelClass",
"version": "1.3.4"
}
},
"formItemStyle": {
Expand All @@ -112,7 +114,8 @@
},
"tags": {
"description": "Form item class",
"localKey": "API.form.global.props.form.share.formItemClass"
"localKey": "API.form.global.props.form.share.formItemClass",
"version": "1.3.4"
}
},
"trigger": {
Expand All @@ -139,6 +142,18 @@
"description": "Form item content style, supports object nesting writing method"
}
},
"contentClassName": {
"defaultValue": null,
"name": "contentClassName",
"type": {
"name": "string"
},
"tags": {
"description": "Content area style class name",
"localKey": "API.form.global.props.form.share.contentClass",
"version": "1.3.4"
}
},
"fullWidth": {
"defaultValue": {
"value": "false"
Expand Down
2 changes: 1 addition & 1 deletion .dumi/metadata/apis/docs_apiDemos_useFormReturnType.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"defaultValue": null,
"name": "setState",
"type": {
"name": "() => Dispatch<unknown>"
"name": "() => void"
},
"tags": {
"localKey": "API.useForm.setState.desc",
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## [1.3.5](https://github.com/easy-form/react-form-simple/compare/v1.3.4...v1.3.5) (2023-12-26)

### Bug Fixes

- fix the bug that prompts setState() method type incompatibility when using type UseFormReturnType ([2386dc0](https://github.com/easy-form/react-form-simple/commit/2386dc01ef693b78c3f359836419e1d69a3a1422))
- Fixed model type prompt error bug when using useWatch ([2801509](https://github.com/easy-form/react-form-simple/commit/2801509bd1c8b68e3d188cd1240e65d1828671fd))

### Features

- Optimize the release of memory resources after destruction when using useForm ([360f00b](https://github.com/easy-form/react-form-simple/commit/360f00b6ec1cc13872fab65a9ad3ff7edafcfa3b))

## [1.3.4](https://github.com/easy-form/react-form-simple/compare/v1.3.3...v1.3.4) (2023-12-25)

### Bug Fixes
Expand Down
7 changes: 5 additions & 2 deletions docs/demos/_controller.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import Button from '@components/Button';
import React, { useEffect } from 'react';
import React, { useEffect, useState } from 'react';
import { useForm } from 'react-form-simple';

export default function App() {
const { render, model } = useForm({ name: 'name' });

const [modelState, setModelState] = useState('');

const renderName = render('name')(<input className="input" />);

useEffect(() => {
Expand All @@ -13,11 +15,12 @@ export default function App() {
}, 2000);
}, []);

const onSubmit = () => void console.log(model);
const onSubmit = () => setModelState(JSON.stringify(model));

return (
<>
{renderName}
{modelState}
<Button onClick={onSubmit}>submit</Button>
</>
);
Expand Down
7 changes: 5 additions & 2 deletions docs/demos/_example.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import Button from '@components/Button';
import React from 'react';
import React, { useState } from 'react';
import { useForm } from 'react-form-simple';

export default function App() {
const { render, model } = useForm({
name: '',
age: '',
});
const [modelState, setModelState] = useState('');

const renderName = render('name')(<input className="input" />);

const renderAge = render('age')(<input className="input" />);

const renderSubmit = (
<Button onClick={() => void console.log(model)}>submit</Button>
<Button onClick={() => setModelState(JSON.stringify(model))}>submit</Button>
);
return (
<>
{renderName}
{renderAge}
{modelState}
{renderSubmit}
</>
);
Expand Down
2 changes: 1 addition & 1 deletion docs/demos/_watch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function App() {
const renderAge = render('age')(<input className="input" />);

useWatch(
({ model }) => [model?.name, model?.age],
({ model }) => [model.name, model.age],
(value, preValue) => {
console.log({ value, preValue });
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-form-simple",
"version": "1.3.4",
"version": "1.3.5",
"description": "A form library for quickly controlling react form input",
"keywords": [
"react",
Expand Down
5 changes: 4 additions & 1 deletion src/types/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,10 @@ export namespace GlobalProps {
contentStyle?: CSSInterpolation;
/**
* @description Content area style class name
* @localKey API.form.global.props.form.share.contentClass
* @version 1.3.4
*/
contentClassName?: string
contentClassName?: string;
/**
* @localKey API.form.global.props.form.share.fullWidth
* @description Whether the width of the form item fills the entire row
Expand Down Expand Up @@ -374,6 +376,7 @@ export namespace GlobalProps {
/**
* @description Form container class name.
* @localKey API.form.global.props.form.formClass
* @version 1.3.4
*/
formClassName?: string;
/**
Expand Down
2 changes: 1 addition & 1 deletion src/types/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export namespace UseFormNamespace {
* @description Manually re-render the view. If you need to re-render the view externally, you can call setState to re-render the current component tree.
* @resetType Function
*/
setState: () => React.Dispatch<React.SetStateAction<unknown>>;
setState: () => void;
} & Apis.FormApis &
Pick<GlobalProps.FormItemProps, 'contextProps'>;
}
Expand Down
15 changes: 12 additions & 3 deletions src/use/useController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ export function useController<T extends Record<string, any>>(obj: T): T {
const [, setState] = useState({});
const proxyStateRef = useRef(obj);

return observer(proxyStateRef.current, () => {
setState({});
});
const { proxyMap, rawMap } = useRef({
proxyMap: new WeakMap(),
rawMap: new WeakMap(),
}).current;

return observer(
proxyStateRef.current,
() => {
setState({});
},
{ proxyMap, rawMap },
);
}

export default useController;
9 changes: 8 additions & 1 deletion src/use/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { usePrivateSubscribe } from './useSubscribe';
import { usePrivateWatch } from './useWatch';

const useForm = <T extends Record<string, any>>(
model?: T,
model: T,
config?: UseFormNamespace.ShareConfig,
) => {
const proxyTarget = useRef(model || {});
Expand All @@ -27,6 +27,11 @@ const useForm = <T extends Record<string, any>>(
[],
);

const createObserverMap = useRef({
proxyMap: new WeakMap(),
rawMap: new WeakMap(),
}).current;

const proxymodel = createObserverForm(
proxyTarget.current as T,
({ path, value }) => {
Expand All @@ -36,6 +41,7 @@ const useForm = <T extends Record<string, any>>(
},
{
path: [],
...createObserverMap,
},
);

Expand Down Expand Up @@ -68,6 +74,7 @@ const useForm = <T extends Record<string, any>>(
useWatch,
setState: useFormExtraApis.setState,
...overlayApis,
...createObserverMap,
};
};

Expand Down
14 changes: 10 additions & 4 deletions src/utils/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ import { isObject, isObjectOrArray } from './util';
export const proxyMap = new WeakMap();
export const rawMap = new WeakMap();

const _proxyMap = proxyMap;
const _rawMap = rawMap;

const proxyPolyfill = ProxyPolyfillBuilder();
const Proxys = window.Proxy || proxyPolyfill;

export type ObserverOptions = { path?: string[] };
export type ObserverOptions = {
path?: string[];
proxyMap?: WeakMap<object, any>;
rawMap?: WeakMap<object, any>;
};

export type ObserverCb = { path: string; value: any };

export const toTarget = (proxy: any) => rawMap.get(proxy);
export const toTarget = (proxy: any) => cloneDeep(proxy);

export const replaceTarget = (
proxy: any,
Expand Down Expand Up @@ -80,6 +87,7 @@ export const observer = <T extends object>(
cb?: (args: ObserverCb) => void,
options?: ObserverOptions,
): T => {
const { path = [], rawMap = _rawMap, proxyMap = _proxyMap } = options || {};
const existingProxy = proxyMap.get(initialVal);
if (existingProxy) {
return existingProxy;
Expand All @@ -89,8 +97,6 @@ export const observer = <T extends object>(
return initialVal;
}

const { path = [] } = options || {};

const proxy = new Proxys(initialVal, {
get(target, key, receiver) {
const ret = Reflect.get(target, key, receiver);
Expand Down
Loading