-
Notifications
You must be signed in to change notification settings - Fork 60
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 input widet configuration (#581)
- Loading branch information
1 parent
90a0790
commit a1bf180
Showing
16 changed files
with
413 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
11 changes: 10 additions & 1 deletion
11
packages/dashboard/src/components/palette/icons/input-component.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 |
---|---|---|
@@ -1,5 +1,14 @@ | ||
import React from 'react'; | ||
|
||
const InputComponent: React.FC = () => <div>input</div>; | ||
const InputComponent: React.FC = () => { | ||
return ( | ||
<svg viewBox='0 0 20 20' xmlns='http://www.w3.org/2000/svg' className='palette-component-icon'> | ||
<text x='8' y='15'> | ||
I | ||
</text> | ||
<line x1='2.5' y1='20' x2='17.5' y2='20' stroke='inherit' strokeWidth='2'></line> | ||
</svg> | ||
); | ||
}; | ||
|
||
export default InputComponent; |
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
114 changes: 114 additions & 0 deletions
114
packages/dashboard/src/components/sidePanel/sections/inputSettingsSection/index.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,114 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import createWrapper from '@cloudscape-design/components/test-utils/dom'; | ||
import { Provider } from 'react-redux'; | ||
import InputSettings from './index'; | ||
import { InputWidget, RecursivePartial } from '~/types'; | ||
import { DashboardState } from '~/store/state'; | ||
import { DefaultDashboardMessages, DashboardMessages } from '~/messages'; | ||
import { configureDashboardStore } from '~/store'; | ||
import { MOCK_INPUT_WIDGET } from '../../../../../testing/mocks'; | ||
import { expect } from '@jest/globals'; | ||
|
||
const widget: InputWidget = { ...MOCK_INPUT_WIDGET }; | ||
|
||
const state: Partial<DashboardState> = { | ||
dashboardConfiguration: { | ||
widgets: [widget], | ||
viewport: { duration: '5m' }, | ||
}, | ||
selectedWidgets: [widget], | ||
}; | ||
|
||
const TestComponent = ( | ||
{ | ||
stateOverride = state, | ||
messageOverride = DefaultDashboardMessages, | ||
}: { | ||
stateOverride?: RecursivePartial<DashboardState>; | ||
messageOverride?: DashboardMessages; | ||
} = { stateOverride: state, messageOverride: DefaultDashboardMessages } | ||
) => ( | ||
<Provider store={configureDashboardStore(stateOverride)}> | ||
<InputSettings messageOverride={messageOverride} /> | ||
</Provider> | ||
); | ||
|
||
it('renders without options from state', () => { | ||
const props = { stateOverride: {} }; | ||
const { container } = render(<TestComponent {...props} />); | ||
const inputSettings = createWrapper(container); | ||
const options = inputSettings.findTokenGroup('[data-test-id="input-widget-token-list"]'); | ||
|
||
expect(options.findTokens().length).toBe(0); | ||
}); | ||
|
||
it('renders with options from state', () => { | ||
const { container } = render(<TestComponent />); | ||
const inputSettings = createWrapper(container); | ||
const options = inputSettings.findTokenGroup('[data-test-id="input-widget-token-list"]'); | ||
|
||
MOCK_INPUT_WIDGET.options.forEach(({ label }) => { | ||
expect(options.getElement()).toHaveTextContent(label); | ||
}); | ||
}); | ||
|
||
it('can add option', () => { | ||
const { container } = render(<TestComponent />); | ||
const inputSettings = createWrapper(container); | ||
const addOptionButton = inputSettings.findButton('[data-test-id="input-widget-add-option-btn"]'); | ||
const optionInput = inputSettings.findInput('[data-test-id="input-widget-option-input"]'); | ||
const options = inputSettings.findTokenGroup('[data-test-id="input-widget-token-list"]'); | ||
const newOption = 'lorem ipsum'; | ||
|
||
expect(addOptionButton.isDisabled()).toBeTruthy(); | ||
expect(options.findTokens().length).toBe(3); | ||
expect(options.getElement()).not.toHaveTextContent(newOption); | ||
|
||
optionInput.setInputValue(newOption); | ||
|
||
expect(addOptionButton.isDisabled()).toBeFalsy(); | ||
addOptionButton.click(); | ||
|
||
expect(options.findTokens().length).toBe(4); | ||
expect(options.getElement()).toHaveTextContent(newOption); | ||
}); | ||
|
||
it('can remove option', () => { | ||
const { container } = render(<TestComponent />); | ||
const inputSettings = createWrapper(container); | ||
const options = inputSettings.findTokenGroup('[data-test-id="input-widget-token-list"]'); | ||
|
||
expect(options.findTokens().length).toBe(3); | ||
|
||
options.findToken(2).findDismiss().click(); | ||
|
||
expect(options.findTokens().length).toBe(2); | ||
expect(options.findToken(1).getElement()).toHaveTextContent(widget.options[0].label); | ||
expect(options.findToken(2).getElement()).toHaveTextContent(widget.options[2].label); | ||
}); | ||
|
||
it('correctly renders translations', () => { | ||
const optionPlaceholder = 'lorem ipsum'; | ||
const addOptionLabel = 'lorem ipsum 2'; | ||
const props = { | ||
messageOverride: { | ||
...DefaultDashboardMessages, | ||
sidePanel: { | ||
...DefaultDashboardMessages.sidePanel, | ||
inputSettings: { | ||
...DefaultDashboardMessages.sidePanel.inputSettings, | ||
optionPlaceholder, | ||
addOptionLabel, | ||
}, | ||
}, | ||
}, | ||
}; | ||
const { container } = render(<TestComponent {...props} />); | ||
const inputSettings = createWrapper(container); | ||
const addOptionButton = inputSettings.findButton('[data-test-id="input-widget-add-option-btn"]'); | ||
const optionInput = inputSettings.findInput('[data-test-id="input-widget-option-input"]'); | ||
|
||
expect(addOptionButton.getElement()).toHaveTextContent(addOptionLabel); | ||
expect(optionInput.findNativeInput().getElement()).toHaveAttribute('placeholder', optionPlaceholder); | ||
}); |
64 changes: 64 additions & 0 deletions
64
packages/dashboard/src/components/sidePanel/sections/inputSettingsSection/index.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,64 @@ | ||
import React, { FC, useState } from 'react'; | ||
import { | ||
ExpandableSection, | ||
Grid, | ||
Input, | ||
Button, | ||
TokenGroup, | ||
InputProps, | ||
TokenGroupProps, | ||
} from '@cloudscape-design/components'; | ||
import { NonCancelableEventHandler } from '@cloudscape-design/components/internal/events'; | ||
import { useInputWidgetInput } from '../../utils'; | ||
import { DashboardMessages } from '~/messages'; | ||
|
||
export type InputComponentProps = { | ||
messageOverride: DashboardMessages; | ||
}; | ||
|
||
const InputSettings: FC<InputComponentProps> = ({ messageOverride }) => { | ||
const { | ||
sidePanel: { inputSettings }, | ||
} = messageOverride; | ||
const [options, setOptions] = useInputWidgetInput('options'); | ||
const [label, setLabel] = useState<string>(); | ||
|
||
const addOption: NonCancelableEventHandler<InputProps.ChangeDetail> = ({ detail: { value } }) => { | ||
setLabel(value); | ||
}; | ||
|
||
const saveOption = () => { | ||
if (label) { | ||
setOptions([...options, { label }]); | ||
setLabel(''); | ||
} | ||
}; | ||
|
||
const removeOption: NonCancelableEventHandler<TokenGroupProps.DismissDetail> = ({ detail: { itemIndex } }) => { | ||
setOptions([...options.slice(0, itemIndex), ...options.slice(itemIndex + 1)]); | ||
}; | ||
|
||
return ( | ||
<ExpandableSection headerText={inputSettings.title} defaultExpanded> | ||
<Grid gridDefinition={[{ colspan: 9 }, { colspan: 3 }]}> | ||
<Input | ||
value={label || ''} | ||
placeholder={inputSettings.optionPlaceholder} | ||
onChange={addOption} | ||
data-test-id='input-widget-option-input' | ||
/> | ||
<Button disabled={!label} onClick={saveOption} data-test-id='input-widget-add-option-btn'> | ||
{inputSettings.addOptionLabel} | ||
</Button> | ||
</Grid> | ||
<TokenGroup | ||
data-test-id='input-widget-token-list' | ||
alignment='vertical' | ||
onDismiss={removeOption} | ||
items={options} | ||
/> | ||
</ExpandableSection> | ||
); | ||
}; | ||
|
||
export default InputSettings; |
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
Oops, something went wrong.