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

fix: no knobs after select #60

Merged
merged 3 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
dist
settings.json
.idea
11 changes: 6 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ typeKnobsMap.forEach(({ name, knob, args = [] }, weight) => addKnobResolver({
}))

const optionsReducer = (res, value) => ({ ...res, [value]: value })
const withDefaultOption = (options) => ({ '': '--', ...options })
const createSelect = (propName, elements, defaultProps) => {
const withDefaultOption = (options) => ({ '--': undefined, ...options })
const createSelect = (propName, elements, defaultProps, isRequired) => {
try {
const options = elements
// Cleanup string quotes, if any.
.map(value => value.value.replace(/^['"](.*)['"]$/, '$1'))
.reduce(optionsReducer, {})
return select(propName, withDefaultOption(options), defaultProps[propName])
const value = defaultProps[propName] || (isRequired && Object.values(options)[0]) || undefined
return select(propName, isRequired ? options : withDefaultOption(options), value)
}
catch (e) { }
}
Expand All @@ -59,11 +60,11 @@ addKnobResolver({
name: 'PropTypes.oneOf',
resolver: (propName, propType, value, propTypes, defaultProps) => {
if (propType.type.name === 'enum' && propType.type.value.length) {
return createSelect(propName, propType.type.value, defaultProps)
return createSelect(propName, propType.type.value, defaultProps, propType.required)
}
// for flow support
if (propType.type.name === 'union' && propType.type.elements) {
return createSelect(propName, propType.type.elements, defaultProps)
return createSelect(propName, propType.type.elements, defaultProps, propType.required)
}
}
})
Expand Down
Loading