Skip to content

Commit

Permalink
Add memo
Browse files Browse the repository at this point in the history
  • Loading branch information
deadrime committed May 25, 2024
1 parent 9854e6e commit 3ec54ba
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 33 deletions.
30 changes: 7 additions & 23 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
node_modules
storybook-static
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
.vscode
.bashrc
.zshenv
.npmrc
public/themes/*.css
bundle.html
4 changes: 2 additions & 2 deletions src/Form.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, CSSProperties, useMemo, useEffect } from 'react';
import React, { useCallback, CSSProperties, useMemo, useEffect, memo } from 'react';

import { FormContext } from './FormContext';
import { FormApi } from './FormApi';
Expand Down Expand Up @@ -94,4 +94,4 @@ export const Form = <
);
}

export default Form;
export default memo(Form) as typeof Form;
8 changes: 5 additions & 3 deletions src/FormArrayItem.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useCallback } from "react"
import { FormItem, FormItemProps } from "./FormItem"
import React, { memo, useCallback } from "react"
import FormItem, { FormItemProps } from "./FormItem"
import { FieldUpdate, FieldUpdateCb, ValidationRule } from "./types"
import { useField } from "./useForm"
import { FormApi } from "./FormApi"
Expand Down Expand Up @@ -64,11 +64,13 @@ export type FormArrayItemProps<FieldName extends string = string, Value extends
onChange?: (value: FieldUpdate<Value>, event?: unknown) => unknown
}

export const FormArrayItem = <FieldName extends string = string, Value extends unknown[] = unknown[]>({ children, ...props }: FormArrayItemProps<FieldName, Value>) => {
const FormArrayItem = <FieldName extends string = string, Value extends unknown[] = unknown[]>({ children, ...props }: FormArrayItemProps<FieldName, Value>) => {
const form = useFormInstance();
const formArray = useArrayField(form, props.name);

return <FormItem {...props}>
{() => children(formArray)}
</FormItem>
}

export default memo(FormArrayItem) as typeof FormArrayItem
6 changes: 4 additions & 2 deletions src/FormItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { CSSProperties, useCallback, useRef } from 'react';
import React, { CSSProperties, useCallback, useRef, memo } from 'react';
import { useDebounce } from 'react-use';
import { useFormContext } from './FormContext';
import { ValidationRule, ValidationStatus, ValidationError } from './types';
Expand Down Expand Up @@ -28,7 +28,7 @@ export type FormItemProps<
validationDebounceDelay?: number
}

export const FormItem = <Value, FieldName extends string = string, Children extends FormItemChildren = FormItemChildren>(props: FormItemProps<FieldName, Value, Children>) => {
const FormItem = <Value, FieldName extends string = string, Children extends FormItemChildren = FormItemChildren>(props: FormItemProps<FieldName, Value, Children>) => {
const {
children,
name,
Expand Down Expand Up @@ -86,3 +86,5 @@ export const FormItem = <Value, FieldName extends string = string, Children exte
};

FormItem.displayName = 'FormItem';

export default memo(FormItem) as typeof FormItem
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Form from './Form.tsx';
import { FormItem, FormItemProps } from './FormItem.tsx';
import FormItem, { FormItemProps } from './FormItem.tsx';
export { default as FormArrayItem } from './FormArrayItem.tsx';
export * from './Form.tsx';
export * from './FormItem.tsx';
export * from './types.ts';
Expand Down
4 changes: 2 additions & 2 deletions src/useForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { FormApi } from "./FormApi";
import { FormApiGenericTypes, ArrayOnly, ArrayOnlyFields } from "./typesHelpers";
import { FieldUpdate, ValidationRule, ValidationError } from "./types";
import { Form, FormProps } from "./Form";
import { FormItem, FormItemProps } from "./FormItem";
import { FormArrayItem, FormArrayItemProps, useArrayField } from "./FormArrayItem";
import FormItem, { FormItemProps } from "./FormItem";
import FormArrayItem, { FormArrayItemProps, useArrayField } from "./FormArrayItem";

export const useCreateForm = <State extends Record<string, unknown>>(initialState: State) => {
const formApiRef = React.useRef<FormApi<State>>(new FormApi(initialState));
Expand Down

0 comments on commit 3ec54ba

Please sign in to comment.