-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
refactor: types for withTheme #2279
Changes from all commits
d9fc78d
d3a5c94
ab45b18
2a86f44
92b6d43
190245a
3af9783
49634db
3cb4bb1
435b1b2
3cb0392
53cba5e
53209bc
c73c745
b9b911d
fe09772
4193cb2
b789a2e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
import { withTheme, FormProps } from "@rjsf/core"; | ||
|
||
import CoreForm from "@rjsf/core"; | ||
import { withTheme } from "@rjsf/core"; | ||
import Theme from "../Theme"; | ||
import { StatelessComponent } from "react"; | ||
|
||
const Form: | ||
| React.ComponentClass<FormProps<any>> | ||
| StatelessComponent<FormProps<any>> = withTheme(Theme); | ||
const Form: typeof CoreForm = withTheme(Theme); | ||
|
||
export default Form; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,7 @@ declare module '@rjsf/core' { | |
onChange: (formData: T, newErrorSchema: ErrorSchema) => void; | ||
onBlur: (id: string, value: boolean | number | string | null) => void; | ||
submit: () => void; | ||
formElement: HTMLFormElement | null; | ||
epicfaace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
export type UiSchema = { | ||
|
@@ -274,7 +275,7 @@ declare module '@rjsf/core' { | |
|
||
export function withTheme<T = any>( | ||
themeProps: ThemeProps<T>, | ||
): React.ComponentClass<FormProps<T>> | React.StatelessComponent<FormProps<T>>; | ||
): typeof Form; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. edit: add back Here's my take on this, in use in a company project for a year. For reference, it is then used in a module that has form factory filling specific needs in our project (additional default behavior, external styling additions, data type references in event handlers, error message translation, etc..). withTheme.tsx import React, { ReactElement, ReactNode, Ref, RefAttributes, forwardRef } from 'react';
import JsonSchemaForm, { FormProps, ThemeProps } from '@rjsf/core';
type TFormWithTheme = <T = {}>(
p: FormProps<T> & { readonly children?: ReactNode } & RefAttributes<JsonSchemaForm<T>>
) => ReactElement | null;
export function withTheme<T = {}>(themeProps: ThemeProps<T>): TFormWithTheme {
return forwardRef(
(props: FormProps<T> & { readonly children?: ReactNode }, ref: Ref<JsonSchemaForm<T>>) => {
const { fields, widgets, children, ...directProps } = props;
return (
<JsonSchemaForm<T>
{...themeProps}
{...directProps}
fields={{ ...themeProps.fields, ...fields }}
widgets={{ ...themeProps.widgets, ...widgets }}
ref={ref}
>
{children}
</JsonSchemaForm>
);
}
) as TFormWithTheme;
} usage.tsx const FormWithMUITheme = withTheme(MuiTheme);
type TForm = <T = {}>(
p: FormProps<T> & RefAttributes<JsonSchemaForm<T>>
) => ReactElement | null;
export const Form: TForm = forwardRef(
<T extends unknown = {}>(props: FormProps<T>, ref: Ref<JsonSchemaForm<T>>) => {
const { whatever, ...otherProps } = props;
/* various operations */
return (
<FormWithMUITheme<T>
ref={ref}
{...otherProps}
/>
)
};
) as TForm; |
||
|
||
export type AddButtonProps = { | ||
className: string; | ||
|
@@ -436,8 +437,7 @@ declare module '@rjsf/core' { | |
} | ||
|
||
declare module '@rjsf/core/lib/components/fields/SchemaField' { | ||
import { JSONSchema7 } from 'json-schema'; | ||
import { FieldProps, UiSchema, IdSchema, FormValidation } from '@rjsf/core'; | ||
import { FieldProps } from '@rjsf/core'; | ||
|
||
export type SchemaFieldProps<T = any> = Pick< | ||
FieldProps<T>, | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"lib": [ | ||
"es6", | ||
"dom" | ||
], | ||
"noImplicitAny": true, | ||
"noImplicitThis": true, | ||
"strictNullChecks": true, | ||
"strictFunctionTypes": true, | ||
"baseUrl": "../", | ||
"typeRoots": [ | ||
"../" | ||
], | ||
"types": [], | ||
"noEmit": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"jsx": "react" | ||
}, | ||
"files": [ | ||
"index.d.ts", | ||
"typing_test.tsx" | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is a type "given"? Should we show an example code of how to do so?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ping @mattcosta7
Would be nice if we can merge this PR.