-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dashboard): add grid settings configuration ux
- Loading branch information
Showing
10 changed files
with
231 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React from 'react'; | ||
|
||
import { Box, Checkbox, Modal, NonCancelableCustomEvent, SpaceBetween } from '@cloudscape-design/components'; | ||
import { BaseChangeDetail } from '@cloudscape-design/components/input/interfaces'; | ||
|
||
import LabeledInput from '../util/labeledInput'; | ||
import { useGridSettings } from './useGridSettings'; | ||
|
||
// Should never return NaN | ||
const numberFromDetail = (event: NonCancelableCustomEvent<BaseChangeDetail>) => parseInt(event.detail.value) || 0; | ||
|
||
export type DashboardSettingsProps = { | ||
onClose: () => void; | ||
isVisible: boolean; | ||
}; | ||
|
||
const DashboardSettings: React.FC<DashboardSettingsProps> = ({ onClose, isVisible }) => { | ||
const { | ||
rows, | ||
columns, | ||
cellSize, | ||
stretchToFit, | ||
onChangeCellSize, | ||
onChangeNumberOfColumns, | ||
onChangeNumberOfRows, | ||
onToggleStretchToFit, | ||
} = useGridSettings(); | ||
|
||
return ( | ||
<Modal onDismiss={onClose} visible={isVisible} closeAriaLabel='Close modal' header='Dashboard Settings'> | ||
<Box> | ||
<SpaceBetween direction='vertical' size='l'> | ||
<Checkbox onChange={({ detail }) => onToggleStretchToFit(detail.checked)} checked={stretchToFit}> | ||
Stretch grid to fit screen size. | ||
</Checkbox> | ||
<LabeledInput | ||
label='Cell Size' | ||
disabled={stretchToFit} | ||
type='number' | ||
value={cellSize.toFixed()} | ||
onChange={(event) => onChangeCellSize(numberFromDetail(event))} | ||
/> | ||
<LabeledInput | ||
label='Number of Rows' | ||
type='number' | ||
value={rows.toFixed()} | ||
onChange={(event) => onChangeNumberOfRows(numberFromDetail(event))} | ||
/> | ||
<LabeledInput | ||
label='Number of Columns' | ||
type='number' | ||
value={columns.toFixed()} | ||
onChange={(event) => onChangeNumberOfColumns(numberFromDetail(event))} | ||
/> | ||
</SpaceBetween> | ||
</Box> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default DashboardSettings; |
54 changes: 54 additions & 0 deletions
54
packages/dashboard/src/components/actions/useGridSettings.spec.tsx
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,54 @@ | ||
import React from 'react'; | ||
import { act, renderHook } from '@testing-library/react'; | ||
import { Provider } from 'react-redux'; | ||
|
||
import { configureDashboardStore } from '~/store'; | ||
import { useGridSettings } from './useGridSettings'; | ||
import type { ReactNode } from 'react'; | ||
import { initialState } from '~/store/state'; | ||
|
||
const TestProvider: React.FC<{ | ||
children: ReactNode; | ||
}> = ({ children }) => <Provider store={configureDashboardStore(initialState)}>{children}</Provider>; | ||
|
||
it('has initial values', () => { | ||
const { result } = renderHook(() => useGridSettings(), { | ||
wrapper: ({ children }) => <TestProvider children={children} />, | ||
}); | ||
|
||
expect(result.current.rows).toBe(initialState.grid.height); | ||
expect(result.current.columns).toBe(initialState.grid.width); | ||
expect(result.current.cellSize).toBe(initialState.grid.cellSize); | ||
expect(result.current.stretchToFit).toBe(initialState.grid.stretchToFit); | ||
expect(result.current.stretchToFit).toBe(initialState.grid.stretchToFit); | ||
}); | ||
|
||
it('has can change values', () => { | ||
const { result } = renderHook(() => useGridSettings(), { | ||
wrapper: ({ children }) => <TestProvider children={children} />, | ||
}); | ||
|
||
const initialCellSize = initialState.grid.cellSize; | ||
act(() => { | ||
result.current.onChangeCellSize(initialCellSize + 1); | ||
}); | ||
expect(result.current.cellSize).toBe(initialCellSize + 1); | ||
|
||
const initialHeight = initialState.grid.height; | ||
act(() => { | ||
result.current.onChangeNumberOfRows(initialHeight + 1); | ||
}); | ||
expect(result.current.rows).toBe(initialHeight + 1); | ||
|
||
const initialWidth = initialState.grid.width; | ||
act(() => { | ||
result.current.onChangeNumberOfColumns(initialWidth + 1); | ||
}); | ||
expect(result.current.columns).toBe(initialWidth + 1); | ||
|
||
const initialStretchToFit = initialState.grid.stretchToFit; | ||
act(() => { | ||
result.current.onToggleStretchToFit(!initialStretchToFit); | ||
}); | ||
expect(result.current.stretchToFit).toBe(!initialStretchToFit); | ||
}); |
39 changes: 39 additions & 0 deletions
39
packages/dashboard/src/components/actions/useGridSettings.ts
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,39 @@ | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
|
||
import { DashboardState } from '~/store/state'; | ||
import { | ||
onChangeDashboardCellSizeAction, | ||
onChangeDashboardHeightAction, | ||
onChangeDashboardStretchToFitAction, | ||
onChangeDashboardWidthAction, | ||
} from '~/store/actions'; | ||
|
||
export const useGridSettings = () => { | ||
const dispatch = useDispatch(); | ||
|
||
const { width, height, cellSize, stretchToFit } = useSelector((state: DashboardState) => state.grid); | ||
|
||
const onToggleStretchToFit = (updatedStretchToFit: boolean) => { | ||
dispatch(onChangeDashboardStretchToFitAction({ stretchToFit: updatedStretchToFit })); | ||
}; | ||
const onChangeNumberOfColumns = (columns: number) => { | ||
dispatch(onChangeDashboardWidthAction({ width: columns })); | ||
}; | ||
const onChangeNumberOfRows = (rows: number) => { | ||
dispatch(onChangeDashboardHeightAction({ height: rows })); | ||
}; | ||
const onChangeCellSize = (updatedCellSize: number) => { | ||
dispatch(onChangeDashboardCellSizeAction({ cellSize: updatedCellSize })); | ||
}; | ||
|
||
return { | ||
rows: height, | ||
columns: width, | ||
cellSize, | ||
stretchToFit, | ||
onToggleStretchToFit, | ||
onChangeCellSize, | ||
onChangeNumberOfColumns, | ||
onChangeNumberOfRows, | ||
}; | ||
}; |
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,13 @@ | ||
import React from 'react'; | ||
|
||
import { FormField, Input, InputProps } from '@cloudscape-design/components'; | ||
|
||
export type LabeledInputProps = InputProps & { label: string }; | ||
|
||
const LabeledInput: React.FC<LabeledInputProps> = ({ label, ...inputProps }) => ( | ||
<FormField label={label}> | ||
<Input {...inputProps} /> | ||
</FormField> | ||
); | ||
|
||
export default LabeledInput; |
22 changes: 22 additions & 0 deletions
22
packages/dashboard/src/store/actions/changeDashboardGrid/changeStretchToFit.spec.ts
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,22 @@ | ||
import { changeDashboardStretchToFit, onChangeDashboardStretchToFitAction } from './changeStretchToFit'; | ||
import { initialState } from '../../state'; | ||
|
||
it('can toggle the dashboard to stretch to fit', () => { | ||
expect( | ||
changeDashboardStretchToFit( | ||
initialState, | ||
onChangeDashboardStretchToFitAction({ | ||
stretchToFit: false, | ||
}) | ||
).grid.stretchToFit | ||
).toEqual(false); | ||
|
||
expect( | ||
changeDashboardStretchToFit( | ||
initialState, | ||
onChangeDashboardStretchToFitAction({ | ||
stretchToFit: true, | ||
}) | ||
).grid.stretchToFit | ||
).toEqual(true); | ||
}); |
23 changes: 23 additions & 0 deletions
23
packages/dashboard/src/store/actions/changeDashboardGrid/changeStretchToFit.ts
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,23 @@ | ||
import { changeGridProperty } from './updateGrid'; | ||
import type { Action } from 'redux'; | ||
import type { DashboardState } from '../../state'; | ||
|
||
type ChangeDashboardStretchToFitActionPayload = { | ||
stretchToFit: boolean; | ||
}; | ||
export interface ChangeDashboardStretchToFitAction extends Action { | ||
type: 'CHANGE_STRETCH_TO_FIT'; | ||
payload: ChangeDashboardStretchToFitActionPayload; | ||
} | ||
|
||
export const onChangeDashboardStretchToFitAction = ( | ||
payload: ChangeDashboardStretchToFitActionPayload | ||
): ChangeDashboardStretchToFitAction => ({ | ||
type: 'CHANGE_STRETCH_TO_FIT', | ||
payload, | ||
}); | ||
|
||
export const changeDashboardStretchToFit = ( | ||
state: DashboardState, | ||
action: ChangeDashboardStretchToFitAction | ||
): DashboardState => changeGridProperty(state, 'stretchToFit', action.payload.stretchToFit); |
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