From: Eduardo San Martin Morote Date: Thu, 8 Apr 2021 09:02:03 +0000 (+0200) Subject: feat: add mapActions X-Git-Tag: v2.0.0-alpha.11~20 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b5d27fbd34b006ae7aec01c091d4e246c3fa73fc;p=thirdparty%2Fvuejs%2Fpinia.git feat: add mapActions --- diff --git a/__tests__/mapHelpers.spec.ts b/__tests__/mapHelpers.spec.ts index 1869554a..d256c84c 100644 --- a/__tests__/mapHelpers.spec.ts +++ b/__tests__/mapHelpers.spec.ts @@ -1,6 +1,7 @@ import { createPinia, defineStore, + mapActions, mapGetters, mapState, mapStores, @@ -151,4 +152,49 @@ describe('Map Helpers', () => { ) }) }) + + describe('mapActions', () => { + const useStore = defineStore({ + id: 'main', + state: () => ({ n: 0 }), + actions: { + increment() { + this.n++ + }, + setN(newN: number) { + return (this.n = newN) + }, + }, + }) + + it('array', () => { + const pinia = createPinia() + const Component = defineComponent({ + template: `

`, + methods: { + ...mapActions(useStore, ['increment', 'setN']), + }, + }) + + const wrapper = mount(Component, { localVue, pinia }) + + expect(wrapper.vm.increment()).toBe(undefined) + expect(wrapper.vm.setN(4)).toBe(4) + }) + + it('object', () => { + const pinia = createPinia() + const Component = defineComponent({ + template: `

`, + methods: { + ...mapActions(useStore, { inc: 'increment', set: 'setN' }), + }, + }) + + const wrapper = mount(Component, { localVue, pinia }) + + expect(wrapper.vm.inc()).toBe(undefined) + expect(wrapper.vm.set(4)).toBe(4) + }) + }) }) diff --git a/src/index.ts b/src/index.ts index fded0df0..7b1caf32 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,6 +15,6 @@ export { DefineStoreOptions, } from './types' -export { mapStores, mapState, mapGetters } from './mapHelpers' +export { mapActions, mapStores, mapState, mapGetters } from './mapHelpers' // TODO: remove in beta export { createStore } from './deprecated' diff --git a/src/mapHelpers.ts b/src/mapHelpers.ts index 42f2b883..e82637e5 100644 --- a/src/mapHelpers.ts +++ b/src/mapHelpers.ts @@ -124,3 +124,106 @@ export function mapState< * Alias for `mapState()`. You should use `mapState()` instead. */ export const mapGetters = mapState + +type MapActionsReturn = { + [key in keyof A]: Store[key] +} + +type MapActionsObjectReturn> = { + [key in keyof T]: Store[T[key]] +} + +/** + * Allows directly using actions from your store without using the composition + * API (`setup()`) by generating an object to be spread in the `methods` field + * of a component. + * + * @example + * ```js + * export default { + * methods: { + * // other methods properties + * ...mapActions(useCounterStore, ['increment', 'setCount']) + * }, + * + * created() { + * this.increment() + * this.setCount(2) // pass arguments as usual + * } + * } + * ``` + * + * @param useStore - store to map from + * @param keys - array of action names to map + */ +export function mapActions( + useStore: StoreDefinition, + keys: Array +): MapActionsReturn +/** + * Allows directly using actions from your store without using the composition + * API (`setup()`) by generating an object to be spread in the `methods` field + * of a component. The values of the object are the actions while the keys are + * the names of the resulting methods. + * + * @example + * ```js + * export default { + * methods: { + * // other methods properties + * // useCounterStore has two actions named `increment` and `setCount` + * ...mapActions(useCounterStore, { moar: 'increment', setIt: 'setCount' }) + * }, + * + * created() { + * this.moar() + * this.setIt(2) + * } + * } + * ``` + * + * @param useStore - store to map from + * @param keyMapper - object to define new names for the actions + */ +export function mapActions< + Id extends string, + S extends StateTree, + G, + A, + KeyMapper extends Record +>( + useStore: StoreDefinition, + keyMapper: KeyMapper +): MapActionsObjectReturn +/** + * Allows directly using actions from your store without using the composition + * API (`setup()`) by generating an object to be spread in the `methods` field + * of a component. + * + * @param useStore - store to map from + * @param keysOrMapper - array or object + */ +export function mapActions< + Id extends string, + S extends StateTree, + G, + A, + KeyMapper extends Record +>( + useStore: StoreDefinition, + keysOrMapper: Array | KeyMapper +): MapActionsReturn | MapActionsObjectReturn { + return Array.isArray(keysOrMapper) + ? keysOrMapper.reduce((reduced, key) => { + reduced[key] = function (this: Vue, ...args: any[]) { + return (getCachedStore(this, useStore)[key] as Method)(...args) + } as Store[keyof A] + return reduced + }, {} as MapActionsReturn) + : Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => { + reduced[key] = function (this: Vue, ...args: any[]) { + return getCachedStore(this, useStore)[keysOrMapper[key]](...args) + } as Store[keyof KeyMapper[]] + return reduced + }, {} as MapActionsObjectReturn) +}