-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
254 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# getDirectChildren | ||
|
||
Use the `getDirectChildren` function to get a list components which are direct descendants of the given parent instance. This function is helpful when working with nested components which declare themselves as children. | ||
|
||
:::tip | ||
If you need to only check if an instance is a direct descendant of another instance, prefer the [`isDirectChild` helper function](/api/helpers/isDirectChild.md) which will return a `boolean` directly. | ||
::: | ||
|
||
## Usage | ||
|
||
```js {1,9,16} | ||
import { Base, getDirectChildren } from '@studiometa/js-toolkit'; | ||
import Child from './Child.js'; | ||
|
||
class Parent extends Base { | ||
static config = { | ||
name: 'Parent', | ||
components: { | ||
Child, | ||
Parent, // Useful for recursive components only | ||
}, | ||
}; | ||
|
||
onChildClick(index, event) { | ||
const childInstance = this.$children.Child[index]; | ||
const directChildren = getDirectChildren(this, 'Parent', 'Child'); | ||
|
||
if (directChildren.includes(childInstance)) { | ||
event.preventDefault(); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
**Parameters** | ||
|
||
- `parentInstance` (`Base`): the target element | ||
- `parentName` (`string`): the name of the recursive parent as specified in the `config.components` object. | ||
- `childrenName` (`string`): the name of the children components as specified in the `config.components` object. | ||
|
||
**Return value** | ||
|
||
- `Base[]`: a list of the direct child components corresponding to the given `childrenName` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# isDirectChild | ||
|
||
Use the `isDirectChild` function to test if a child component instance is a direct descendant of the given parent instance. This function is helpful when working with nested components which declare themselves as children. | ||
|
||
## Usage | ||
|
||
```js {1,9,16} | ||
import { Base, isDirectChild } from '@studiometa/js-toolkit'; | ||
import Child from './Child.js'; | ||
|
||
class Parent extends Base { | ||
static config = { | ||
name: 'Parent', | ||
components: { | ||
Child, | ||
Parent, // Useful for recursive components only | ||
}, | ||
}; | ||
|
||
onChildClick(index, event) { | ||
const childInstance = this.$children.Child[index]; | ||
|
||
if (isDirectChild(this, 'Parent', 'Child', childInstance)) { | ||
event.preventDefault(); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
**Parameters** | ||
|
||
- `parentInstance` (`Base`): the parent instance. | ||
- `parentName` (`string`): the name of the recursive parent as specified in the `config.components` object. | ||
- `childName` (`string`): the name of the child component as specified in the `config.components` object. | ||
- `childInstance` (`Base`): the child instance. | ||
|
||
**Return value** | ||
|
||
- `boolean`: `true` if the given child instance is a direct descendant of the given parent instance. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { isArray } from '../utils/index.js'; | ||
|
||
/** | ||
* @typedef {import('../Base/index.js').default} Base | ||
*/ | ||
|
||
/** | ||
* Get direct children from a parent when working with nested components. | ||
* | ||
* @template {Base} T | ||
* @param {Base} parentInstance | ||
* @param {string} parentName | ||
* @param {string} childrenName | ||
* @returns {Array<T>} | ||
*/ | ||
export function getDirectChildren(parentInstance, parentName, childrenName) { | ||
const children = parentInstance.$children[childrenName]; | ||
const nestedParents = parentInstance.$children[parentName]; | ||
|
||
if (!isArray(children)) { | ||
return []; | ||
} | ||
|
||
if (!isArray(nestedParents) || nestedParents.length <= 0) { | ||
return children; | ||
} | ||
|
||
return children.filter((child) => { | ||
return nestedParents.every((nestedParent) => { | ||
const nestedChildren = nestedParent.$children[childrenName]; | ||
/* istanbul ignore next */ | ||
return isArray(nestedChildren) ? !nestedChildren.includes(child) : true; | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Test if a component instance is a direct child from the given component. | ||
* | ||
* @param {Base} parentInstance | ||
* @param {string} parentName | ||
* @param {string} childrenName | ||
* @param {Base} childInstance | ||
* @returns {boolean} | ||
*/ | ||
export function isDirectChild(parentInstance, parentName, childrenName, childInstance) { | ||
return getDirectChildren(parentInstance, parentName, childrenName).includes(childInstance); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`helpers exports 1`] = ` | ||
Array [ | ||
"createApp", | ||
"getDirectChildren", | ||
"getInstanceFromElement", | ||
"importOnInteraction", | ||
"importWhenIdle", | ||
"importWhenVisible", | ||
"isDirectChild", | ||
] | ||
`; |
Oops, something went wrong.