Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
}
}
26 changes: 19 additions & 7 deletions src/timeInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(':')
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down