Skip to content

Commit 626eb84

Browse files
ParroMParolariDoveconvieneepicfaace
authored
Fix rjsf-team#1314 event in onAddClick is no longer mandatory - with test (rjsf-team#1730)
* Fix rjsf-team#1314 event in onAddClick is no longer mandatory * Fix optional field syntax in docs Co-Authored-By: Ashwin Ramaswami <[email protected]> * Update TypeScript types * Checks only for element existence in test * Remove useless attribute in test custom component Co-authored-by: Mauro Parolari <[email protected]> Co-authored-by: Ashwin Ramaswami <[email protected]>
1 parent d6f0964 commit 626eb84

File tree

4 files changed

+74
-40
lines changed

4 files changed

+74
-40
lines changed

docs/advanced-customization.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ The following props are passed to each `ArrayFieldTemplate`:
120120
- `disabled`: A boolean value stating if the array is disabled.
121121
- `idSchema`: Object
122122
- `items`: An array of objects representing the items in the array. Each of the items represent a child with properties described below.
123-
- `onAddClick: (event) => void`: A function that adds a new item to the array.
123+
- `onAddClick: (event?) => void`: A function that adds a new item to the array. `event` is optional.
124124
- `readonly`: A boolean value stating if the array is read-only.
125125
- `required`: A boolean value stating if the array is required.
126126
- `schema`: The schema object for this array.

packages/core/index.d.ts

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ declare module '@rjsf/core' {
181181
readonly: boolean;
182182
key: string;
183183
}[];
184-
onAddClick: (event: any) => (event: any) => void;
184+
onAddClick: (event?: any) => void;
185185
readonly: boolean;
186186
required: boolean;
187187
schema: JSONSchema7;
@@ -270,107 +270,107 @@ declare module '@rjsf/core' {
270270
export module utils {
271271

272272
export const ADDITIONAL_PROPERTY_FLAG: string;
273-
273+
274274
export function getDefaultRegistry(): FieldProps['registry'];
275-
275+
276276
export function getSchemaType(schema: JSONSchema7): string;
277-
277+
278278
export function getWidget(
279279
schema: JSONSchema7,
280280
widget: Widget,
281281
registeredWidgets?: { [name: string]: Widget },
282282
): Widget | Error;
283-
283+
284284
export function hasWidget(
285285
schema: JSONSchema7,
286286
widget: Widget,
287287
registeredWidgets?: { [name: string]: Widget },
288288
): boolean;
289-
289+
290290
export function computeDefaults<T = any>(
291291
schema: JSONSchema7,
292292
parentDefaults: JSONSchema7['default'][],
293293
definitions: FieldProps['registry']['definitions'],
294294
rawFormData?: T,
295295
includeUndefinedValues?: boolean,
296296
): JSONSchema7['default'][];
297-
297+
298298
export function getDefaultFormState<T = any>(
299299
schema: JSONSchema7,
300300
formData: T,
301301
definitions?: FieldProps['registry']['definitions'],
302302
includeUndefinedValues?: boolean,
303303
): T | JSONSchema7['default'][];
304-
304+
305305
export function getUiOptions(uiSchema: UiSchema): UiSchema['ui:options'];
306-
306+
307307
export function isObject(thing: any): boolean;
308-
308+
309309
export function mergeObjects(obj1: object, obj2: object, concatArrays?: boolean): object;
310-
310+
311311
export function asNumber(value: string | null): number | string | undefined | null;
312-
312+
313313
export function orderProperties(properties: [], order: []): [];
314-
314+
315315
export function isConstant(schema: JSONSchema7): boolean;
316-
316+
317317
export function toConstant(schema: JSONSchema7): JSONSchema7Type | JSONSchema7['const'] | Error;
318-
318+
319319
export function isSelect(_schema: JSONSchema7, definitions?: FieldProps['registry']['definitions']): boolean;
320-
320+
321321
export function isMultiSelect(schema: JSONSchema7, definitions?: FieldProps['registry']['definitions']): boolean;
322-
322+
323323
export function isFilesArray(
324324
schema: JSONSchema7,
325325
uiSchema: UiSchema,
326326
definitions?: FieldProps['registry']['definitions'],
327327
): boolean;
328-
328+
329329
export function isFixedItems(schema: JSONSchema7): boolean;
330-
330+
331331
export function allowAdditionalItems(schema: JSONSchema7): boolean;
332-
332+
333333
export function optionsList(schema: JSONSchema7): { label: string; value: string }[];
334-
334+
335335
export function guessType(value: any): JSONSchema7TypeName;
336-
336+
337337
export function stubExistingAdditionalProperties<T = any>(
338338
schema: JSONSchema7,
339339
definitions?: FieldProps['registry']['definitions'],
340340
formData?: T,
341341
): JSONSchema7;
342-
342+
343343
export function resolveSchema<T = any>(
344344
schema: JSONSchema7Definition,
345345
definitions?: FieldProps['registry']['definitions'],
346346
formData?: T,
347347
): JSONSchema7;
348-
348+
349349
export function retrieveSchema<T = any>(
350350
schema: JSONSchema7Definition,
351351
definitions?: FieldProps['registry']['definitions'],
352352
formData?: T,
353353
): JSONSchema7;
354-
354+
355355
export function deepEquals<T>(a: T, b: T): boolean;
356-
356+
357357
export function shouldRender(comp: React.Component, nextProps: any, nextState: any): boolean;
358-
358+
359359
export function toIdSchema<T = any>(
360360
schema: JSONSchema7Definition,
361361
id: string,
362362
definitions: FieldProps['registry']['definitions'],
363363
formData?: T,
364364
idPredix?: string,
365365
): IdSchema | IdSchema[];
366-
366+
367367
export function toPathSchema<T = any>(
368368
schema: JSONSchema7Definition,
369369
name: string | undefined,
370370
definitions: FieldProps['registry']['definitions'],
371371
formData?: T,
372372
): PathSchema | PathSchema[];
373-
373+
374374
export interface DateObject {
375375
year: number;
376376
month: number;
@@ -379,25 +379,25 @@ declare module '@rjsf/core' {
379379
minute: number;
380380
second: number;
381381
}
382-
382+
383383
export function parseDateString(dateString: string, includeTime?: boolean): DateObject;
384-
384+
385385
export function toDateString(dateObject: DateObject, time?: boolean): string;
386-
386+
387387
export function pad(num: number, size: number): string;
388-
388+
389389
export function setState(instance: React.Component, state: any, callback: Function): void;
390-
390+
391391
export function dataURItoBlob(dataURI: string): { name: string; blob: Blob };
392-
392+
393393
export interface IRangeSpec {
394394
min?: number;
395395
max?: number;
396396
step?: number;
397397
}
398-
398+
399399
export function rangeSpec(schema: JSONSchema7): IRangeSpec;
400-
400+
401401
export function getMatchingOption(
402402
formData: any,
403403
options: JSONSchema7[],
@@ -430,4 +430,4 @@ declare module '@rjsf/core/lib/validate' {
430430
additionalMetaSchemas?: FormProps<T>['additionalMetaSchemas'],
431431
customFormats?: FormProps<T>['customFormats'],
432432
): { errors: AjvError[]; errorSchema: ErrorSchema };
433-
}
433+
}

packages/core/src/components/fields/ArrayField.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ class ArrayField extends Component {
281281
};
282282

283283
onAddClick = event => {
284-
event.preventDefault();
284+
if (event) {
285+
event.preventDefault();
286+
}
285287

286288
const { onChange } = this.props;
287289
const newKeyedFormDataRow = {

packages/core/test/ArrayField_test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,27 @@ const ExposedArrayKeyTemplate = function(props) {
6262
);
6363
};
6464

65+
const CustomOnAddClickTemplate = function(props) {
66+
return (
67+
<div className="array">
68+
{props.items &&
69+
props.items.map(element => (
70+
<div key={element.key} className="array-item">
71+
<div>{element.children}</div>
72+
</div>
73+
))}
74+
75+
{props.canAdd && (
76+
<div className="array-item-add">
77+
<button onClick={() => props.onAddClick()} type="button">
78+
Add New
79+
</button>
80+
</div>
81+
)}
82+
</div>
83+
);
84+
};
85+
6586
describe("ArrayField", () => {
6687
let sandbox;
6788
const CustomComponent = props => {
@@ -249,6 +270,17 @@ describe("ArrayField", () => {
249270
.to.be.true;
250271
});
251272

273+
it("should add a field when clicking add button even if event is not passed to onAddClick", () => {
274+
const { node } = createFormComponent({
275+
schema,
276+
ArrayFieldTemplate: CustomOnAddClickTemplate,
277+
});
278+
279+
Simulate.click(node.querySelector(".array-item-add button"));
280+
281+
expect(node.querySelector(".array-item")).not.to.be.null;
282+
});
283+
252284
it("should not provide an add button if length equals maxItems", () => {
253285
const { node } = createFormComponent({
254286
schema: { maxItems: 2, ...schema },

0 commit comments

Comments
 (0)