material-ui [文档基础设施] Joy UI和MUI系统中的默认值没有文档记录,

mzsu5hc0  于 4个月前  发布在  其他
关注(0)|答案(3)|浏览(39)

重复问题

  • 我搜索了现有的问题

最新版本

  • 我测试了最新版本

重现步骤 🕹

  • joy Stack方向没有默认值:https://mui.com/joy-ui/api/stack/#Stack-prop-direction
  • system Stack方向没有默认值:https://mui.com/system/api/stack/#Stack-prop-direction

问题来自 docs:api 脚本,因为 @default 已经定义,但JSON文件中没有可见的默认值。对于system来说,muiDefaultPropsHandler似乎没有应用,添加到处理程序中会使脚本崩溃:需要进一步调查

当前行为 😯

  • 无响应*

预期行为 🤔

  • 无响应*

上下文 🔦

  • 无响应*

你的环境 🌎

npx @mui/envinfo

Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.
xn1cxnb4

xn1cxnb41#

Material UI也存在相同的问题 - https://mui.com/material-ui/api/stack/#Stack-prop-direction。

hmtdttj4

hmtdttj42#

我们可以执行以下操作:

--- a/packages/api-docs-builder/ApiBuilders/ComponentApiBuilder.ts
+++ b/packages/api-docs-builder/ApiBuilders/ComponentApiBuilder.ts
@@ -9,11 +9,12 @@ import remark from 'remark';
 import remarkVisit from 'unist-util-visit';
 import { Link } from 'mdast';
 import { defaultHandlers, parse as docgenParse, ReactDocgenApi } from 'react-docgen';
+import { parse as parseDoctrine } from 'doctrine';
 import { unstable_generateUtilityClass as generateUtilityClass } from '@mui/utils';
 import { renderMarkdown } from '@mui/markdown';
 import { LANGUAGES } from 'docs/config';
 import { ComponentInfo, writePrettifiedFile } from '../buildApiUtils';
-import muiDefaultPropsHandler from '../utils/defaultPropsHandler';
+import muiDefaultPropsHandler, { getJsdocDefaultValue } from '../utils/defaultPropsHandler';
 import parseTest from '../utils/parseTest';
 import generatePropTypeDescription, { getChained } from '../utils/generatePropTypeDescription';
 import createDescribeableProp, {
@@ -552,7 +553,7 @@ const attachTranslations = (reactApi: ReactApi) => {
   reactApi.translations = translations;
 };

-const attachPropsTable = (reactApi: ReactApi) => {
+const attachPropsTable = (reactApi: ReactApi, isSystemComponent?: boolean) => {
   const propErrors: Array<[propName: string, error: Error]> = [];
   type Pair = [string, ReactApi['propsTable'][string]];
   const componentProps: ReactApi['propsTable'] = _.fromPairs(
@@ -569,7 +570,15 @@ const attachPropsTable = (reactApi: ReactApi) => {
         return [] as any;
       }

-      const defaultValue = propDescriptor.jsdocDefaultValue?.value;
+      let defaultValue = propDescriptor.jsdocDefaultValue?.value;
+
+      if (isSystemComponent && !defaultValue && propDescriptor.description) {
+        defaultValue = getJsdocDefaultValue(
+          parseDoctrine(propDescriptor.description, {
+            sloppy: true,
+          }),
+        )?.value;
+      }

       const {
         signature: signatureType,
@@ -834,7 +843,7 @@ export default async function generateComponentApi(
     reactApi.styles.globalClasses = {};
   }

-  attachPropsTable(reactApi);
+  attachPropsTable(reactApi, isSystemComponent);
   attachTranslations(reactApi);

   // eslint-disable-next-line no-console
diff --git a/packages/api-docs-builder/utils/defaultPropsHandler.ts b/packages/api-docs-builder/utils/defaultPropsHandler
.ts
index 77b8942afa..f7dd3bf9eb 100644
--- a/packages/api-docs-builder/utils/defaultPropsHandler.ts
+++ b/packages/api-docs-builder/utils/defaultPropsHandler.ts
@@ -55,7 +55,7 @@ function getDefaultValue(propertyPath: NodePath, importer: Importer) {
   return null;
 }

-function getJsdocDefaultValue(jsdoc: Annotation): { value: string } | undefined {
+export function getJsdocDefaultValue(jsdoc: Annotation): { value: string } | undefined {
   const defaultTag = jsdoc.tags.find((tag) => tag.title === 'default');
   if (defaultTag === undefined) {
     return undefined;

但是,上述逻辑不会将实现的默认值与JSDoc的默认值标签进行比较,就像对其他组件一样(这就是muiDefaultPropsHandler所做的)。我认为我们无法将muiDefaultPropsHandler应用于系统组件,因为组件的定义方式。我们在AST解析器中检查createStackcreateGrid,它在其定义中没有默认属性值。

3duebb1j

3duebb1j3#

我原以为这只是在@default中添加proptypes,但我现在理解正确了,它只在props以以下形状在代码中定义时添加默认值:

const {
    'aria-label': ariaLabel,
    'aria-valuetext': ariaValuetext,
	/* ..., */
	...other
  } = props;

但是,上述逻辑不会将实现的默认值与JSDoc的默认值标签进行比较,就像对其他组件一样。从我的理解来看,当一些属性只是从一个组件传播到另一个组件时,这已经是一个情况了:
https://github.com/mui/material-ui/blob/master/packages/api-docs-builder/utils/defaultPropsHandler.ts/#L81-L93

相关问题