-
Notifications
You must be signed in to change notification settings - Fork 17
7093 add sort toggle to prevent report filter dropdowns overlapping #7406
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
Changes from all commits
89f94a3
da31b8c
acfbdd3
2cdc675
ef440ba
9453b77
dc97264
a83ec1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React, { useState } from 'react'; | ||
import { rankWith, ControlProps, uiTypeIs } from '@jsonforms/core'; | ||
import { withJsonFormsControlProps } from '@jsonforms/react'; | ||
import { | ||
alpha, | ||
DetailInputWithLabelRow, | ||
Theme, | ||
useTranslation, | ||
} from '@openmsupply-client/common'; | ||
import { FORM_LABEL_WIDTH, DefaultFormRowSx } from '../styleConstants'; | ||
import { FlatButton } from '@common/components'; | ||
import { SortAscIcon, SortDescIcon } from '@common/icons'; | ||
|
||
export const SortToggleTester = rankWith(10, uiTypeIs('SortToggle')); | ||
|
||
type SortDirection = 'asc' | 'desc' | null; | ||
|
||
const UIComponent = (props: ControlProps) => { | ||
const { handleChange, label, path, enabled } = props; | ||
const t = useTranslation(); | ||
const [sortDirection, setSortDirection] = useState<SortDirection>(); | ||
|
||
if (!props.visible) { | ||
return null; | ||
} | ||
|
||
const selectedStyles = (theme: Theme) => ({ | ||
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. custom styling for when the button is the selected option |
||
fontWeight: 'bold', | ||
backgroundColor: alpha(theme.palette.primary.main, 0.3), | ||
'&:hover': { | ||
backgroundColor: alpha(theme.palette.primary.main, 0.3), | ||
}, | ||
}); | ||
|
||
const getSelectedSx = (value: SortDirection) => | ||
sortDirection === value ? selectedStyles : undefined; | ||
|
||
const handleClick = (value: SortDirection) => { | ||
const newValue = sortDirection === value ? null : value; | ||
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. This line with |
||
setSortDirection(newValue); | ||
handleChange(path, newValue); | ||
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. oh hahahaha ya that'd do it |
||
}; | ||
|
||
return ( | ||
<DetailInputWithLabelRow | ||
label={label} | ||
DisabledInput={!enabled} | ||
labelWidthPercentage={FORM_LABEL_WIDTH} | ||
sx={DefaultFormRowSx} | ||
Input={ | ||
<> | ||
<FlatButton | ||
label={t('report.ascending')} | ||
onClick={() => handleClick('asc')} | ||
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. Passing in 'asc' as the value, as FlatButton does not have a value attribute. Works in this scenario where only two options exist |
||
startIcon={<SortAscIcon />} | ||
sx={[getSelectedSx('asc') || {}, { borderRadius: 24 }]} | ||
/> | ||
<FlatButton | ||
label={t('report.descending')} | ||
onClick={() => handleClick('desc')} | ||
startIcon={<SortDescIcon />} | ||
sx={[getSelectedSx('desc') || {}, { borderRadius: 24 }]} | ||
/> | ||
</> | ||
} | ||
/> | ||
); | ||
}; | ||
|
||
export const SortToggle = withJsonFormsControlProps(UIComponent); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,8 +26,8 @@ | |
}, | ||
"dir": { | ||
"description": "sort by dir", | ||
"type": "string", | ||
"enum": ["asc", "desc"] | ||
"type": ["string", "null"], | ||
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. Included 'null' here to prevent console errors when deselecting |
||
"format": "SortToggle" | ||
}, | ||
"monthlyConsumptionLookBackPeriod": { | ||
"description": "Average Monthly Consumption Look Back Period", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,8 +35,8 @@ | |
}, | ||
"dir": { | ||
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. tangentially I find it funny that we shorten this to dir, meanwhile having properties like "monthlyConsumptionLookBackPeriod". Also just curious is the "description" just for documenting in this JSON or is it shown somewhere? 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. Yes that is quite a difference between the property names! It's not immediately obvious (at least to me) what 'dir' represents. I'd be happy to update that to 'direction' 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. Honestly the reason I even commented is it took me a second too - I was like wait Nah maybe a refactor issue if anything heh. Yea I thought description might be a tool tip, but then it'd need localisation! Maybe it should be regardless, but the use case isn't really obvious heh. 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. Thanks Chris, I have raised in #7546 |
||
"description": "sort by dir", | ||
"type": "string", | ||
"enum": ["asc", "desc"] | ||
"type": ["string", "null"], | ||
"format": "SortToggle" | ||
} | ||
} | ||
} | ||
|
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.
Looks good. If I could request thinking about one thing, it's that this is pretty hard coded to the sorting use case. A more generic and flexible component could probably be made of this (perhaps in the future)
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 bringing that up, I should have covered that in the PR!
I did consider this when creating it, and went with the most straight forward solution to fix the issue. With sorting direction only having two possible options, and without another use case in mind, it made sense to me to hardcode for this case
There is an existing forms pattern with 'options' like in the 'sort-by' dropdown below - to make that pattern work for a flexible toggle button setup it would need at least:
open-msupply/standard_reports/item-usage/latest/argument_schemas/arguments_ui.json
Lines 31 to 46 in 35917ab
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.
Yea with 1-3 short options (especially if icons and no text) toggle is good, <=~5 radio buttons (especially if text) and beyond that is combobox.