diff --git a/package-lock.json b/package-lock.json
index 63828d0..2722d2c 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "@ukhomeoffice/asl-components",
- "version": "13.4.0",
+ "version": "13.5.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@ukhomeoffice/asl-components",
- "version": "13.4.0",
+ "version": "13.5.1",
"license": "MIT",
"dependencies": {
"@ukhomeoffice/asl-constants": "^2.1.5",
diff --git a/package.json b/package.json
index 4523e2c..72608cf 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "@ukhomeoffice/asl-components",
- "version": "13.5.0",
+ "version": "13.5.1",
"description": "React components for ASL layouts and elements",
"main": "src/index.jsx",
"styles": "styles/index.scss",
diff --git a/src/snippet/index.jsx b/src/snippet/index.jsx
index 1aa8c9c..1bfb4cb 100644
--- a/src/snippet/index.jsx
+++ b/src/snippet/index.jsx
@@ -4,12 +4,24 @@ import { connect } from 'react-redux';
import Markdown from '../markdown';
import { render } from 'mustache';
+function getKeysToTry(primary, fallback) {
+ if (Array.isArray(fallback)) {
+ return [primary, ...fallback];
+ }
+
+ if(['string', 'number'].includes(typeof fallback)) {
+ return [primary, fallback];
+ }
+
+ return [primary];
+}
+
function getTemplate(content, primary, fallback) {
- const keysToTry = [primary, ...(Array.isArray(fallback) ? fallback : [fallback])];
+ const keysToTry = getKeysToTry(primary, fallback);
for (let key of keysToTry) {
const template = get(content, key);
- if (template != undefined) {
+ if (typeof template === 'string') {
return template;
}
}
@@ -19,16 +31,15 @@ function getTemplate(content, primary, fallback) {
export const Snippet = ({ content, children, optional, fallback, ...props }) => {
const str = getTemplate(content, children, fallback);
+
if (str === undefined && optional) {
return null;
-
}
+
if (str === undefined) {
- throw new Error(`Failed to lookup content snippet: ${children}`);
- }
- if (typeof str !== 'string') {
- throw new Error(`Invalid content snippet for key ${children}: ${JSON.stringify(str)}`);
+ throw new Error(`Failed to lookup content snippet. Tried keys: ${JSON.stringify(getKeysToTry(children, fallback))}`);
}
+
const source = render(str, props);
return (
diff --git a/src/snippet/index.spec.jsx b/src/snippet/index.spec.jsx
index 04d2ff1..62e30a7 100644
--- a/src/snippet/index.spec.jsx
+++ b/src/snippet/index.spec.jsx
@@ -20,7 +20,11 @@ describe('