Skip to content

Commit

Permalink
Add useScope
Browse files Browse the repository at this point in the history
  • Loading branch information
shoonia committed Apr 18, 2021
1 parent f072c56 commit 053e60d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 47 deletions.
47 changes: 0 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,6 @@

Create handlers for the repeater items.

**`public/util.js`**

```js
/**
* Create Repeated Item Scope
* https://github.com/shoonia/repeater-scope
*
* @typedef {{
* _id: string;
* [key: string]: any;
* }} ItemData;
*
* @typedef {{
* $item: $w.$w;
* itemData: ItemData;
* index: number;
* data: ItemData[];
* }} ScopeData;
*
* @param {() => ItemData[]} getData
* @returns {(event: $w.Event) => ScopeData}
*/
export const createScope = (getData) => (event) => {
const itemId = event.context.itemId;
const find = (i) => i._id === itemId;

return {
// @ts-ignore
$item: $w.at(event.context),

get itemData() {
return getData().find(find);
},

get index() {
return getData().findIndex(find);
},

get data() {
return getData();
},
};
};

```

**`Page Code`**

```js
Expand All @@ -71,7 +25,6 @@ export function repeatedButton_dblClick(event) {
}
```

- [Code Snippet](/index.js)
- [Velo by Wix: Event handling of Repeater Item](https://shoonia.site/event-handling-of-repeater-item/)

## MIT
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "repeater-scope",
"version": "1.0.0",
"description": "Create handlers for the repeater items",
"main": "src/index.js",
"scripts": {},
"sideEffects": false,
"keywords": [
"wix",
"corvid",
"wixcode",
"editorx",
"velo"
],
"author": {
"name": "Alexander Zaytsev",
"email": "[email protected]",
"url": "https://twitter.com/_shoonia"
},
"repository": {
"type": "git",
"url": "git+https://github.com/shoonia/repeater-scope.git"
},
"bugs": {
"url": "https://github.com/shoonia/repeater-scope/issues"
},
"homepage": "https://github.com/shoonia/repeater-scope#readme",
"license": "MIT"
}
File renamed without changes.
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { createScope } from './createScope';
export { useScope } from './useScope';
44 changes: 44 additions & 0 deletions src/useScope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Use Repeater Scope
* https://github.com/shoonia/repeater-scope
*
* @typedef {{
* _id: string;
* [key: string]: any;
* }} ItemData;
*
* @typedef {{
* $item: $w.$w;
* itemData: ItemData;
* index: number;
* data: ItemData[];
* }} ScopeData;
*
* @param {$w.Event} event
* @returns {ScopeData}
*/
export const useScope = (event) => {
const ctx = event.context;
const find = (i) => i._id === ctx.itemId;

let repeter = event.target;
// @ts-ignore
while ((repeter = repeter.parent).type !== '$w.Repeater');

return {
// @ts-ignore
$item: $w.at(ctx),

get itemData() {
return repeter.data.find(find);
},

get index() {
return repeter.data.findIndex(find);
},

get data() {
return repeter.data;
},
};
};

0 comments on commit 053e60d

Please sign in to comment.