From 51a2d3f9ba7cbfcbaf6489267d01eb76918bbc86 Mon Sep 17 00:00:00 2001 From: atnartur Date: Sat, 10 Feb 2018 08:17:29 +0300 Subject: [PATCH] added one symbol hour support --- package.json | 4 ++-- src/timeInput.jsx | 26 +++++++++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 6145f64..f007926 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "A simple react component for select time in format HH:mm", "main": "index.js", "scripts": { - "example": "webpack-dev-server --content-base ./example/ --config ./example/webpack.config.js --colors --watch ", + "example": "webpack-dev-server --content-base ./example/ --config ./example/webpack.config.js --colors --watch --host 127.0.0.1", "build": "babel src/timeInput.jsx -o index.js", "prepublish": "npm run build", "test": "karma start --auto-watch" @@ -51,6 +51,6 @@ "react-dom": "15.0.1", "react-hot-loader": "^1.3.0", "webpack": "1.13.0", - "webpack-dev-server": "^1.16.2" + "webpack-dev-server": "1.16.2" } } diff --git a/src/timeInput.jsx b/src/timeInput.jsx index d8dade8..2ba17d2 100644 --- a/src/timeInput.jsx +++ b/src/timeInput.jsx @@ -33,8 +33,7 @@ class TimeInput extends Component { } isValid (val) { - var letterArr = val.split(':').join('').split(''), - regexp = /^\d{0,2}?\:?\d{0,2}$/, + var regexp = /^\d{0,2}?\:?\d{0,2}$/, valArr = []; var [hoursStr, minutesStr] = val.split(':') @@ -43,8 +42,16 @@ class TimeInput extends Component { return false } - const hours = Number(hoursStr) - const minutes = Number(minutesStr) + let hours = Number(hoursStr); + + if (hours > 23 && minutesStr === undefined) { + let arr = hoursStr.split(''); + hours = Number(arr[0]); + minutesStr = arr[1]; + val = arr[0] + ':' + arr[1] + } + + const minutes = Number(minutesStr); const isValidHour = (hour) => Number.isInteger(hours) && hours >= 0 && hours < 24 const isValidMinutes = (minutes) => (Number.isInteger(minutes) && hours >= 0 && hours < 24) || Number.isNaN(minutes) @@ -79,9 +86,14 @@ class TimeInput extends Component { return; } if (this.isValid(val)) { - - if (val.length === 2 && this.lastVal.length !== 3 && val.indexOf(':') === -1) { - val = val + ':'; + if (val.length === 2) { + if (Number(val) > 23) { + let arr = val.split(''); + val = arr[0] + ':' + arr[1]; + } + else if (this.lastVal.length !== 3 && val.indexOf(':') === -1) { + val = val + ':'; + } } if (val.length === 2 && this.lastVal.length === 3) {