Enhanced table with Typescript #1989
-
Hi there 👋 I'm trying to do an MUI enhanced table following this example https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/material-UI-enhanced-table except my project is in typescript using Eslint. I encountered many errors due to that.
Error:
Output:
Output Version: Config Code Thanks for your help! |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 12 replies
-
You need to update your @types/react-table package to 7.0.11 as it should have the updated versions of props you need. Also, it looks like you need to play around with your typing for the Ref on the Indeterminate Checkbox. Let me see if I can whip up a quick CodeSandbox with what I'm using for a typed Indeterminate Checkbox. |
Beta Was this translation helpful? Give feedback.
-
Actually, I'll just toss the code in here: import React, { useEffect, forwardRef } from 'react';
interface Props {
indeterminate?: boolean;
name: string;
}
const useCombinedRefs = (...refs): React.MutableRefObject<any> => {
const targetRef = React.useRef();
React.useEffect(() => {
refs.forEach(ref => {
if (!ref) return;
if (typeof ref === 'function') {
ref(targetRef.current);
} else {
ref.current = targetRef.current;
}
});
}, [refs]);
return targetRef;
};
const IndeterminateCheckbox = forwardRef<HTMLInputElement, Props>(
({ indeterminate, ...rest }, ref: React.Ref<HTMLInputElement>) => {
const defaultRef = React.useRef(null);
const combinedRef = useCombinedRefs(ref, defaultRef);
useEffect(() => {
if (combinedRef?.current) {
combinedRef.current.indeterminate = indeterminate ?? false;
}
}, [combinedRef, indeterminate]);
return (
<React.Fragment>
<input type="checkbox" ref={combinedRef} {...rest} />
</React.Fragment>
);
}
);
export default IndeterminateCheckbox; That's what I'm using in our current codebase and it works as expected. Note that I am using nullish coalescing, so you'll need to be on at least TS 3.7. |
Beta Was this translation helpful? Give feedback.
-
Work like a charm thank a lot ! |
Beta Was this translation helpful? Give feedback.
-
I'm getting an error: "
|
Beta Was this translation helpful? Give feedback.
-
The problem is |
Beta Was this translation helpful? Give feedback.
-
Hello,
}, [resolvedRef, indeterminate]);
} but my checkbox aren't working(no check). can someone help? |
Beta Was this translation helpful? Give feedback.
-
@riceboyler @tannerlinsley
); const IndeterminateCheckbox = React.forwardRef<HTMLInputElement, Props>(({ indeterminate, ...rest }, ref) => { React.useEffect(() => { // eslint-disable-next-line no-console return ( |
Beta Was this translation helpful? Give feedback.
-
The following code below works like a charm. import React, { useRef, forwardRef, useEffect } from 'react'; interface IIndeterminateInputProps { const CheckBox = forwardRef<HTMLInputElement, IIndeterminateInputProps>(
} CheckBox.displayName = 'CheckBox'; |
Beta Was this translation helpful? Give feedback.
Actually, I'll just toss the code in here: