Skip to content

Commit afc7e36

Browse files
stasiukanyaRomanHotsiy
authored andcommitted
feat: Add option for skipping quotes in enums enumSkipQuotes (#968)
* feat: add option for skipping enum quotes * chore: move enumSkipQuotes
1 parent 7219344 commit afc7e36

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/components/Fields/EnumValues.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ import * as React from 'react';
22
import { ExampleValue, FieldLabel } from '../../common-elements/fields';
33

44
import { l } from '../../services/Labels';
5+
import { OptionsContext } from '../OptionsProvider';
56

67
export interface EnumValuesProps {
78
values: string[];
89
type: string;
910
}
1011

1112
export class EnumValues extends React.PureComponent<EnumValuesProps> {
13+
static contextType = OptionsContext;
1214
render() {
1315
const { values, type } = this.props;
16+
const { enumSkipQuotes } = this.context;
1417
if (!values.length) {
1518
return null;
1619
}
@@ -21,11 +24,14 @@ export class EnumValues extends React.PureComponent<EnumValuesProps> {
2124
{type === 'array' ? l('enumArray') : ''}{' '}
2225
{values.length === 1 ? l('enumSingleValue') : l('enum')}:
2326
</FieldLabel>
24-
{values.map((value, idx) => (
25-
<React.Fragment key={idx}>
26-
<ExampleValue>{JSON.stringify(value)}</ExampleValue>{' '}
27-
</React.Fragment>
28-
))}
27+
{values.map((value, idx) => {
28+
const exampleValue = enumSkipQuotes ? value : JSON.stringify(value);
29+
return (
30+
<React.Fragment key={idx}>
31+
<ExampleValue>{exampleValue}</ExampleValue>
32+
</React.Fragment>
33+
);
34+
})}
2935
</div>
3036
);
3137
}

src/components/Fields/FieldDetails.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ import { FieldDetail } from './FieldDetail';
2121
import { Badge } from '../../common-elements/';
2222

2323
import { l } from '../../services/Labels';
24+
import { OptionsContext } from '../OptionsProvider';
2425

2526
export class FieldDetails extends React.PureComponent<FieldProps> {
27+
static contextType = OptionsContext;
2628
render() {
2729
const { showExamples, field, renderDiscriminatorSwitch } = this.props;
30+
const { enumSkipQuotes } = this.context;
2831

2932
const { schema, description, example, deprecated } = field;
3033

@@ -65,7 +68,7 @@ export class FieldDetails extends React.PureComponent<FieldProps> {
6568
<Badge type="warning"> {l('deprecated')} </Badge>
6669
</div>
6770
)}
68-
<FieldDetail label={l('default') + ':'} value={schema.default} />
71+
<FieldDetail raw={enumSkipQuotes} label={l('default') + ':'} value={schema.default} />
6972
{!renderDiscriminatorSwitch && <EnumValues type={schema.type} values={schema.enum} />}{' '}
7073
{exampleField}
7174
{<Extensions extensions={{ ...field.extensions, ...schema.extensions }} />}

src/services/RedocNormalizedOptions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface RedocRawOptions {
2828
allowedMdComponents?: Dict<MDXComponentMeta>;
2929

3030
labels?: LabelsConfigRaw;
31+
enumSkipQuotes?: boolean | string;
3132
}
3233

3334
function argValueToBoolean(val?: string | boolean): boolean {
@@ -125,6 +126,7 @@ export class RedocNormalizedOptions {
125126
onlyRequiredInSamples: boolean;
126127
showExtensions: boolean | string[];
127128
hideSingleRequestSampleTab: boolean;
129+
enumSkipQuotes: boolean;
128130

129131
/* tslint:disable-next-line */
130132
unstable_ignoreMimeParameters: boolean;
@@ -156,6 +158,7 @@ export class RedocNormalizedOptions {
156158
this.onlyRequiredInSamples = argValueToBoolean(raw.onlyRequiredInSamples);
157159
this.showExtensions = RedocNormalizedOptions.normalizeShowExtensions(raw.showExtensions);
158160
this.hideSingleRequestSampleTab = argValueToBoolean(raw.hideSingleRequestSampleTab);
161+
this.enumSkipQuotes = argValueToBoolean(raw.enumSkipQuotes);
159162

160163
this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters);
161164

0 commit comments

Comments
 (0)