Skip to content

Commit cf0b065

Browse files
committed
Make it possible to pass namespace to mapFields()
Fixes #23
1 parent 3122683 commit cf0b065

File tree

3 files changed

+112
-4
lines changed

3 files changed

+112
-4
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,27 @@ export default {
370370
</script>
371371
```
372372

373+
Or you can pass the module namespace string as the first argument of the `mapFields()` function.
374+
375+
```html
376+
<template>
377+
<div id="app">
378+
<input v-model="foo">
379+
</div>
380+
</template>
381+
382+
<script>
383+
import { mapFields } from 'vuex-map-fields';
384+
385+
export default {
386+
computed: {
387+
// `fooModule` is the name of the Vuex module.
388+
...mapFields(`fooModule`, ['foo']),
389+
},
390+
};
391+
</script>
392+
```
393+
373394
### Multi-row fields
374395

375396
If you want to build a form which allows the user to enter multiple rows of a specific data type with multiple fields (e.g. multiple addresses) you can use the multi-row field mapping function.

src/index.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
import arrayToObject from './lib/array-to-object';
22

3+
function normalizeNamespace(fn) {
4+
return (namespace, map, getterType, mutationType) => {
5+
/* eslint-disable no-param-reassign */
6+
if (typeof namespace !== `string`) {
7+
mutationType = getterType;
8+
getterType = map;
9+
map = namespace;
10+
namespace = ``;
11+
} else if (namespace.charAt(namespace.length - 1) !== `/`) {
12+
namespace += `/`;
13+
}
14+
15+
getterType = `${namespace}${getterType || `getField`}`;
16+
mutationType = `${namespace}${mutationType || `updateField`}`;
17+
/* eslint-enable */
18+
19+
return fn(namespace, map, getterType, mutationType);
20+
};
21+
}
22+
323
export function getField(state) {
424
return path => path.split(/[.[\]]+/).reduce((prev, key) => prev[key], state);
525
}
@@ -15,7 +35,7 @@ export function updateField(state, { path, value }) {
1535
}, state);
1636
}
1737

18-
export function mapFields(fields, getterType = `getField`, mutationType = `updateField`) {
38+
export const mapFields = normalizeNamespace((namespace, fields, getterType, mutationType) => {
1939
const fieldsObject = Array.isArray(fields) ? arrayToObject(fields) : fields;
2040

2141
return Object.keys(fieldsObject).reduce((prev, key) => {
@@ -34,9 +54,15 @@ export function mapFields(fields, getterType = `getField`, mutationType = `updat
3454

3555
return prev;
3656
}, {});
37-
}
57+
});
3858

39-
export function mapMultiRowFields(paths, getterType = `getField`, mutationType = `updateField`) {
59+
export const mapMultiRowFields = normalizeNamespace((
60+
namespace,
61+
paths,
62+
getterType,
63+
mutationType,
64+
) => {
65+
// export function mapMultiRowFields(paths, getterType = `getField`, mutationType = `updateField`) {
4066
const pathsObject = Array.isArray(paths) ? arrayToObject(paths) : paths;
4167

4268
return Object.keys(pathsObject).reduce((entries, key) => {
@@ -66,7 +92,7 @@ export function mapMultiRowFields(paths, getterType = `getField`, mutationType =
6692

6793
return entries;
6894
}, {});
69-
}
95+
});
7096

7197
export const createHelpers = ({ getterType, mutationType }) => ({
7298
[getterType]: getField,
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import Vuex from 'vuex';
2+
import { createLocalVue, shallowMount } from '@vue/test-utils';
3+
4+
import { mapFields, getField, updateField } from './package/src';
5+
6+
const localVue = createLocalVue();
7+
8+
localVue.use(Vuex);
9+
10+
describe(`Component initialized with namespaced Vuex module.`, () => {
11+
let Component;
12+
let store;
13+
let wrapper;
14+
15+
beforeEach(() => {
16+
Component = {
17+
template: `<input id="foo" v-model="foo">`,
18+
computed: {
19+
...mapFields(`fooModule`, [
20+
`foo`,
21+
]),
22+
},
23+
};
24+
25+
store = new Vuex.Store({
26+
modules: {
27+
fooModule: {
28+
namespaced: true,
29+
state: {
30+
foo: ``,
31+
},
32+
getters: {
33+
getField,
34+
},
35+
mutations: {
36+
updateField,
37+
},
38+
},
39+
},
40+
});
41+
42+
wrapper = shallowMount(Component, { localVue, store });
43+
});
44+
45+
test(`It should render the component.`, () => {
46+
expect(wrapper.exists()).toBe(true);
47+
});
48+
49+
test(`It should update field values when the store is updated.`, () => {
50+
store.state.fooModule.foo = `foo`;
51+
52+
expect(wrapper.element.value).toBe(`foo`);
53+
});
54+
55+
test(`It should update the store when the field values are updated.`, () => {
56+
wrapper.element.value = `foo`;
57+
wrapper.trigger(`input`);
58+
59+
expect(store.state.fooModule.foo).toBe(`foo`);
60+
});
61+
});

0 commit comments

Comments
 (0)