-
Notifications
You must be signed in to change notification settings - Fork 0
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
test date input for time slider and adjust input element #1095
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4839c8b
test date input for time slider and adjust input element
moontrip 931de5b
applying to SAM and adjust layout
moontrip a772b99
add step buttons
moontrip aafb235
Merge branch 'main' into timeslider-date-input
moontrip 2ba81b1
address feedbacks for step buttons
moontrip 291d302
Merge branch 'main' into timeslider-date-input
moontrip f82ccf6
Merge branch 'main' into timeslider-date-input
moontrip 1716200
Merge branch 'main' into timeslider-date-input
moontrip 9efbed8
address feedbacks
moontrip f8801c5
considering leap years
moontrip b1b2c1e
Merge branch 'main' into timeslider-date-input
moontrip d94500b
updated help tooltip
bobular 16088c0
simplifying codes
moontrip 2b32107
Merge branch 'timeslider-date-input' of https://github.com/VEuPathDB/…
moontrip 6bb10a9
Merge branch 'main' into timeslider-date-input
moontrip eceed45
Merge branch 'main' into timeslider-date-input
moontrip 9366590
remove href
moontrip 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 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 |
---|---|---|
|
@@ -91,46 +91,6 @@ function TimeSlider(props: TimeSliderProps) { | |
const getXData = (d: TimeSliderDataProp) => new Date(d.x); | ||
const getYData = (d: TimeSliderDataProp) => d.y; | ||
|
||
const onBrushChange = useMemo( | ||
() => | ||
debounce((domain: Bounds | null) => { | ||
if (!domain) return; | ||
const { x0, x1 } = domain; | ||
|
||
// computing the offset of 2 pixel (SAFE_PIXEL) in domain (milliseconds) | ||
// https://github.com/airbnb/visx/blob/86a851cb3bf622b013b186f02f955bcd6548a87f/packages/visx-brush/src/Brush.tsx#L14 | ||
const brushOffset = | ||
xBrushScale.invert(2).getTime() - xBrushScale.invert(0).getTime(); | ||
|
||
// compensating the offset | ||
// x0 and x1 are millisecond value | ||
const startDate = millisecondTodate(x0 + brushOffset); | ||
const endDate = millisecondTodate(x1 - brushOffset); | ||
|
||
setSelectedRange({ | ||
// don't let range go outside the xAxisRange, if provided | ||
start: xAxisRange | ||
? startDate < xAxisRange.start | ||
? xAxisRange.start | ||
: startDate | ||
: startDate, | ||
end: xAxisRange | ||
? endDate > xAxisRange.end | ||
? xAxisRange.end | ||
: endDate | ||
: endDate, | ||
}); | ||
}, debounceRateMs), | ||
[setSelectedRange, xAxisRange] | ||
); | ||
|
||
// Cancel any pending onBrushChange requests when this component is unmounted | ||
useEffect(() => { | ||
return () => { | ||
onBrushChange.cancel(); | ||
}; | ||
}, []); | ||
|
||
// bounds | ||
const xBrushMax = Math.max(width - margin.left - margin.right, 0); | ||
// take 70 % of given height considering axis tick/tick labels at the bottom | ||
|
@@ -176,10 +136,50 @@ function TimeSlider(props: TimeSliderProps) { | |
); | ||
|
||
// `brushKey` makes/fakes the brush as a controlled component, | ||
const brushKey = 'not_fake_controlled'; | ||
// selectedRange != null | ||
// ? selectedRange.start + ':' + selectedRange.end | ||
// : 'no_brush'; | ||
const brushKey = | ||
selectedRange != null | ||
? selectedRange.start + ':' + selectedRange.end | ||
: 'no_brush'; | ||
|
||
const onBrushChange = useMemo( | ||
() => | ||
debounce((domain: Bounds | null) => { | ||
if (!domain) return; | ||
const { x0, x1 } = domain; | ||
|
||
// computing the offset of 2 pixel (SAFE_PIXEL) in domain (milliseconds) | ||
// https://github.com/airbnb/visx/blob/86a851cb3bf622b013b186f02f955bcd6548a87f/packages/visx-brush/src/Brush.tsx#L14 | ||
const brushOffset = | ||
xBrushScale.invert(2).getTime() - xBrushScale.invert(0).getTime(); | ||
|
||
// compensating the offset | ||
// x0 and x1 are millisecond value | ||
const startDate = millisecondTodate(x0 + brushOffset); | ||
const endDate = millisecondTodate(x1 - brushOffset); | ||
|
||
setSelectedRange({ | ||
// don't let range go outside the xAxisRange, if provided | ||
start: xAxisRange | ||
? startDate < xAxisRange.start | ||
? xAxisRange.start | ||
: startDate | ||
: startDate, | ||
end: xAxisRange | ||
? endDate > xAxisRange.end | ||
? xAxisRange.end | ||
: endDate | ||
: endDate, | ||
}); | ||
}, debounceRateMs), | ||
[setSelectedRange, xAxisRange, debounceRateMs, xBrushScale] | ||
); | ||
|
||
// Cancel any pending onBrushChange requests when this component is unmounted | ||
useEffect(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
return () => { | ||
onBrushChange.cancel(); | ||
}; | ||
}, [onBrushChange]); | ||
|
||
return ( | ||
<div | ||
|
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 |
---|---|---|
@@ -1,11 +1,14 @@ | ||
import { useState } from 'react'; | ||
import { useState, useCallback } from 'react'; | ||
import { Story, Meta } from '@storybook/react/types-6-0'; | ||
import { LinePlotProps } from '../../plots/LinePlot'; | ||
import TimeSlider, { | ||
TimeSliderDataProp, | ||
} from '../../components/plotControls/TimeSlider'; | ||
import { DraggablePanel } from '@veupathdb/coreui/lib/components/containers'; | ||
|
||
import AxisRangeControl from '../../components/plotControls/AxisRangeControl'; | ||
import { NumberOrDateRange } from '../../types/general'; | ||
|
||
export default { | ||
title: 'Plot Controls/TimeSlider', | ||
component: TimeSlider, | ||
|
@@ -301,7 +304,56 @@ export const TimeFilter: Story<LinePlotProps> = (args: any) => { | |
|
||
// set constant values | ||
const defaultSymbolSize = 0.8; | ||
const defaultColor = '#333'; | ||
|
||
// control selectedRange | ||
const handleAxisRangeChange = useCallback( | ||
(newRange?: NumberOrDateRange) => { | ||
if (newRange) | ||
setSelectedRange({ | ||
start: newRange.min as string, | ||
end: newRange.max as string, | ||
}); | ||
}, | ||
[setSelectedRange] | ||
); | ||
|
||
const handleArrowClick = useCallback( | ||
(arrow: string) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, this is STORY code. I didn't understand how it worked but now I realise I should ignore it. |
||
// let's assume that selectedRange has the format of 'yyyy-mm-dd' | ||
if ( | ||
selectedRange && | ||
selectedRange.start != null && | ||
selectedRange.end != null | ||
) { | ||
const selectedRangeArray = | ||
arrow === 'left' | ||
? selectedRange.start.split('-') | ||
: selectedRange.end.split('-'); | ||
const addSubtractYear = | ||
arrow === 'left' | ||
? String(Number(selectedRangeArray[0]) - 1) | ||
: String(Number(selectedRangeArray[0]) + 1); | ||
const changeYear = | ||
addSubtractYear + | ||
'-' + | ||
selectedRangeArray[1] + | ||
'-' + | ||
selectedRangeArray[2]; | ||
setSelectedRange((prev) => { | ||
return arrow === 'left' | ||
? { | ||
start: changeYear as string, | ||
end: prev?.end as string, | ||
} | ||
: { | ||
start: prev?.start as string, | ||
end: changeYear as string, | ||
}; | ||
}); | ||
} | ||
}, | ||
[selectedRange, setSelectedRange] | ||
); | ||
|
||
return ( | ||
<DraggablePanel | ||
|
@@ -322,16 +374,47 @@ export const TimeFilter: Story<LinePlotProps> = (args: any) => { | |
> | ||
<div | ||
style={{ | ||
display: 'grid', | ||
gridTemplateColumns: '1fr repeat(1, auto) 1fr', | ||
gridColumnGap: '5px', | ||
display: 'flex', | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
paddingTop: '1em', | ||
}} | ||
> | ||
{/* display start to end value */} | ||
<div style={{ gridColumnStart: 2 }}> | ||
{selectedRange?.start} ~ {selectedRange?.end} | ||
<div> | ||
<button | ||
style={{ marginRight: '1em' }} | ||
onClick={() => handleArrowClick('left')} | ||
> | ||
<i className="fa fa-arrow-left" aria-hidden="true"></i> | ||
</button> | ||
</div> | ||
{/* add axis range control */} | ||
<AxisRangeControl | ||
range={ | ||
selectedRange != null | ||
? { | ||
min: selectedRange.start, | ||
max: selectedRange.end, | ||
} | ||
: undefined | ||
} | ||
onRangeChange={handleAxisRangeChange} | ||
valueType={'date'} | ||
// set maxWidth | ||
containerStyles={{ | ||
maxWidth: '350px', | ||
}} | ||
// default height of the input element is 36.5 which may be too high for timeSlider | ||
// thus, introduced new prop to control it | ||
inputHeight={20} | ||
/> | ||
<div> | ||
<button | ||
style={{ marginLeft: '2em' }} | ||
onClick={() => handleArrowClick('right')} | ||
> | ||
<i className="fa fa-arrow-right" aria-hidden="true"></i> | ||
</button> | ||
</div> | ||
</div> | ||
<TimeSlider | ||
|
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for spotting those missing deps!