Skip to content

Commit

Permalink
fix!: Console error when using MutableRef from useRef
Browse files Browse the repository at this point in the history
  • Loading branch information
Cavallando committed Nov 9, 2022
1 parent 0d0a597 commit 6d5a595
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 70 deletions.
3 changes: 1 addition & 2 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ export interface CSVReaderProps {
inputId?: string;
inputName?: string;
inputStyle?: object;
inputRef?: React.LegacyRef<HTMLInputElement>;
label?: string | React.ReactNode;
onError?: (error: Error) => void;
onFileLoaded: (data: Array<any>, fileInfo: IFileInfo, originalFile?: File) => any;
parserOptions?: PapaParse.ParseConfig;
disabled?: boolean;
strict?: boolean;
}
declare const CSVReader: React.FC<CSVReaderProps>;
declare const CSVReader: React.ForwardRefExoticComponent<CSVReaderProps & React.RefAttributes<HTMLInputElement>>;
export default CSVReader;
2 changes: 1 addition & 1 deletion dist/react-csv-reader.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/react-csv-reader.js.map

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,22 @@ test('prop-types error when give RefObject to inputRef', async () => {
console.error = jest.fn()
const inputRef: React.RefObject<HTMLInputElement> = { current: null }
React.createElement(CSVReader, {
inputRef,
ref: inputRef,
onFileLoaded: (data, fileInfo) => console.dir(data, fileInfo),
})
expect(console.error).toHaveBeenCalledTimes(0)
})

test('prop-types in Production do not error when given MutableRef to inputRef', async () => {
// @ts-ignore
const ProductionCSVReader = (await import('../dist/react-csv-reader')).default as typeof CSVReader

console.error = jest.fn()
const inputRef: React.MutableRefObject<HTMLInputElement> = { current: null }
React.createElement(ProductionCSVReader, {
ref: inputRef,
onFileLoaded: (data, fileInfo) => console.dir(data, fileInfo),
})

expect(console.error).toHaveBeenCalledTimes(0)
})
132 changes: 67 additions & 65 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface CSVReaderProps {
inputId?: string
inputName?: string
inputStyle?: object
inputRef?: React.LegacyRef<HTMLInputElement>
label?: string | React.ReactNode
onError?: (error: Error) => void
onFileLoaded: (data: Array<any>, fileInfo: IFileInfo, originalFile?: File) => any
Expand All @@ -26,75 +25,79 @@ export interface CSVReaderProps {
strict?: boolean
}

const CSVReader: React.FC<CSVReaderProps> = ({
accept = '.csv, text/csv',
cssClass = 'csv-reader-input',
cssInputClass = 'csv-input',
cssLabelClass = 'csv-label',
fileEncoding = 'UTF-8',
inputId = 'react-csv-reader-input',
inputName = 'react-csv-reader-input',
inputStyle = {},
inputRef,
label,
onError = () => {},
onFileLoaded,
parserOptions = {} as PapaParse.ParseConfig,
disabled = false,
strict = false,
}) => {
const handleChangeFile = (e: React.ChangeEvent<HTMLInputElement>) => {
let reader: FileReader = new FileReader()
const files: FileList = e.target.files!
const CSVReader = React.forwardRef<HTMLInputElement, CSVReaderProps>(
(
{
accept = '.csv, text/csv',
cssClass = 'csv-reader-input',
cssInputClass = 'csv-input',
cssLabelClass = 'csv-label',
fileEncoding = 'UTF-8',
inputId = 'react-csv-reader-input',
inputName = 'react-csv-reader-input',
inputStyle = {},
label,
onError = () => {},
onFileLoaded,
parserOptions = {} as PapaParse.ParseConfig,
disabled = false,
strict = false,
},
inputRef,
) => {
const handleChangeFile = (e: React.ChangeEvent<HTMLInputElement>) => {
let reader: FileReader = new FileReader()
const files: FileList = e.target.files!

if (files.length > 0) {
const fileInfo: IFileInfo = {
name: files[0].name,
size: files[0].size,
type: files[0].type,
}
if (files.length > 0) {
const fileInfo: IFileInfo = {
name: files[0].name,
size: files[0].size,
type: files[0].type,
}

if (strict && accept.indexOf(fileInfo.type) <= 0) {
onError(new Error(`[strict mode] Accept type not respected: got '${fileInfo.type}' but not in '${accept}'`))
return
}
if (strict && accept.indexOf(fileInfo.type) <= 0) {
onError(new Error(`[strict mode] Accept type not respected: got '${fileInfo.type}' but not in '${accept}'`))
return
}

reader.onload = (_event: Event) => {
const csvData = PapaParse.parse(
reader.result as string,
Object.assign(parserOptions, {
error: onError,
encoding: fileEncoding,
}),
)
onFileLoaded(csvData?.data ?? [], fileInfo, files[0])
}
reader.onload = (_event: Event) => {
const csvData = PapaParse.parse(
reader.result as string,
Object.assign(parserOptions, {
error: onError,
encoding: fileEncoding,
}),
)
onFileLoaded(csvData?.data ?? [], fileInfo, files[0])
}

reader.readAsText(files[0], fileEncoding)
reader.readAsText(files[0], fileEncoding)
}
}
}

return (
<div className={cssClass}>
{label && (
<label className={cssLabelClass} htmlFor={inputId}>
{label}
</label>
)}
<input
className={cssInputClass}
type="file"
id={inputId}
name={inputName}
style={inputStyle}
accept={accept}
onChange={handleChangeFile}
disabled={disabled}
ref={inputRef}
/>
</div>
)
}
return (
<div className={cssClass}>
{label && (
<label className={cssLabelClass} htmlFor={inputId}>
{label}
</label>
)}
<input
className={cssInputClass}
type="file"
id={inputId}
name={inputName}
style={inputStyle}
accept={accept}
onChange={handleChangeFile}
disabled={disabled}
ref={inputRef}
/>
</div>
)
},
)

CSVReader.propTypes = {
accept: PropTypes.string,
Expand All @@ -105,7 +108,6 @@ CSVReader.propTypes = {
inputId: PropTypes.string,
inputName: PropTypes.string,
inputStyle: PropTypes.object,
inputRef: PropTypes.oneOfType([PropTypes.func, PropTypes.exact({ current: PropTypes.instanceOf(HTMLInputElement) })]),
label: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
onError: PropTypes.func,
onFileLoaded: PropTypes.func.isRequired,
Expand Down

0 comments on commit 6d5a595

Please sign in to comment.