-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: parameter validations * chore: review suggestions * chore: cleanup and separete Parameter component * test: update tests
- Loading branch information
Jakub Jankowski
authored
Dec 22, 2020
1 parent
58b4c32
commit fec2d79
Showing
6 changed files
with
238 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { safeStringify } from '@stoplight/json'; | ||
import { Flex, Input, Select, Text } from '@stoplight/mosaic'; | ||
import { IHttpParam, INodeExample, INodeExternalExample } from '@stoplight/types'; | ||
import { isObject, map } from 'lodash'; | ||
import * as React from 'react'; | ||
|
||
interface ParameterProps { | ||
parameter: IHttpParam; | ||
value: string; | ||
onChange: (e: React.FormEvent<HTMLSelectElement> | React.ChangeEvent<HTMLInputElement>) => void; | ||
} | ||
|
||
const booleanOptions = [ | ||
{ label: 'Not Set', value: '' }, | ||
{ label: 'False', value: 'false' }, | ||
{ label: 'True', value: 'true' }, | ||
]; | ||
|
||
const selectExampleOption = { value: '', label: 'Pick an example' }; | ||
|
||
export const Parameter: React.FC<ParameterProps> = ({ parameter, value, onChange }) => { | ||
const parameterValueOptions = parameterOptions(parameter); | ||
const examples = exampleOptions(parameter); | ||
const selectedExample = examples?.find(e => e.value === value) ?? selectExampleOption; | ||
return ( | ||
<Flex align="center" key={parameter.name}> | ||
<Input appearance="minimal" readOnly value={parameter.name} /> | ||
<Text mx={3}>:</Text> | ||
{parameterValueOptions ? ( | ||
<Select | ||
flexGrow | ||
aria-label={parameter.name} | ||
options={parameterValueOptions} | ||
value={value} | ||
onChange={onChange} | ||
/> | ||
) : ( | ||
<Flex flexGrow> | ||
<Input | ||
style={{ paddingLeft: 15 }} | ||
aria-label={parameter.name} | ||
appearance="minimal" | ||
flexGrow | ||
placeholder={getPlaceholderForParameter(parameter)} | ||
type={parameter.schema?.type === 'number' ? 'number' : 'text'} | ||
required | ||
value={value ?? ''} | ||
onChange={onChange} | ||
/> | ||
{examples && ( | ||
<Select | ||
aria-label={`${parameter.name}-select`} | ||
flexGrow | ||
value={selectedExample.value} | ||
options={examples} | ||
onChange={onChange} | ||
/> | ||
)} | ||
</Flex> | ||
)} | ||
</Flex> | ||
); | ||
}; | ||
|
||
function parameterOptions(parameter: IHttpParam) { | ||
return parameter.schema?.type === 'boolean' | ||
? booleanOptions | ||
: parameter.schema?.enum !== undefined | ||
? map(parameter.schema.enum, v => (Number.isNaN(Number(v)) ? String(v) : Number(v))) | ||
: null; | ||
} | ||
|
||
function exampleOptions(parameter: IHttpParam) { | ||
return parameter.examples?.length && parameter.examples.length > 1 | ||
? [ | ||
selectExampleOption, | ||
...parameter.examples.map(example => ({ label: example.key, value: exampleValue(example) })), | ||
] | ||
: null; | ||
} | ||
|
||
export function exampleValue(example: INodeExample | INodeExternalExample) { | ||
return 'value' in example ? example.value : example.externalValue; | ||
} | ||
|
||
function getPlaceholderForParameter(parameter: IHttpParam) { | ||
const defaultOrType = getDefaultForParameter(parameter) ?? parameter.schema?.type; | ||
return defaultOrType !== undefined ? String(defaultOrType) : undefined; | ||
} | ||
|
||
function getDefaultForParameter(parameter: IHttpParam) { | ||
const defaultValue = parameter.schema?.default; | ||
return isObject(defaultValue) ? safeStringify(defaultValue) : defaultValue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/elements/src/components/TryIt/__tests__/parameters.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { httpOperation } from '../../../__fixtures__/operations/put-todos'; | ||
import { initialParameterValues } from '../OperationParameters'; | ||
|
||
describe('Parameters', () => { | ||
it('should fill initial parameters', () => { | ||
const operationParameters = { | ||
path: httpOperation.request?.path, | ||
query: httpOperation.request?.query, | ||
headers: httpOperation.request?.headers, | ||
}; | ||
|
||
const parameters = initialParameterValues(operationParameters); | ||
|
||
expect(parameters).toMatchObject({ | ||
limit: '0', | ||
type: 'something', | ||
value: '0', | ||
'account-id': 'example id', | ||
'message-id': 'example value', | ||
}); | ||
}); | ||
}); |