Skip to content

Commit e0ce89f

Browse files
author
vvo
committed
fix: allow passing only one key of transformData as an object
before things like transformData{hit} was not working because missing `empty` property.
1 parent 32aaeb6 commit e0ce89f

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

components/Template.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ class Template {
1010
this.props.templatesConfig.compileOptions :
1111
{},
1212
helpers: this.props.templatesConfig.helpers,
13-
data: this.props.transformData ?
14-
getTransformData(this.props.transformData, this.props.templateKey)(this.props.data) :
15-
this.props.data
13+
data: transformData(this.props.transformData, this.props.templateKey, this.props.data)
1614
});
1715

1816
if (content === null) {
@@ -56,14 +54,19 @@ Template.defaultProps = {
5654
data: {}
5755
};
5856

59-
function getTransformData(transformData, templateKey) {
60-
if (typeof transformData === 'function') {
61-
return transformData;
62-
} else if (typeof transformData[templateKey] === 'function') {
63-
return transformData[templateKey];
57+
function transformData(fn, templateKey, originalData) {
58+
if (!fn) {
59+
return originalData;
6460
}
6561

66-
throw new Error('transformData should be a function or an object');
62+
if (typeof fn === 'function') {
63+
return fn(originalData);
64+
} else if (typeof fn === 'object') {
65+
// ex: transformData: {hit, empty}
66+
return fn[templateKey] && fn[templateKey](originalData) || originalData;
67+
}
68+
69+
throw new Error('`transformData` must be a function or an object');
6770
}
6871

6972
module.exports = Template;

widgets/menu.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function menu({
4343
item: null
4444
},
4545
templates = defaultTemplates,
46-
transformData = null,
46+
transformData,
4747
hideWhenNoResults = true
4848
}) {
4949
hierarchicalCounter++;

0 commit comments

Comments
 (0)