Skip to content

Commit

Permalink
Fix typescript test
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yadav committed Sep 6, 2022
1 parent 605fb39 commit 201ddde
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 302 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"module": "dist/react-number-format.es.js",
"author": "Sudhanshu Yadav",
"license": "MIT",
"types": "typings/number_format.d.ts",
"types": "typings/types.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/s-yadav/react-number-format"
Expand Down
101 changes: 2 additions & 99 deletions src/number_format.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,40 +51,6 @@ class NumberFormat extends React.Component<NumberFormatProps, NumberFormatState>
};
static defaultProps: Object;

// TODO: remove this on v5. This is only for backward compatibility
static propTypes: {
thousandSeparator: any;
decimalSeparator: any;
allowedDecimalSeparators: any;
thousandsGroupStyle: any;
decimalScale: any;
fixedDecimalScale: any;
displayType: any;
prefix: any;
suffix: any;
format: any;
removeFormatting: any;
mask: any;
value: any;
defaultValue: any;
isNumericString: any;
customInput: any;
allowNegative: any;
allowEmptyFormatting: any;
allowLeadingZeros: any;
onValueChange: any;
onKeyDown: any;
onMouseUp: any;
onChange: any;
onFocus: any;
onBlur: any;
type: any;
isAllowed: any;
renderText: any;
getInputRef: any;
customNumerals: (props: any, propName: any, componentName: any) => Error;
};

constructor(props: NumberFormatProps) {
super(props);
const { defaultValue } = props;
Expand Down Expand Up @@ -497,14 +463,8 @@ class NumberFormat extends React.Component<NumberFormatProps, NumberFormatState>
* @return {string} formatted Value
*/
formatAsNumber(numStr: string) {
const {
decimalScale,
fixedDecimalScale,
prefix,
suffix,
allowNegative,
thousandsGroupStyle,
} = this.props;
const { decimalScale, fixedDecimalScale, prefix, suffix, allowNegative, thousandsGroupStyle } =
this.props;
const { thousandSeparator, decimalSeparator } = this.getSeparators();

const hasDecimalSeparator = numStr.indexOf('.') !== -1 || (decimalScale && fixedDecimalScale);
Expand Down Expand Up @@ -1090,61 +1050,4 @@ class NumberFormat extends React.Component<NumberFormatProps, NumberFormatState>

NumberFormat.defaultProps = defaultProps;

if (process.env.NODE_ENV !== 'production') {
const PropTypes = require('prop-types');
NumberFormat.propTypes = {
thousandSeparator: PropTypes.oneOfType([PropTypes.string, PropTypes.oneOf([true])]),
decimalSeparator: PropTypes.string,
allowedDecimalSeparators: PropTypes.arrayOf(PropTypes.string),
thousandsGroupStyle: PropTypes.oneOf(['thousand', 'lakh', 'wan']),
decimalScale: PropTypes.number,
fixedDecimalScale: PropTypes.bool,
displayType: PropTypes.oneOf(['input', 'text']),
prefix: PropTypes.string,
suffix: PropTypes.string,
format: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
removeFormatting: PropTypes.func,
mask: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
defaultValue: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
isNumericString: PropTypes.bool,
customInput: PropTypes.elementType,
allowNegative: PropTypes.bool,
allowEmptyFormatting: PropTypes.bool,
allowLeadingZeros: PropTypes.bool,
onValueChange: PropTypes.func,
onKeyDown: PropTypes.func,
onMouseUp: PropTypes.func,
onChange: PropTypes.func,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
type: PropTypes.oneOf(['text', 'tel', 'password']),
isAllowed: PropTypes.func,
renderText: PropTypes.func,
getInputRef: PropTypes.oneOfType([
PropTypes.func, // for legacy refs
PropTypes.shape({ current: PropTypes.any }),
]),
customNumerals: (props, propName, componentName) => {
if (!props[propName]) {
return;
}
const arrayPropLength = props[propName].length;
const hasSingleCharString = props[propName].every(
(item) => typeof item === 'string' && item.length === 1,
);
if (arrayPropLength !== 10) {
return new Error(
`Invalid array length ${arrayPropLength} (expected ${10}) for prop ${propName} supplied to ${componentName}. Validation failed.`,
);
}
if (!hasSingleCharString) {
return new Error(
`Invalid element type for prop ${propName} supplied to ${componentName}. all provided elements in the array must be string. Validation failed.`,
);
}
},
};
}

export default NumberFormat;
27 changes: 9 additions & 18 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React from "react";
import { SyntheticEvent } from "react";
import React from 'react';
import { SyntheticEvent } from 'react';


export interface NumberFormatState {
value?: string;
numAsString?: string;
mounted: boolean
mounted: boolean;
}

export interface NumberFormatValues {
Expand Down Expand Up @@ -59,23 +58,15 @@ export type NumberFormatPropsBase<T> = {
renderText?: (formattedValue: string, otherProps: Partial<NumberFormatProps>) => React.ReactNode;
getInputRef?: ((el: HTMLInputElement) => void) | React.Ref<any>;
allowedDecimalSeparators?: Array<string>;
customNumerals?: [
string,
string,
string,
string,
string,
string,
string,
string,
string,
string,
];
customNumerals?: [string, string, string, string, string, string, string, string, string, string];
};
export type InputAttributes = Omit<React.InputHTMLAttributes<HTMLInputElement>, 'defaultValue' | 'value'>
export type InputAttributes = Omit<
React.InputHTMLAttributes<HTMLInputElement>,
'defaultValue' | 'value'
>;
export type NumberFormatProps<T = InputAttributes> = NumberFormatPropsBase<T> &
Omit<React.InputHTMLAttributes<HTMLInputElement>, keyof T> &
Omit<T, keyof NumberFormatPropsBase<unknown> | 'ref'>;

class NumberFormat<T> extends React.Component<NumberFormatProps<T>, any> {}
export default NumberFormat;
export default NumberFormat;
1 change: 1 addition & 0 deletions test/types/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"module": "commonjs",
"lib": ["es6", "dom"],
"jsx": "react",
"esModuleInterop": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitAny": true,
Expand Down
Loading

0 comments on commit 201ddde

Please sign in to comment.