Skip to content

Commit

Permalink
feat: use example gen for multiple example value retainer examples (#…
Browse files Browse the repository at this point in the history
…6920)

* fix: multiple examples with same value jumps to first example
  • Loading branch information
mathis-m authored Feb 11, 2021
1 parent 649be5d commit fad81f8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
13 changes: 10 additions & 3 deletions src/core/components/examples-select-value-retainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,23 @@ export default class ExamplesSelectValueRetainer extends React.PureComponent {
nextProps
)

const exampleMatchingNewValue = examples.find(
const examplesMatchingNewValue = examples.filter(
(example) =>
example.get("value") === newValue ||
// sometimes data is stored as a string (e.g. in Request Bodies), so
// let's check against a stringified version of our example too
stringify(example.get("value")) === newValue
)

if (exampleMatchingNewValue) {
onSelect(examples.keyOf(exampleMatchingNewValue), {
if (examplesMatchingNewValue.size) {
let key
if(examplesMatchingNewValue.has(nextProps.currentKey))
{
key = nextProps.currentKey
} else {
key = examplesMatchingNewValue.keySeq().first()
}
onSelect(key, {
isSyntheticChange: true,
})
} else if (
Expand Down
21 changes: 16 additions & 5 deletions src/core/plugins/oas3/components/request-body.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,18 @@ const RequestBody = ({

const mediaTypeValue = requestBodyContent.get(contentType, OrderedMap())
const schemaForMediaType = mediaTypeValue.get("schema", OrderedMap())
const examplesForMediaType = mediaTypeValue.get("examples", null)
const rawExamplesOfMediaType = mediaTypeValue.get("examples", null)
const sampleForMediaType = rawExamplesOfMediaType?.map((container, key) => {
const val = container?.get("value", null)
if(val) {
container = container.set("value", getDefaultRequestBodyValue(
requestBody,
contentType,
key,
), val)
}
return container
})

const handleExamplesSelect = (key /*, { isSyntheticChange } */) => {
updateActiveExamplesKey(key)
Expand Down Expand Up @@ -223,10 +234,10 @@ const RequestBody = ({
<Markdown source={requestBodyDescription} />
}
{
examplesForMediaType ? (
sampleForMediaType ? (
<ExamplesSelectValueRetainer
userHasEditedBody={userHasEditedBody}
examples={examplesForMediaType}
examples={sampleForMediaType}
currentKey={activeExamplesKey}
currentUserInputValue={requestBodyValue}
onSelect={handleExamplesSelect}
Expand Down Expand Up @@ -269,9 +280,9 @@ const RequestBody = ({
)
}
{
examplesForMediaType ? (
sampleForMediaType ? (
<Example
example={examplesForMediaType.get(activeExamplesKey)}
example={sampleForMediaType.get(activeExamplesKey)}
getComponent={getComponent}
getConfigs={getConfigs}
/>
Expand Down

0 comments on commit fad81f8

Please sign in to comment.