Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 3831dee

Browse files
mei33nekitk
authored andcommitted
Fix crash on calling replace on non-string default props
1 parent 3c60cb3 commit 3831dee

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { action } from '@storybook/addon-actions'
33
import { logger } from '@storybook/client-logger'
44
import { text, boolean, number, object, select } from '@storybook/addon-knobs'
55

6-
const cleanupString = str => str.replace(/^['"](.*)['"]$/, '$1')
6+
const QUOTED_STRING_REGEXP = /^['"](.*)['"]$/
7+
8+
const cleanupValue = (value) => typeof value === 'string' ? value.replace(QUOTED_STRING_REGEXP, '$1') : value
79

810
const knobResolvers = {}
911
export const addKnobResolver = ({ name, resolver, weight = 0 }) => (knobResolvers[name] = { name, resolver, weight })
@@ -51,7 +53,7 @@ const createSelect = (propName, elements, defaultValue, isRequired) => {
5153
try {
5254
const options = elements
5355
// Cleanup string quotes, if any.
54-
.map(value => cleanupString(value.value))
56+
.map(value => cleanupValue(value.value))
5557
.reduce(optionsReducer, {})
5658
const value = defaultValue || (isRequired && Object.values(options)[0]) || undefined
5759
return select(propName, isRequired ? options : withDefaultOption(options), value)
@@ -143,7 +145,7 @@ const resolvePropValues = (propTypes, defaultProps) => {
143145
.map(propName => resolvers.reduce(
144146
(value, resolver) => {
145147
const propType = propTypes[propName] || {}
146-
const defaultValue = defaultProps[propName] || (propType.defaultValue && cleanupString(propType.defaultValue.value || '')) || undefined
148+
const defaultValue = defaultProps[propName] || (propType.defaultValue && cleanupValue(propType.defaultValue.value || '')) || undefined
147149

148150
return value !== undefined ? value
149151
: resolver(propName, propType, defaultValue)

0 commit comments

Comments
 (0)