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

Commit

Permalink
Fix crash on calling replace on non-string default props
Browse files Browse the repository at this point in the history
  • Loading branch information
mei33 authored and nekitk committed Oct 24, 2019
1 parent 3c60cb3 commit 3831dee
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { action } from '@storybook/addon-actions'
import { logger } from '@storybook/client-logger'
import { text, boolean, number, object, select } from '@storybook/addon-knobs'

const cleanupString = str => str.replace(/^['"](.*)['"]$/, '$1')
const QUOTED_STRING_REGEXP = /^['"](.*)['"]$/

const cleanupValue = (value) => typeof value === 'string' ? value.replace(QUOTED_STRING_REGEXP, '$1') : value

const knobResolvers = {}
export const addKnobResolver = ({ name, resolver, weight = 0 }) => (knobResolvers[name] = { name, resolver, weight })
Expand Down Expand Up @@ -51,7 +53,7 @@ const createSelect = (propName, elements, defaultValue, isRequired) => {
try {
const options = elements
// Cleanup string quotes, if any.
.map(value => cleanupString(value.value))
.map(value => cleanupValue(value.value))
.reduce(optionsReducer, {})
const value = defaultValue || (isRequired && Object.values(options)[0]) || undefined
return select(propName, isRequired ? options : withDefaultOption(options), value)
Expand Down Expand Up @@ -143,7 +145,7 @@ const resolvePropValues = (propTypes, defaultProps) => {
.map(propName => resolvers.reduce(
(value, resolver) => {
const propType = propTypes[propName] || {}
const defaultValue = defaultProps[propName] || (propType.defaultValue && cleanupString(propType.defaultValue.value || '')) || undefined
const defaultValue = defaultProps[propName] || (propType.defaultValue && cleanupValue(propType.defaultValue.value || '')) || undefined

return value !== undefined ? value
: resolver(propName, propType, defaultValue)
Expand Down

0 comments on commit 3831dee

Please sign in to comment.