-
Notifications
You must be signed in to change notification settings - Fork 858
Inherit selected value to quick select in EuiSuperDatePicker #3105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
chandlerprall
merged 21 commits into
elastic:master
from
ashikmeerankutty:fix/super-date-picker
Mar 26, 2020
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
4e79305
Update quick select values on change of input
ashikmeerankutty 5553459
updated changelog
ashikmeerankutty 99d2f3d
updated cl
ashikmeerankutty b5108aa
removed console statement
ashikmeerankutty 66532c3
support other commonly used selections
ashikmeerankutty 414af6f
Merge branch 'master' into fix/super-date-picker
ashikmeerankutty ba19b25
Merge branch 'master' into fix/super-date-picker
ashikmeerankutty af3edb0
Added function description
ashikmeerankutty e80a549
Merge branch 'master' into fix/super-date-picker
ashikmeerankutty 0e279fd
renamed file
ashikmeerankutty 96cf8a4
Added quick select utils unit tests
ashikmeerankutty 988ebb9
updated function for a better result for 'now'
ashikmeerankutty 78072f6
updated test
ashikmeerankutty 9939ac6
added return
ashikmeerankutty d57239a
merge master
ashikmeerankutty f0f51f3
fixed bug in now -> relative case
ashikmeerankutty 99db6c2
removed unwanted && and - escape in regex
ashikmeerankutty 6659f4d
removed []
ashikmeerankutty 2b5165b
Added test for duration parsing
ashikmeerankutty df45d4b
Merge branch 'master' into fix/super-date-picker
ashikmeerankutty db6cc79
Merge branch 'master' into fix/super-date-picker
chandlerprall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
72 changes: 72 additions & 0 deletions
72
src/components/date_picker/super_date_picker/quick_select_popover/quick_select_utils.js
This file contains hidden or 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,72 @@ | ||
| /** | ||
| * This function returns time value, time unit and time tense for a given time string. | ||
| * For example: for `now-40m` it will parse output as time value to `40` | ||
| * time unit to `m` and time unit to `last`. | ||
| * If given a datetime string it will return a default value. | ||
| * If the given string is in the format such as `now/d` it will parse the string to moment object | ||
| * and find the time value, time unit and time tense using moment | ||
| * This function accepts two strings start and end time. I the start value is now then it uses | ||
| * the end value to parse. | ||
| * | ||
| * @param {string} start start time | ||
| * @param {string} start end time | ||
| * @returns {object} time value, time unit and time tense | ||
| */ | ||
|
|
||
| import moment from 'moment'; | ||
| import dateMath from '@elastic/datemath'; | ||
| import { isString } from '../../../../services/predicate'; | ||
| import { relativeUnitsFromLargestToSmallest } from '../relative_options'; | ||
| import { DATE_MODES } from '../date_modes'; | ||
|
|
||
| const LAST = 'last'; | ||
| const NEXT = 'next'; | ||
|
|
||
| const isNow = value => value === DATE_MODES.NOW; | ||
|
|
||
| export const parseTimeParts = (start, end) => { | ||
| const results = { | ||
| timeValueDefault: 15, | ||
| timeUnitsDefault: 'm', | ||
| timeTenseDefault: LAST, | ||
| }; | ||
|
|
||
| const value = isNow(start) ? end : start; | ||
|
|
||
| const matches = | ||
| isString(value) && | ||
| value.match(/now(([-+])(\d+)([smhdwMy])(\/[smhdwMy])?)?/); | ||
|
|
||
| if (!matches) { | ||
| return results; | ||
| } | ||
|
|
||
| const operator = matches[2]; | ||
| const timeValue = matches[3]; | ||
| const timeUnitsDefault = matches[4]; | ||
|
|
||
| if (timeValue && timeUnitsDefault && operator) { | ||
| const timeValueDefault = parseInt(timeValue, 10); | ||
| const timeTenseDefault = operator === '+' ? NEXT : LAST; | ||
|
|
||
| return { | ||
| timeValueDefault, | ||
| timeUnitsDefault, | ||
| timeTenseDefault, | ||
| }; | ||
| } | ||
|
|
||
| const duration = moment.duration(moment().diff(dateMath.parse(value))); | ||
| let unitOp = ''; | ||
| for (let i = 0; i < relativeUnitsFromLargestToSmallest.length; i++) { | ||
| const as = duration.as(relativeUnitsFromLargestToSmallest[i]); | ||
| if (as < 0) unitOp = '+'; | ||
| if (Math.abs(as) > 1) { | ||
| results.timeValueDefault = Math.round(Math.abs(as)); | ||
| results.timeUnitsDefault = relativeUnitsFromLargestToSmallest[i]; | ||
| results.timeTenseDefault = unitOp === '+' ? NEXT : LAST; | ||
| break; | ||
| } | ||
| } | ||
| return results; | ||
| }; | ||
51 changes: 51 additions & 0 deletions
51
src/components/date_picker/super_date_picker/quick_select_popover/quick_select_utils.test.js
This file contains hidden or 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,51 @@ | ||
| import moment from 'moment'; | ||
| import { parseTimeParts } from './quick_select_utils'; | ||
|
|
||
| describe('parseTimeParts', () => { | ||
| it('should parse now', () => { | ||
| const out = parseTimeParts('now', 'now+5m'); | ||
| expect(out).toEqual({ | ||
| timeValueDefault: 5, | ||
| timeUnitsDefault: 'm', | ||
| timeTenseDefault: 'next', | ||
| }); | ||
| }); | ||
|
|
||
| it('should parse now-2h', () => { | ||
| const out = parseTimeParts('now-2h', 'now+5m'); | ||
| expect(out).toEqual({ | ||
| timeValueDefault: 2, | ||
| timeUnitsDefault: 'h', | ||
| timeTenseDefault: 'last', | ||
| }); | ||
| }); | ||
|
|
||
| it('should parse now+2h', () => { | ||
| const out = parseTimeParts('now+2h', 'now+5m'); | ||
| expect(out).toEqual({ | ||
| timeValueDefault: 2, | ||
| timeUnitsDefault: 'h', | ||
| timeTenseDefault: 'next', | ||
| }); | ||
| }); | ||
|
|
||
| describe('duration parsing', () => { | ||
| const duration = moment.duration; | ||
| beforeEach(() => { | ||
| moment.duration = () => duration(6 * 60 * 60 * 1000); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| moment.duration = duration; | ||
| }); | ||
|
|
||
| it('should parse now/d', () => { | ||
| const out = parseTimeParts('now/d', 'now+5m'); | ||
| expect(out).toEqual({ | ||
| timeValueDefault: 6, | ||
| timeUnitsDefault: 'h', | ||
| timeTenseDefault: 'last', | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.