jsxProperty.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import {isValidElement} from 'react';
  2. interface Props {
  3. name: string;
  4. value: unknown;
  5. }
  6. export default function JSXProperty({name, value}: Props) {
  7. if (name === 'children') {
  8. return <code data-property="children">{`{children}`}</code>;
  9. }
  10. if (value === null || value === undefined) {
  11. return <code data-property="nullish">{`${name}={${value}}`}</code>;
  12. }
  13. if (value === true) {
  14. return <code data-property="boolean">{name}</code>;
  15. }
  16. if (value === false) {
  17. return <code data-property="boolean">{`${name}={${value}}`}</code>;
  18. }
  19. if (value === Number || value === Boolean || value === Function) {
  20. // @ts-expect-error
  21. return <code data-property="constructor">{`${name}={${value.name}}`}</code>;
  22. }
  23. if (typeof value === 'string') {
  24. return <code data-property="string">{`${name}=${JSON.stringify(value)}`}</code>;
  25. }
  26. if (typeof value === 'number') {
  27. return <code data-property="number">{`${name}={${value}}`}</code>;
  28. }
  29. if (typeof value === 'function') {
  30. return (
  31. <code data-property="function">{`${name}={${value.name || 'Function'}}`}</code>
  32. );
  33. }
  34. if (isValidElement(value)) {
  35. return <code data-property="element">{`${name}=${value}`}</code>;
  36. }
  37. return <code data-property="object">{`${name}={${JSON.stringify(value)}}`}</code>;
  38. }