Merge pull request #1482 from alexisfontaine/container-add-empty-array

Handle adding empty children arrays to a container
This commit is contained in:
Anton Lavrenov 2023-01-17 08:34:46 -05:00 committed by GitHub
commit a54cfcae48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -109,22 +109,27 @@ export abstract class Container<
* add a child and children into container * add a child and children into container
* @name Konva.Container#add * @name Konva.Container#add
* @method * @method
* @param {...Konva.Node} child * @param {...Konva.Node} children
* @returns {Container} * @returns {Container}
* @example * @example
* layer.add(rect); * layer.add(rect);
* layer.add(shape1, shape2, shape3); * layer.add(shape1, shape2, shape3);
* // empty arrays are accepted, though each individual child must be defined
* layer.add(...shapes);
* // remember to redraw layer if you changed something * // remember to redraw layer if you changed something
* layer.draw(); * layer.draw();
*/ */
add(...children: ChildType[]) { add(...children: ChildType[]) {
if (arguments.length > 1) { if (children.length === 0) {
for (var i = 0; i < arguments.length; i++) { return this;
this.add(arguments[i]); }
if (children.length > 1) {
for (var i = 0; i < children.length; i++) {
this.add(children[i]);
} }
return this; return this;
} }
var child = children[0]; const child = children[0];
if (child.getParent()) { if (child.getParent()) {
child.moveTo(this); child.moveTo(this);
return this; return this;