Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Xiao committed Jun 19, 2019
1 parent 9bdc773 commit f83d20d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,7 @@ class Index extends Component {
check: true, // Optional.[Bool].Default: true. To determin if you need to validate.
required: true, // Optional.[Bool].Default: true. To determin if it is a required field.
// type: 'string', // Optional.[String].Default: "string". Validation type, options are ['string', 'number'].
// numberType: 'decimal', // Optional.[String].Default: "decimal". Validation number type, options are ['decimal', 'int']. Handy when the validation type is number.
// showMsg: true, // Optional.[Bool].Default: true. To determin display the error message or not.
// min: 2, // Optional.[Number].Default: 0. Validation of min length when validationOption['type'] is string, min amount when validationOption['type'] is number.
// max: 10, // Optional.[Number].Default: 0. Validation of max length when validationOption['type'] is string, max amount when validationOption['type'] is number.
Expand Down
21 changes: 15 additions & 6 deletions src/js/Inputs/Textbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { REACT_INPUTS_VALIDATION_CUSTOM_ERROR_MESSAGE_EXAMPLE, DEFAULT_LOCALE, M
import reactInputsValidationCss from './react-inputs-validation.css';
const TYPE = 'textbox';
const VALIDATE_OPTION_TYPE_LIST = ['string', 'number'];
const VALIDATE_NUMBER_TYPE_LIST = ['decimal', 'int'];
const DEFAULT_MAX_LENGTH = 524288; // Default value is 524288
const DEFAULT_AUTO_COMPLETE = 'on'; // Default value is on
interface DefaultValidationOption {
Expand All @@ -15,6 +16,7 @@ interface DefaultValidationOption {
min?: number;
max?: number;
type?: string;
numberType?: string;
name?: string;
check?: boolean;
showMsg?: boolean;
Expand All @@ -27,12 +29,13 @@ interface DefaultValidationOption {
customFunc?: Function | undefined;
}
const getDefaultValidationOption = (obj: DefaultValidationOption) => {
let { reg, min, max, type, name, check, length, regMsg, compare, required, showMsg, locale, msgOnError, msgOnSuccess, customFunc } = obj;
let { reg, min, max, type, numberType, name, check, length, regMsg, compare, required, showMsg, locale, msgOnError, msgOnSuccess, customFunc } = obj;
locale = typeof locale !== 'undefined' ? locale : DEFAULT_LOCALE;
reg = typeof reg !== 'undefined' ? reg : '';
min = typeof min !== 'undefined' ? min : 0;
max = typeof max !== 'undefined' ? max : 0;
type = typeof type !== 'undefined' ? type : 'string';
numberType = typeof numberType !== 'undefined' ? numberType : 'string';
name = typeof name !== 'undefined' ? name : '';
check = typeof check !== 'undefined' ? check : true;
showMsg = typeof showMsg !== 'undefined' ? showMsg : true;
Expand All @@ -48,6 +51,7 @@ const getDefaultValidationOption = (obj: DefaultValidationOption) => {
min,
max,
type,
numberType,
name,
check,
length,
Expand Down Expand Up @@ -106,7 +110,7 @@ interface Props {
onKeyUp?: (e: React.KeyboardEvent<HTMLElement>) => void;
validationCallback?: (res: boolean) => void;
}
const autoFormatNumber = (v: number | string) => {
const autoFormatNumber = (v: number | string, numberType: string) => {
const DOT = '.';
let res = '';
let hasDot = false;
Expand All @@ -116,13 +120,18 @@ const autoFormatNumber = (v: number | string) => {
const charCode = i.toLowerCase().charCodeAt(0);
if ((charCode >= 48 && charCode <= 57) || (charCode === 46 && !hasDot)) {
if (charCode === 46) {
if (numberType === VALIDATE_NUMBER_TYPE_LIST[1]) {
return;
}
hasDot = true;
}
res += i;
}
});
if (res.length && res[0] === DOT) {
res = `0${res}`;
if (numberType === VALIDATE_NUMBER_TYPE_LIST[0]) {
if (res.length && res[0] === DOT) {
res = `0${res}`;
}
}
return res;
};
Expand Down Expand Up @@ -204,9 +213,9 @@ const component: React.FC<Props> = ({
return;
}
}
const { type } = option;
const { type, numberType } = option;
if (type === VALIDATE_OPTION_TYPE_LIST[1]) {
v = String(autoFormatNumber(v));
v = String(autoFormatNumber(v, VALIDATE_NUMBER_TYPE_LIST.indexOf(numberType) >= 0 ? numberType : VALIDATE_NUMBER_TYPE_LIST[0]));
}
setInternalValue(v);
onChange && onChange(v, e);
Expand Down

0 comments on commit f83d20d

Please sign in to comment.