feat: use example gen for multiple example value retainer examples (#6920)

* fix: multiple examples with same value jumps to first example
This commit is contained in:
Mahtis Michel
2021-02-11 18:33:04 +01:00
committed by GitHub
parent 649be5d78b
commit fad81f8cb9
2 changed files with 26 additions and 8 deletions

View File

@@ -189,7 +189,7 @@ 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
@@ -197,8 +197,15 @@ export default class ExamplesSelectValueRetainer extends React.PureComponent {
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 (

View File

@@ -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)
@@ -223,10 +234,10 @@ const RequestBody = ({
<Markdown source={requestBodyDescription} />
}
{
examplesForMediaType ? (
sampleForMediaType ? (
<ExamplesSelectValueRetainer
userHasEditedBody={userHasEditedBody}
examples={examplesForMediaType}
examples={sampleForMediaType}
currentKey={activeExamplesKey}
currentUserInputValue={requestBodyValue}
onSelect={handleExamplesSelect}
@@ -269,9 +280,9 @@ const RequestBody = ({
)
}
{
examplesForMediaType ? (
sampleForMediaType ? (
<Example
example={examplesForMediaType.get(activeExamplesKey)}
example={sampleForMediaType.get(activeExamplesKey)}
getComponent={getComponent}
getConfigs={getConfigs}
/>