Skip to content
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

Sort widget types by name #28

Merged
merged 3 commits into from
Sep 4, 2019
Merged
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
17 changes: 8 additions & 9 deletions cogboard-webapp/src/components/WidgetForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import styled from '@emotion/styled/macro';

import widgetTypes from './widgets';
import { useFormData } from '../hooks';
import DropdownField from './DropdownField';
import WidgetTypeForm from './WidgetTypeForm';
import { sortByKey } from "./helpers";
import { COLUMNS_MIN, ROWS_MIN } from '../constants';

import { Box, FormControlLabel, FormControl, MenuItem, TextField, Switch } from '@material-ui/core';
import { COLUMNS_MIN, ROWS_MIN } from '../constants';
import DropdownField from './DropdownField';
import WidgetTypeForm from './WidgetTypeForm';

const StyledFieldset = styled(FormControl)`
display: flex;
Expand All @@ -22,11 +23,9 @@ const StyledNumberField = styled(TextField)`
`;

const renderWidgetTypesMenu = (widgetTypes) =>
Object.entries(widgetTypes).map(([type, { name }]) => {
const formatedName = type === 'DefaultWidget' ? <em>{name}</em> : name;

return <MenuItem key={type} value={type}>{formatedName}</MenuItem>;
});
Object.entries(widgetTypes).map(([type, { name }]) => (
<MenuItem key={type} value={type}>{name}</MenuItem>
));

const WidgetForm = ({ renderActions, ...initialFormValues }) => {
const boardColumns = useSelector(
Expand All @@ -43,7 +42,7 @@ const WidgetForm = ({ renderActions, ...initialFormValues }) => {
id="widget-type"
name="type"
value={values.type}
dropdownItems={widgetTypes}
dropdownItems={sortByKey(widgetTypes, 'name')}
>
{renderWidgetTypesMenu}
</DropdownField>
Expand Down
11 changes: 10 additions & 1 deletion cogboard-webapp/src/components/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ export const setSize = factor => ({ theme }) => `${theme.spacing(factor)}px`;

export const splitPropsGroupName = (propName) => {
return propName.includes('.') ? propName.split('.') : [undefined, propName];
};
};

export const sortByKey = (obj, key, asc = true) => Object.entries(obj)
.sort(([, { [key]: keyA }], [, { [key]: keyB }]) => (
asc ? keyA.localeCompare(keyB) : keyB.localeCompare(keyA)))
.reduce((newObj, [key, value]) => {
newObj[key] = value;

return newObj;
}, {});