CodeDocBlock.jsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import React from "react";
  2. import Link from "@docusaurus/Link";
  3. // import clsx from "clsx";
  4. import styles from "../src/pages/CodeDocBlock.module.css";
  5. export function DocBlock({ children }) {
  6. return <section className={styles.DocBlock}>{children}</section>;
  7. }
  8. export function DocSideBySide({ children }) {
  9. return <section className={styles.DocSideBySide}>{children}</section>;
  10. }
  11. export function CodeParams({ children }) {
  12. return <section className={styles.CodeParams}>{children}</section>;
  13. }
  14. export function CodeSnippets({ children }) {
  15. return (
  16. <section className={styles.CodeSnippets}>
  17. {/* <p className={styles.Heading}>Code Sample:</p> */}
  18. {children}
  19. </section>
  20. );
  21. }
  22. /*
  23. Display a single Parameter
  24. */
  25. export function Parameter(props) {
  26. const {
  27. name = null,
  28. type = null,
  29. required = null,
  30. optional = null,
  31. children,
  32. } = computeHeader(props);
  33. return (
  34. <section className={styles.Parameter}>
  35. <p className={styles.ParameterHeader}>
  36. {name && name} {type && type} {required && required}{" "}
  37. {optional && optional}
  38. </p>
  39. {children}
  40. </section>
  41. );
  42. }
  43. /*
  44. Display a single Parameter's field data
  45. */
  46. export function Field(props) {
  47. const {
  48. name = null,
  49. type = null,
  50. values = null,
  51. required = null,
  52. defaultValue = null,
  53. optional = null,
  54. children,
  55. } = computeHeader(props);
  56. return (
  57. <section className={styles.Field}>
  58. <p className={styles.ParameterHeader}>
  59. {name && name} {type && type} {required && required}{" "}
  60. {optional && optional}
  61. {defaultValue && defaultValue}
  62. </p>
  63. <section>
  64. {values && values}
  65. {children}
  66. </section>
  67. </section>
  68. );
  69. }
  70. /*
  71. Parse an array of string values to display
  72. */
  73. export function Values({ values = null }) {
  74. // format the Parameter's values
  75. if (values && Array.isArray(values) && values?.length) {
  76. values = values.map((value) => (
  77. <code style={{ marginRight: ".5em" }} key={value}>
  78. {value}
  79. </code>
  80. ));
  81. }
  82. return (
  83. <p style={{}}>
  84. <span className={styles.SubHeading}>Values:</span>&nbsp;{values}
  85. </p>
  86. );
  87. }
  88. /*
  89. Compute the formatted Parameter and Field component's header meta data
  90. */
  91. function computeHeader({
  92. name = null,
  93. type = null,
  94. href = null,
  95. values = null,
  96. required = null,
  97. defaultValue = null,
  98. optional = null,
  99. children,
  100. }) {
  101. // format the Parameter's name
  102. if (name) {
  103. name = <span className={styles.ParameterName}>{name}</span>;
  104. if (href) name = <Link href={href}>{name}</Link>;
  105. }
  106. // format the Parameter's type
  107. if (type) type = <code>{type}</code>;
  108. // format the Parameter's values
  109. if (values && Array.isArray(values)) {
  110. values = values.map((value) => (
  111. <code style={{ marginRight: ".5em" }}>{value}</code>
  112. ));
  113. }
  114. // format the `defaultValue` flag
  115. if (defaultValue) {
  116. defaultValue = (
  117. <span className={styles.FlagItem}>
  118. Default: <code>{defaultValue.toString()}</code>
  119. </span>
  120. );
  121. }
  122. // format the `required` flag
  123. if (required) {
  124. required = <span className={styles.FlagItem}>required</span>;
  125. }
  126. // format the `optional` flag
  127. else if (optional) {
  128. optional = <span className={styles.FlagItem}>optional</span>;
  129. }
  130. return {
  131. name,
  132. type,
  133. href,
  134. values,
  135. required,
  136. defaultValue,
  137. optional,
  138. children,
  139. };
  140. }