Skip to content

Commit 63cac57

Browse files
committed
Add types from DefinitelyTyped
1 parent 35bb61c commit 63cac57

File tree

3 files changed

+349
-0
lines changed

3 files changed

+349
-0
lines changed

packages/core/package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"@babel/preset-react": "^7.0.0",
6464
"@babel/preset-typescript": "^7.7.4",
6565
"@babel/register": "^7.7.4",
66+
"@types/json-schema": "^7.0.3",
6667
"atob": "^2.0.3",
6768
"babel-eslint": "^10.0.1",
6869
"babel-loader": "^8.0.6",

packages/core/src/types.ts

Lines changed: 342 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,345 @@ export const fieldProps = {
3434
}),
3535
}),
3636
};
37+
38+
declare module 'react-jsonschema-form' {
39+
import * as React from 'react';
40+
import { JSONSchema6, JSONSchema6Type } from 'json-schema';
41+
42+
type ErrorSchema = {
43+
[k: string]: ErrorSchema;
44+
};
45+
46+
export interface FormProps<T> {
47+
schema: JSONSchema6;
48+
disabled?: boolean;
49+
uiSchema?: UiSchema;
50+
formData?: T;
51+
formContext?: any;
52+
widgets?: { [name: string]: Widget };
53+
fields?: { [name: string]: Field };
54+
noValidate?: boolean;
55+
noHtml5Validate?: boolean;
56+
showErrorList?: boolean;
57+
ErrorList?: React.StatelessComponent<ErrorListProps>;
58+
validate?: (formData: T, errors: FormValidation) => FormValidation;
59+
onBlur?: (id: string, value: boolean | number | string | null) => void;
60+
onChange?: (e: IChangeEvent<T>, es?: ErrorSchema) => any;
61+
onError?: (e: any) => any;
62+
onSubmit?: (e: ISubmitEvent<T>) => any;
63+
liveValidate?: boolean;
64+
FieldTemplate?: React.StatelessComponent<FieldTemplateProps>;
65+
ArrayFieldTemplate?: React.StatelessComponent<ArrayFieldTemplateProps>;
66+
ObjectFieldTemplate?: React.StatelessComponent<ObjectFieldTemplateProps>;
67+
safeRenderCompletion?: boolean;
68+
transformErrors?: (errors: AjvError[]) => AjvError[];
69+
idPrefix?: string;
70+
71+
// HTML Attributes
72+
id?: string;
73+
className?: string;
74+
name?: string;
75+
method?: string;
76+
target?: string;
77+
action?: string;
78+
autocomplete?: string;
79+
enctype?: string;
80+
acceptcharset?: string;
81+
}
82+
83+
export default class Form<T> extends React.Component<FormProps<T>> {
84+
submit: () => void;
85+
}
86+
87+
export type UiSchema = {
88+
'ui:field'?: Field | string;
89+
'ui:widget'?: Widget | string;
90+
'ui:options'?: { [key: string]: boolean | number | string | object | any[] | null };
91+
'ui:order'?: string[];
92+
[name: string]: any;
93+
};
94+
95+
export type FieldId = {
96+
$id: string;
97+
};
98+
99+
export type IdSchema = FieldId & {
100+
[key: string]: FieldId;
101+
};
102+
103+
export interface WidgetProps
104+
extends Pick<
105+
React.HTMLAttributes<HTMLElement>,
106+
Exclude<keyof React.HTMLAttributes<HTMLElement>, 'onBlur' | 'onFocus'>
107+
> {
108+
id: string;
109+
schema: JSONSchema6;
110+
value: any;
111+
required: boolean;
112+
disabled: boolean;
113+
readonly: boolean;
114+
autofocus: boolean;
115+
onChange: (value: any) => void;
116+
options: { [key: string]: boolean | number | string | object | null };
117+
formContext: any;
118+
onBlur: (id: string, value: boolean | number | string | null) => void;
119+
onFocus: (id: string, value: boolean | number | string | null) => void;
120+
label: string;
121+
}
122+
123+
export type Widget = React.StatelessComponent<WidgetProps> | React.ComponentClass<WidgetProps>;
124+
125+
export interface FieldProps<T = any> extends React.HTMLAttributes<HTMLElement> {
126+
schema: JSONSchema6;
127+
uiSchema: UiSchema;
128+
idSchema: IdSchema;
129+
formData: T;
130+
errorSchema: ErrorSchema;
131+
onChange: (e: IChangeEvent<T> | any, es?: ErrorSchema) => any;
132+
registry: {
133+
fields: { [name: string]: Field };
134+
widgets: { [name: string]: Widget };
135+
definitions: { [name: string]: any };
136+
formContext: any;
137+
};
138+
formContext: any;
139+
autofocus: boolean;
140+
disabled: boolean;
141+
readonly: boolean;
142+
required: boolean;
143+
name: string;
144+
[prop: string]: any;
145+
}
146+
147+
export type Field = React.StatelessComponent<FieldProps> | React.ComponentClass<FieldProps>;
148+
149+
export type FieldTemplateProps = {
150+
id: string;
151+
classNames: string;
152+
label: string;
153+
description: React.ReactElement;
154+
rawDescription: string;
155+
children: React.ReactElement;
156+
errors: React.ReactElement;
157+
rawErrors: string[];
158+
help: React.ReactElement;
159+
rawHelp: string;
160+
hidden: boolean;
161+
required: boolean;
162+
readonly: boolean;
163+
disabled: boolean;
164+
displayLabel: boolean;
165+
fields: Field[];
166+
schema: JSONSchema6;
167+
uiSchema: UiSchema;
168+
formContext: any;
169+
};
170+
171+
export type ArrayFieldTemplateProps<T = any> = {
172+
DescriptionField: React.StatelessComponent<{ id: string; description: string | React.ReactElement }>;
173+
TitleField: React.StatelessComponent<{ id: string; title: string; required: boolean }>;
174+
canAdd: boolean;
175+
className: string;
176+
disabled: boolean;
177+
idSchema: IdSchema;
178+
items: {
179+
children: React.ReactElement;
180+
className: string;
181+
disabled: boolean;
182+
hasMoveDown: boolean;
183+
hasMoveUp: boolean;
184+
hasRemove: boolean;
185+
hasToolbar: boolean;
186+
index: number;
187+
onDropIndexClick: (index: number) => (event: any) => void;
188+
onReorderClick: (index: number, newIndex: number) => (event: any) => void;
189+
readonly: boolean;
190+
}[];
191+
onAddClick: (event: any) => (event: any) => void;
192+
readonly: boolean;
193+
required: boolean;
194+
schema: JSONSchema6;
195+
uiSchema: UiSchema;
196+
title: string;
197+
formContext: any;
198+
formData: T;
199+
registry: FieldProps['registry'];
200+
};
201+
202+
export type ObjectFieldTemplateProps<T = any> = {
203+
DescriptionField: React.StatelessComponent<{ id: string; description: string | React.ReactElement }>;
204+
TitleField: React.StatelessComponent<{ id: string; title: string; required: boolean }>;
205+
title: string;
206+
description: string;
207+
properties: {
208+
content: React.ReactElement;
209+
name: string;
210+
disabled: boolean;
211+
readonly: boolean;
212+
}[];
213+
required: boolean;
214+
schema: JSONSchema6;
215+
uiSchema: UiSchema;
216+
idSchema: IdSchema;
217+
formData: T;
218+
formContext: any;
219+
};
220+
221+
export type AjvError = {
222+
message: string;
223+
name: string;
224+
params: any;
225+
property: string;
226+
stack: string;
227+
};
228+
229+
export type ErrorListProps = {
230+
errorSchema: FormValidation;
231+
errors: AjvError[];
232+
formContext: any;
233+
schema: JSONSchema6;
234+
uiSchema: UiSchema;
235+
};
236+
237+
export interface IChangeEvent<T = any> {
238+
edit: boolean;
239+
formData: T;
240+
errors: AjvError[];
241+
errorSchema: FormValidation;
242+
idSchema: IdSchema;
243+
schema: JSONSchema6;
244+
uiSchema: UiSchema;
245+
status?: string;
246+
}
247+
248+
export type ISubmitEvent<T> = IChangeEvent<T>;
249+
250+
export type FieldError = string;
251+
252+
type FieldValidation = {
253+
__errors: FieldError[];
254+
addError: (message: string) => void;
255+
};
256+
257+
type FormValidation = FieldValidation & {
258+
[fieldName: string]: FieldValidation;
259+
};
260+
261+
type FormSubmit<T = any> = {
262+
formData: T;
263+
};
264+
265+
export type ThemeProps<T = any> = Omit<FormProps<T>, 'schema'>;
266+
267+
export function withTheme<T = any>(
268+
themeProps: ThemeProps<T>
269+
): React.ComponentClass<FormProps<T>> | React.StatelessComponent<FormProps<T>>;
270+
271+
export type AddButtonProps = {
272+
className: string;
273+
onClick: (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
274+
disabled: boolean;
275+
};
276+
}
277+
278+
declare module 'react-jsonschema-form/lib/components/fields/SchemaField' {
279+
import { JSONSchema6 } from 'json-schema';
280+
import { FieldProps, UiSchema, IdSchema, FormValidation } from 'react-jsonschema-form';
281+
282+
export type SchemaFieldProps<T = any> = Pick<
283+
FieldProps<T>,
284+
| 'schema'
285+
| 'uiSchema'
286+
| 'idSchema'
287+
| 'formData'
288+
| 'errorSchema'
289+
| 'registry'
290+
>;
291+
292+
export default class SchemaField extends React.Component<
293+
SchemaFieldProps
294+
> {}
295+
}
296+
297+
declare module 'react-jsonschema-form/lib/utils' {
298+
import { JSONSchema6, JSONSchema6Definition, JSONSchema6Type } from 'json-schema';
299+
import { FieldProps, UiSchema, IdSchema, Widget } from 'react-jsonschema-form';
300+
301+
export const ADDITIONAL_PROPERTY_FLAG: string;
302+
303+
export function getDefaultRegistry(): FieldProps['registry'];
304+
305+
export function getSchemaType(schema: JSONSchema6): string;
306+
307+
export function getWidget(
308+
schema: JSONSchema6,
309+
widget: Widget,
310+
registeredWidgets: { [name: string]: Widget }
311+
): Widget | Error;
312+
313+
export function hasWidget(
314+
schema: JSONSchema6,
315+
wdiget: Widget,
316+
registeredWidgets: { [name: string]: Widget }
317+
): boolean;
318+
319+
export function computeDefaults<T = any>(
320+
schema: JSONSchema6,
321+
parentDefaults: JSONSchema6['default'][],
322+
definitions: FieldProps['registry']['definitions'],
323+
rawFormData: T
324+
): JSONSchema6['default'][];
325+
326+
export function getDefaultFormState<T = any>(
327+
schema: JSONSchema6,
328+
formData: T,
329+
definitions: FieldProps['registry']['definitions']
330+
): T | JSONSchema6['default'][];
331+
332+
export function getUiOptions(uiSchema: UiSchema): UiSchema['ui:options'];
333+
334+
export function isObject(thing: any): boolean;
335+
336+
export function mergeObjects(obj1: object, obj2: object, concatArrays: boolean): object;
337+
338+
export function asNumber(value: any): Number | string;
339+
340+
export function orderProperties(properties: [], order: []): [];
341+
342+
export function isConstant(schema: JSONSchema6): boolean;
343+
344+
export function toConstant(schema: JSONSchema6): JSONSchema6Type | JSONSchema6['const'] | Error;
345+
346+
export function isSelect(_schema: JSONSchema6, definitions: FieldProps['registry']['definitions']): boolean;
347+
348+
export function isMultiSelect(schema: JSONSchema6, definitions: FieldProps['registry']['definitions']): boolean;
349+
350+
export function dataURItoBlob(dataURI: string): { name: string; blob: Blob };
351+
352+
export function shouldRender(comp: React.Component, nextProps: any, nextState: any): boolean;
353+
354+
export function setState(instance: React.Component, state: any, callback: Function): void;
355+
356+
export function guessType(value: any): string;
357+
358+
export interface IRangeSpec {
359+
min?: number;
360+
max?: number;
361+
step?: number;
362+
}
363+
364+
export function rangeSpec(schema: JSONSchema6): IRangeSpec;
365+
366+
export function resolveSchema<T = any>(
367+
schema: JSONSchema6Definition,
368+
definitions: FieldProps['registry']['definitions'],
369+
formData: T
370+
): JSONSchema6;
371+
}
372+
373+
declare module 'react-jsonschema-form/lib/validate' {
374+
import { JSONSchema6Definition } from 'json-schema';
375+
import { AjvError } from 'react-jsonschema-form';
376+
377+
export default function validateFormData<T = any>(formData: T, schema: JSONSchema6Definition): AjvError[];
378+
}

0 commit comments

Comments
 (0)