diff --git a/README.md b/README.md index 740c8a6..adedee2 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/package.json b/package.json new file mode 100644 index 0000000..d10629d --- /dev/null +++ b/package.json @@ -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": "alexanderz@wix.com", + "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" +} diff --git a/index.js b/src/createScope.js similarity index 100% rename from index.js rename to src/createScope.js diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..c951dfd --- /dev/null +++ b/src/index.js @@ -0,0 +1,2 @@ +export { createScope } from './createScope'; +export { useScope } from './useScope'; diff --git a/src/useScope.js b/src/useScope.js new file mode 100644 index 0000000..b96abd0 --- /dev/null +++ b/src/useScope.js @@ -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; + }, + }; +};