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

Commit

Permalink
Merge pull request #60 from nekitk/fix-select-order
Browse files Browse the repository at this point in the history
fix: no knobs after select
  • Loading branch information
nekitk authored Oct 24, 2019
2 parents a25560a + 1fc39da commit bd18398
Show file tree
Hide file tree
Showing 3 changed files with 1,455 additions and 1,305 deletions.
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

1 comment on commit bd18398

@vercel
Copy link

@vercel vercel bot commented on bd18398 Oct 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deployment failed with the following error:

Your `package.json` file is missing a `build` property inside the `script` property.

Please sign in to comment.