microsoft/typespec
Publicmirrored from https://github.com/microsoft/typespecAvailable
packages/html-program-viewer/src/type-ui-base.tsx
59lines · modecode
| 1 | import { css } from "@emotion/react"; |
| 2 | import { Type } from "@typespec/compiler"; |
| 3 | import { FunctionComponent } from "react"; |
| 4 | import { KeyValueSection } from "./common.js"; |
| 5 | import { Colors } from "./constants.js"; |
| 6 | import { getIdForType } from "./utils.js"; |
| 7 | |
| 8 | export interface TypeUIBaseProperty { |
| 9 | name: string; |
| 10 | value: any; |
| 11 | description?: string; |
| 12 | } |
| 13 | |
| 14 | export interface TypeUIBaseProps { |
| 15 | type: Type; |
| 16 | name: string; |
| 17 | /** |
| 18 | * Alternate id |
| 19 | * @default getIdForType(type) |
| 20 | */ |
| 21 | id?: string; |
| 22 | properties: TypeUIBaseProperty[]; |
| 23 | } |
| 24 | |
| 25 | const TypeNameStyles = css({ |
| 26 | display: "inline", |
| 27 | color: "#333333", |
| 28 | }); |
| 29 | |
| 30 | export const TypeUIBase: FunctionComponent<TypeUIBaseProps> = (props) => { |
| 31 | const id = props.id ?? getIdForType(props.type); |
| 32 | const properties = props.properties.map((prop) => { |
| 33 | return ( |
| 34 | <li key={prop.name}> |
| 35 | <span css={{ color: "#9c5d27" }} title={prop.description}> |
| 36 | {prop.name} |
| 37 | </span> |
| 38 | : <span>{prop.value}</span> |
| 39 | </li> |
| 40 | ); |
| 41 | }); |
| 42 | return ( |
| 43 | <div> |
| 44 | <div id={id}> |
| 45 | <span |
| 46 | css={{ |
| 47 | display: "inline", |
| 48 | color: Colors.typeKind, |
| 49 | marginRight: "5px", |
| 50 | }} |
| 51 | > |
| 52 | {props.type.kind} |
| 53 | </span> |
| 54 | <span css={TypeNameStyles}>{props.name}</span> |
| 55 | </div> |
| 56 | <KeyValueSection>{properties}</KeyValueSection> |
| 57 | </div> |
| 58 | ); |
| 59 | }; |
| 60 | |