import React, { useState } from 'react'; import CodeSnippet from './CodeSnippet'; import { jsonStringify } from '../../util'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faCheck, faTimes } from '@fortawesome/free-solid-svg-icons'; type Props = { className?: string; style?: React.CSSProperties; children?: React.ReactNode | Array; }; export default function DefinitionList({ children, className = '', ...props }: Props) { if (!children) { return null; } return (
{children}
); } DefinitionList.Row = DefinitionListRow; type Value = string | React.ReactNode | Array | Object | boolean | number; type DefinitionListRowProps = { value?: Value; label?: string | React.ReactNode; className?: string; stacked?: boolean; type?: string; small?: boolean; }; function DefinitionListRow({ value = '', label = '', className = '', stacked = false, type, small = false }: DefinitionListRowProps) { let valueOutput = value; const [expandLabel, setExpandLabel] = useState(false); let timeout: NodeJS.Timeout; function startExpandLabel() { timeout = setTimeout(() => setExpandLabel(true), 500); } function stopExpandLabel() { clearTimeout(timeout); setExpandLabel(false); } if (React.isValidElement(value)) { valueOutput = value; } else if (typeof value === 'boolean') { valueOutput = ( {value ? 'true' : 'false'} ); } else if (type === 'string' || typeof value === 'object') { valueOutput = ; } else if (typeof value === 'string') { valueOutput = ; } else if (typeof value === 'number') { valueOutput = ; } const expandedLabelClass = 'flex-grow truncate min-w-[8rem] max-w-max'; const expandedSmallLabelClass = 'flex-grow truncate min-w-[2rem] max-w-max'; const normalLabelClass = 'flex-none truncate w-[8rem]'; const normalSmallLabelClass = 'flex-none truncate w-[2rem]'; return (
{ startExpandLabel(); }} onMouseOut={() => { stopExpandLabel(); }} > {label}
{valueOutput as React.ReactNode}
); }