Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing props in "table" type definitions #713

Closed
4 tasks done
lucasassisrosa opened this issue Nov 30, 2022 · 6 comments
Closed
4 tasks done

Missing props in "table" type definitions #713

lucasassisrosa opened this issue Nov 30, 2022 · 6 comments
Labels
☂️ area/types This affects typings 💪 phase/solved Post is done

Comments

@lucasassisrosa
Copy link
Contributor

lucasassisrosa commented Nov 30, 2022

Initial checklist

Affected packages and versions

8.0.3

Link to runnable example

No response

Steps to reproduce

Check https://github.com/remarkjs/react-markdown/blob/main/lib/ast-to-react.js#L61 and try to match a td or th component with ThHTMLAttributes or TdHTMLAttributes from react

Expected behavior

as-to-react.js should have

 * @typedef {ComponentPropsWithoutRef<'th'> & ReactMarkdownProps & {style?: Record<string, unknown>, isHeader: true}} TableHeaderCellProps
 * @typedef {ComponentPropsWithoutRef<'td'> & ReactMarkdownProps & {style?: Record<string, unknown>, isHeader: false}} TableDataCellProps
 * @typedef {ComponentType<TableHeaderCellProps>} TableHeaderCellComponent
 * @typedef {ComponentType<TableDataCellProps>} TableDataCellProps
 * @property {TableHeaderCellComponent|ReactMarkdownNames} th
 * @property {TableDataCellComponent|ReactMarkdownNames} td

Actual behavior

as-to-react.js has

 * @typedef {ComponentPropsWithoutRef<'table'> & ReactMarkdownProps & {style?: Record<string, unknown>, isHeader: boolean}} TableCellProps
 * @typedef {ComponentType<TableCellProps>} TableCellComponent
 * @property {TableCellComponent|ReactMarkdownNames} td
 * @property {TableCellComponent|ReactMarkdownNames} th

Runtime

Node v16

Package manager

yarn 1

OS

macOS

Build and bundle tools

Next.js

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Nov 30, 2022
@ChristianMurphy
Copy link
Member

This may be a good add, but it's hard to tell because it is currently framed as an XY Problem.
What is the actual issue you are running into? Could you share an example?

@ChristianMurphy ChristianMurphy added the 🙉 open/needs-info This needs some more info label Nov 30, 2022
@github-actions

This comment has been minimized.

@lucasassisrosa
Copy link
Contributor Author

lucasassisrosa commented Nov 30, 2022

This may be a good add, but it's hard to tell because it is currently framed as an XY Problem. What is the actual issue you are running into? Could you share an example?

Yes I can share. I have custom TableHeaderCell and TableDataCell elements that look like this:

export type TableHeaderCellProps = ThHTMLAttributes<HTMLTableCellElement>;
const HeaderCell = ({ children, ...props }: TableHeaderCellProps) => {
  return (
    <th {...props}>
      {children}
    </th>
  );
};

export type TableDataCellProps = TdHTMLAttributes<HTMLTableCellElement>;
const DataCell = ({ children, ...props }: TableDataCellProps) => {
  return (
    <td {...props}>
      {children}
    </td>
  );
};

When I add custom components to <ReactMarkdown />:

import ReactMarkdown from 'react-markdown';
import type { ReactMarkdownOptions } from 'react-markdown/lib/react-markdown';

type Components = ReactMarkdownOptions['components'];

const Markdown = ({ children }) => {
  const initialComponents: Components = {
    th: Table.HeaderCell,
    td: Table.DataCell,
  };
  
  return (
    <ReactMarkdown
      components={{ ...initialComponents, ...components }}
    >
      {children}
    </ReactMarkdown>
  );
};

I receive the following type error:

      Type 'TableCellProps' is not assignable to type 'TableHeaderCellProps'.
        Types of property 'onCopy' are incompatible.
          Type 'ClipboardEventHandler<HTMLTableElement> | undefined' is not assignable to type 'ClipboardEventHandler<HTMLTableCellElement> | undefined'.
            Type 'ClipboardEventHandler<HTMLTableElement>' is not assignable to type 'ClipboardEventHandler<HTMLTableCellElement>'.
              Type 'HTMLTableElement' is missing the following properties from type 'HTMLTableCellElement': abbr, axis, cellIndex, ch, and 8 more.ts(2322)

@ChristianMurphy
Copy link
Member

Thanks!

I'm not quite following where ThHTMLAttributes and TdHTMLAttributes are coming from.

Reading between the lines a bit, it sounds like you want to reflect react's html element props exactly for the prop type?
Something like:

import React, { DetailedHTMLProps, HTMLAttributes } from "react";
import Markdown from "react-markdown";

const markdownSource = `
# heading

* list
* items

\`\`\`js
function () {}
\`\`\`
`;

const HeaderCell = ({
  children,
  ...props
}: DetailedHTMLProps<
  HTMLAttributes<HTMLTableHeaderCellElement>,
  HTMLTableHeaderCellElement
>) => {
  return <th {...props}>{children}</th>;
};

const DataCell = ({
  children,
  ...props
}: DetailedHTMLProps<
  HTMLAttributes<HTMLTableDataCellElement>,
  HTMLTableDataCellElement
>) => {
  return <td {...props}>{children}</td>;
};

const App = () => (
  <Markdown components={{ th: HeaderCell, td: DataCell }}>
    {markdownSource}
  </Markdown>
);

export default App;

runnable example in a sandbox: https://codesandbox.io/s/trusting-hamilton-wnnqfh?file=/src/app.tsx

Is that accurate to your end goal?

@lucasassisrosa
Copy link
Contributor Author

Thanks!

I'm not quite following where ThHTMLAttributes and TdHTMLAttributes are coming from.

Reading between the lines a bit, it sounds like you want to reflect react's html element props exactly for the prop type? Something like:

import React, { DetailedHTMLProps, HTMLAttributes } from "react";
import Markdown from "react-markdown";

const markdownSource = `
# heading

* list
* items

\`\`\`js
function () {}
\`\`\`
`;

const HeaderCell = ({
  children,
  ...props
}: DetailedHTMLProps<
  HTMLAttributes<HTMLTableHeaderCellElement>,
  HTMLTableHeaderCellElement
>) => {
  return <th {...props}>{children}</th>;
};

const DataCell = ({
  children,
  ...props
}: DetailedHTMLProps<
  HTMLAttributes<HTMLTableDataCellElement>,
  HTMLTableDataCellElement
>) => {
  return <td {...props}>{children}</td>;
};

const App = () => (
  <Markdown components={{ th: HeaderCell, td: DataCell }}>
    {markdownSource}
  </Markdown>
);

export default App;

runnable example in a sandbox: https://codesandbox.io/s/trusting-hamilton-wnnqfh?file=/src/app.tsx

Is that accurate to your end goal?

Yes exactly. ThHTMLAttributes and TdHTMLAttributes come from react

@wooorm wooorm closed this as completed in 9b20440 Dec 1, 2022
@github-actions

This comment has been minimized.

@wooorm wooorm added the 💪 phase/solved Post is done label Dec 1, 2022
@github-actions github-actions bot removed 🤞 phase/open Post is being triaged manually 🙉 open/needs-info This needs some more info labels Dec 1, 2022
@ChristianMurphy ChristianMurphy added the ☂️ area/types This affects typings label Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
☂️ area/types This affects typings 💪 phase/solved Post is done
Development

No branches or pull requests

3 participants