-
-
Notifications
You must be signed in to change notification settings - Fork 921
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(DateInput,DateRangeInput): fix the issue of incorrect cursor posi…
…tion in the input box (#3785) * fix(DateInput,DateRangeInput): fix the issue of incorrect cursor position in the input box * test: add tests for input continuously * test: add tests for input continuously * test: add tests for input continuously
- Loading branch information
Showing
13 changed files
with
235 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useCallback, useRef } from 'react'; | ||
import { getPatternGroups } from '../utils'; | ||
import useUpdateEffect from '../../utils/useUpdateEffect'; | ||
export function useFieldCursor<V = Date | null>(format: string, value?: V) { | ||
const typeCount = useRef(0); | ||
|
||
const increment = () => { | ||
typeCount.current += 1; | ||
}; | ||
|
||
const reset = () => { | ||
typeCount.current = 0; | ||
}; | ||
|
||
const isMoveCursor = useCallback( | ||
(value: number, pattern: string) => { | ||
const patternGroup = getPatternGroups(format, pattern); | ||
|
||
if (value.toString().length === patternGroup.length) { | ||
return true; | ||
} else if (pattern === 'y' && typeCount.current === 4) { | ||
return true; | ||
} else if (pattern !== 'y' && typeCount.current === 2) { | ||
return true; | ||
} | ||
|
||
switch (pattern) { | ||
case 'M': | ||
return parseInt(`${value}0`) > 12; | ||
case 'd': | ||
return parseInt(`${value}0`) > 31; | ||
case 'H': | ||
return parseInt(`${value}0`) > 23; | ||
case 'h': | ||
return parseInt(`${value}0`) > 12; | ||
case 'm': | ||
case 's': | ||
return parseInt(`${value}0`) > 59; | ||
default: | ||
return false; | ||
} | ||
}, | ||
[format] | ||
); | ||
|
||
useUpdateEffect(() => { | ||
if (!value) { | ||
reset(); | ||
} | ||
}, [value]); | ||
|
||
return { increment, reset, isMoveCursor }; | ||
} | ||
|
||
export default useFieldCursor; |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useState } from 'react'; | ||
|
||
const defaultSelectedState = { | ||
selectedPattern: 'y', | ||
selectionStart: 0, | ||
selectionEnd: 0 | ||
}; | ||
|
||
export function useSelectedState() { | ||
const [selectedState, setSelectedState] = useState<{ | ||
selectedPattern: string; | ||
selectionStart: number; | ||
selectionEnd: number; | ||
}>(defaultSelectedState); | ||
|
||
return { selectedState, setSelectedState }; | ||
} | ||
|
||
export default useSelectedState; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import DateInput from './DateInput'; | ||
export { useDateInputState } from './useDateInputState'; | ||
export { useKeyboardInputEvent } from './useKeyboardInputEvent'; | ||
export { useIsFocused } from './useIsFocused'; | ||
export { useDateInputState } from './hooks/useDateInputState'; | ||
export { useKeyboardInputEvent } from './hooks/useKeyboardInputEvent'; | ||
export { useIsFocused } from './hooks/useIsFocused'; | ||
export { useSelectedState } from './hooks/useSelectedState'; | ||
export { useFieldCursor } from './hooks/useFieldCursor'; | ||
export * from './utils'; | ||
export type { DateInputProps } from './DateInput'; | ||
export default DateInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.