Skip to content

Commit

Permalink
Update Component.replaceWith
Browse files Browse the repository at this point in the history
  • Loading branch information
artf committed Nov 8, 2023
1 parent 8bb884c commit ae1107a
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/dom_components/model/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,16 +503,18 @@ export default class Component extends StyleableModel<ComponentProperties> {
/**
* Replace a component with another one
* @param {String|Component} el Component or HTML string
* @return {Component|Array<Component>} New added component/s
* @param {Object} [opts={}] Options for the append action
* @returns {Array<Component>} New replaced components
* @example
* component.replaceWith('<div>Some new content</div>');
* // -> Component
* const result = component.replaceWith('<div>Some new content</div>');
* // result -> [Component]
*/
replaceWith(el: Component | String) {
replaceWith<C extends Component = Component>(el: ComponentAdd, opts: AddOptions = {}): C[] {
const coll = this.collection;
const at = coll.indexOf(this);
coll.remove(this);
return coll.add(el, { at });
const result = coll.add(el, { ...opts, at });
return isArray(result) ? result : [result];
}

/**
Expand Down

0 comments on commit ae1107a

Please sign in to comment.