From 783b715a7fd4e01b71a10f90f1518d94d01861c4 Mon Sep 17 00:00:00 2001 From: Charis <26616127+charislam@users.noreply.github.com> Date: Wed, 5 Feb 2025 09:37:15 -0500 Subject: [PATCH] fix(ref docs): make unions more intuitive (#33332) Change the description for unions to make it clearer what is happening + auto-expand them --- apps/docs/features/docs/Reference.ui.tsx | 37 +++++++++++++++++++----- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/apps/docs/features/docs/Reference.ui.tsx b/apps/docs/features/docs/Reference.ui.tsx index 2b0c29817cc..ed99b71339b 100644 --- a/apps/docs/features/docs/Reference.ui.tsx +++ b/apps/docs/features/docs/Reference.ui.tsx @@ -204,6 +204,8 @@ function ParamOrTypeDetails({ paramOrType }: { paramOrType: object }) { ? getSubDetails(paramOrType) : undefined + const defaultOpen = isDefaultExpanded(paramOrType) + return ( <>
@@ -228,7 +230,9 @@ function ParamOrTypeDetails({ paramOrType }: { paramOrType: object }) {
)} - {subContent && subContent.length > 0 && } + {subContent && subContent.length > 0 && ( + + )} ) } @@ -240,6 +244,7 @@ export function ReturnTypeDetails({ returnType }: { returnType: MethodTypes['ret if (isNameOnlyType) return const subContent = getSubDetails(returnType) + const isDefaultOpen = isDefaultExpanded(returnType) return (
@@ -254,7 +259,9 @@ export function ReturnTypeDetails({ returnType }: { returnType: MethodTypes['ret />
)} - {subContent && subContent.length > 0 && } + {subContent && subContent.length > 0 && ( + + )} ) @@ -263,12 +270,14 @@ export function ReturnTypeDetails({ returnType }: { returnType: MethodTypes['ret function TypeSubDetails({ details, className, + defaultOpen = false, }: { details: Array | Array | Array className?: string + defaultOpen?: boolean }) { return ( - + ` case 'union': - return 'Union: expand to see options' + return 'One of the following options' case 'index signature': // Needs an extra level of wrapping to fake the wrapping parameter // @ts-ignore @@ -667,7 +676,7 @@ function getSubDetails(parentType: MethodTypes['params'][number] | MethodTypes[' break case 'union': subDetails = parentType.type.subTypes.map((subType, index) => ({ - name: `union option ${index + 1}`, + name: `Option ${index + 1}`, type: { ...subType }, isOptional: 'NA', })) @@ -675,7 +684,7 @@ function getSubDetails(parentType: MethodTypes['params'][number] | MethodTypes[' case 'promise': if (parentType.type.awaited.type === 'union') { subDetails = parentType.type.awaited.subTypes.map((subType, index) => ({ - name: `union option ${index + 1}`, + name: `Option ${index + 1}`, type: { ...subType }, isOptional: 'NA', })) @@ -693,7 +702,7 @@ function getSubDetails(parentType: MethodTypes['params'][number] | MethodTypes[' case 'array': if (parentType.type.elemType?.type === 'union') { subDetails = parentType.type.elemType.subTypes.map((subType, index) => ({ - name: `union option ${index + 1}`, + name: `Option ${index + 1}`, type: { ...subType }, isOptional: 'NA', })) @@ -871,3 +880,17 @@ function applyParameterMergeStrategy( } } } + +function isDefaultExpanded(meta: object) { + return ( + 'type' in meta && + typeof meta.type === 'object' && + 'type' in meta.type && + (meta.type.type == 'union' || + (meta.type.type === 'promise' && + 'awaited' in meta.type && + typeof meta.type.awaited === 'object' && + 'type' in meta.type.awaited && + meta.type.awaited.type === 'union')) + ) +}