Skip to content

Commit ea2654d

Browse files
2 parents c7a4ab9 + 7bc8e5d commit ea2654d

File tree

5 files changed

+75
-8
lines changed

5 files changed

+75
-8
lines changed

.github/workflows/generator-version-guard.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
with:
1818
fetch-depth: 2
1919

20-
- name: Run generator version guard
21-
uses: DevExpress/github-actions/generator-version-guard@main
20+
- name: Run check for version update
21+
uses: DevExpress/github-actions/check-for-version-update@main
2222
with:
23-
generator_path: packages/devextreme-react-generator
23+
path: packages/devextreme-react-generator

packages/devextreme-react-generator/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"author": "Developer Express Inc.",
33
"name": "devextreme-react-generator",
4-
"version": "1.3.1",
4+
"version": "1.3.2",
55
"description": "DevExtreme React UI and Visualization Components",
66
"repository": {
77
"type": "git",

packages/devextreme-react-generator/src/component-generator.test.ts

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,61 @@ export {
10041004
).toBe(EXPECTED);
10051005
});
10061006

1007+
it('adds check for acceptable values with common type', () => {
1008+
// #region EXPECTED
1009+
const EXPECTED = `
1010+
import dxCLASS_NAME, {
1011+
Properties
1012+
} from "DX/WIDGET/PATH";
1013+
1014+
import * as PropTypes from "prop-types";
1015+
import { Component as BaseComponent, IHtmlOptions } from "BASE_COMPONENT_PATH";
1016+
1017+
type ICLASS_NAMEOptions = React.PropsWithChildren<Properties & IHtmlOptions & {
1018+
}>
1019+
1020+
class CLASS_NAME extends BaseComponent<React.PropsWithChildren<ICLASS_NAMEOptions>> {
1021+
1022+
public get instance(): dxCLASS_NAME {
1023+
return this._instance;
1024+
}
1025+
1026+
protected _WidgetClass = dxCLASS_NAME;
1027+
}
1028+
(CLASS_NAME as any).propTypes = {
1029+
PROP1: PropTypes.oneOfType([
1030+
PropTypes.string,
1031+
PropTypes.oneOf([
1032+
"VALUE_1",
1033+
"VALUE_2"])
1034+
])
1035+
};
1036+
export default CLASS_NAME;
1037+
export {
1038+
CLASS_NAME,
1039+
ICLASS_NAMEOptions
1040+
};
1041+
`.trimLeft();
1042+
// #endregion
1043+
1044+
expect(
1045+
generate({
1046+
name: 'CLASS_NAME',
1047+
baseComponentPath: 'BASE_COMPONENT_PATH',
1048+
extensionComponentPath: 'EXTENSION_COMPONENT_PATH',
1049+
dxExportPath: 'DX/WIDGET/PATH',
1050+
propTypings: [
1051+
{
1052+
propName: 'PROP1',
1053+
types: [],
1054+
acceptableType: 'string',
1055+
acceptableValues: ['"VALUE_1"', '"VALUE_2"'],
1056+
},
1057+
],
1058+
}),
1059+
).toBe(EXPECTED);
1060+
});
1061+
10071062
it('adds check for acceptable values', () => {
10081063
// #region EXPECTED
10091064
const EXPECTED = `
@@ -1028,8 +1083,7 @@ class CLASS_NAME extends BaseComponent<React.PropsWithChildren<ICLASS_NAMEOption
10281083
(CLASS_NAME as any).propTypes = {
10291084
PROP1: PropTypes.oneOf([
10301085
"VALUE_1",
1031-
"VALUE_2"
1032-
])
1086+
"VALUE_2"])
10331087
};
10341088
export default CLASS_NAME;
10351089
export {

packages/devextreme-react-generator/src/component-generator.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { createTempate, L1, L2 } from './template';
1+
import {
2+
createTempate, L1, L2, L3,
3+
} from './template';
24

35
import {
46
createKeyComparator,
@@ -61,6 +63,7 @@ type IOption = {
6163
interface IPropTyping {
6264
propName: string;
6365
types: string[];
66+
acceptableType?: string;
6467
acceptableValues?: string[];
6568
}
6669

@@ -428,10 +431,19 @@ function buildPropsTypeName(className: string) {
428431
return `I${className}Props`;
429432
}
430433

434+
function acceptableTypes(acceptableType: string | undefined, acceptableValues: string[]): string {
435+
const indent = acceptableType ? L3 : L2;
436+
const acceptableTemplate = `PropTypes.oneOf([${indent}${acceptableValues.join(`,${indent}`)}])`;
437+
return acceptableType ? `PropTypes.oneOfType([
438+
PropTypes.${acceptableType},
439+
${acceptableTemplate}
440+
])` : acceptableTemplate;
441+
}
442+
431443
function createPropTypingModel(typing: IPropTyping): IRenderedPropTyping {
432444
const types = typing.types.map((t) => `PropTypes.${t}`);
433445
if (typing.acceptableValues && isNotEmptyArray(typing.acceptableValues)) {
434-
types.push(`PropTypes.oneOf([\n ${typing.acceptableValues.join(',\n ')}\n ])`);
446+
types.push(acceptableTypes(typing.acceptableType, typing.acceptableValues));
435447
}
436448
return {
437449
propName: typing.propName,

packages/devextreme-react-generator/src/generator.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export function createPropTyping(
129129
return {
130130
propName: option.name,
131131
types: types || [],
132+
acceptableType: restrictedTypes[0].type.toLowerCase(),
132133
acceptableValues: restrictedTypes[0].acceptableValues,
133134
};
134135
}

0 commit comments

Comments
 (0)