-
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(scene): add ground plane button
- Loading branch information
Showing
33 changed files
with
1,140 additions
and
8 deletions.
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
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
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
109 changes: 109 additions & 0 deletions
109
...ene-composer/src/components/panels/scene-components/PlaneGeometryComponentEditor.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,109 @@ | ||
import wrapper from '@awsui/components-react/test-utils/dom'; | ||
import { render } from '@testing-library/react'; | ||
import React from 'react'; | ||
|
||
import { IPlaneGeometryComponentInternal, useStore } from '../../../store'; | ||
import { KnownComponentType } from '../../../interfaces'; | ||
import { mockNode, mockComponent } from '../../../../tests/components/panels/scene-components/MockComponents'; | ||
|
||
import { PlaneGeometryComponentEditor } from './PlaneGeometryComponentEditor'; | ||
|
||
jest.mock('@awsui/components-react', () => ({ | ||
...jest.requireActual('@awsui/components-react'), | ||
})); | ||
|
||
describe('PlaneGeometryComponentEditor', () => { | ||
const component: IPlaneGeometryComponentInternal = { | ||
...mockComponent, | ||
type: KnownComponentType.PlaneGeometry, | ||
width: 10, | ||
height: 20, | ||
}; | ||
|
||
const componentWithColor: IPlaneGeometryComponentInternal = { | ||
...mockComponent, | ||
type: KnownComponentType.PlaneGeometry, | ||
width: 10, | ||
height: 20, | ||
color: '#abcdef', | ||
}; | ||
|
||
const updateComponentInternalFn = jest.fn(); | ||
|
||
const baseState = { | ||
updateComponentInternal: updateComponentInternalFn, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should update width when width changes', async () => { | ||
useStore('default').setState(baseState); | ||
const { container } = render( | ||
<PlaneGeometryComponentEditor node={{ ...mockNode, components: [component] }} component={component} />, | ||
); | ||
const polarisWrapper = wrapper(container); | ||
const widthInput = polarisWrapper.findInput('[data-testid="plane-width-input"]'); | ||
|
||
expect(widthInput).toBeDefined(); | ||
|
||
widthInput!.focus(); | ||
widthInput!.setInputValue('2'); | ||
widthInput!.blur(); | ||
|
||
expect(updateComponentInternalFn).toBeCalledTimes(1); | ||
expect(updateComponentInternalFn).toBeCalledWith( | ||
mockNode.ref, | ||
{ ...component, ref: component.ref, width: 2, height: component.height }, | ||
true, | ||
); | ||
}); | ||
|
||
it('should update height when height changes', async () => { | ||
useStore('default').setState(baseState); | ||
const { container } = render( | ||
<PlaneGeometryComponentEditor node={{ ...mockNode, components: [component] }} component={component} />, | ||
); | ||
const polarisWrapper = wrapper(container); | ||
const heightInput = polarisWrapper.findInput('[data-testid="plane-height-input"]'); | ||
|
||
expect(heightInput).toBeDefined(); | ||
|
||
heightInput!.focus(); | ||
heightInput!.setInputValue('2'); | ||
heightInput!.blur(); | ||
|
||
expect(updateComponentInternalFn).toBeCalledTimes(1); | ||
expect(updateComponentInternalFn).toBeCalledWith( | ||
mockNode.ref, | ||
{ ...component, ref: component.ref, width: component.width, height: 2 }, | ||
true, | ||
); | ||
}); | ||
|
||
it('should update background when colors changes', async () => { | ||
useStore('default').setState(baseState); | ||
const { container } = render( | ||
<PlaneGeometryComponentEditor | ||
node={{ ...mockNode, components: [componentWithColor] }} | ||
component={componentWithColor} | ||
/>, | ||
); | ||
const polarisWrapper = wrapper(container); | ||
const colorInput = polarisWrapper.findInput('[data-testid="hexcode"]'); | ||
|
||
expect(colorInput).toBeDefined(); | ||
|
||
// click checkbox should update store | ||
colorInput!.focus(); | ||
colorInput!.setInputValue('#FFFFFF'); | ||
colorInput!.blur(); | ||
expect(updateComponentInternalFn).toBeCalledTimes(1); | ||
expect(updateComponentInternalFn).toBeCalledWith( | ||
mockNode.ref, | ||
{ ...component, ref: component.ref, width: component.width, height: component.height, color: '#FFFFFF' }, | ||
true, | ||
); | ||
}); | ||
}); |
113 changes: 113 additions & 0 deletions
113
...es/scene-composer/src/components/panels/scene-components/PlaneGeometryComponentEditor.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,113 @@ | ||
import React, { useCallback, useContext, useState } from 'react'; | ||
import { FormField, Input, SpaceBetween } from '@awsui/components-react'; | ||
import { useIntl } from 'react-intl'; | ||
|
||
import { IComponentEditorProps } from '../ComponentEditor'; | ||
import { KnownSceneProperty } from '../../../interfaces'; | ||
import { IPlaneGeometryComponentInternal, ISceneComponentInternal, useStore } from '../../../store'; | ||
import { sceneComposerIdContext } from '../../../common/sceneComposerIdContext'; | ||
import { ColorSelectorCombo } from '../scene-components/tag-style/ColorSelectorCombo/ColorSelectorCombo'; | ||
|
||
export type IPlaneGeometryComponentEditorProps = IComponentEditorProps; | ||
|
||
export const PlaneGeometryComponentEditor: React.FC<IPlaneGeometryComponentEditorProps> = ({ | ||
node, | ||
component, | ||
}: IPlaneGeometryComponentEditorProps) => { | ||
const sceneComposerId = useContext(sceneComposerIdContext); | ||
const updateComponentInternal = useStore(sceneComposerId)((state) => state.updateComponentInternal); | ||
const planeGeometryComponent = component as IPlaneGeometryComponentInternal; | ||
const intl = useIntl(); | ||
|
||
const geometryColors = useStore(sceneComposerId)((state) => | ||
state.getSceneProperty<string[]>(KnownSceneProperty.GeometryCustomColors, []), | ||
); | ||
const setGeometryColorsSceneProperty = useStore(sceneComposerId)((state) => state.setSceneProperty<string[]>); | ||
|
||
const [internalWidth, setInternalWidth] = useState(planeGeometryComponent.width); | ||
const [internalHeight, setInternalHeight] = useState(planeGeometryComponent.height); | ||
const [internalColor, setInternalColor] = useState(planeGeometryComponent.color || '#cccccc'); | ||
|
||
const onUpdateCallback = useCallback( | ||
(componentPartial: IPlaneGeometryComponentInternal, replace?: boolean) => { | ||
const componentPartialWithRef: ISceneComponentInternal = { ...componentPartial, ref: component.ref }; | ||
updateComponentInternal(node.ref, componentPartialWithRef, replace); | ||
}, | ||
[node.ref, component.ref], | ||
); | ||
|
||
const onColorChange = useCallback( | ||
(color: string) => { | ||
if (color !== internalColor) { | ||
setInternalColor(color); | ||
const updatedComponent = { ...planeGeometryComponent, color }; | ||
onUpdateCallback(updatedComponent, true); | ||
} | ||
}, | ||
[internalColor, planeGeometryComponent], | ||
); | ||
|
||
const onInputBlur = useCallback(() => { | ||
const updatedComponent = { ...planeGeometryComponent, width: internalWidth, height: internalHeight }; | ||
onUpdateCallback(updatedComponent, true); | ||
}, [planeGeometryComponent, internalWidth, internalHeight]); | ||
|
||
const onWidthChange = useCallback( | ||
(event) => { | ||
const value = Number(event.detail.value); | ||
if (value !== internalWidth) { | ||
setInternalWidth(value); | ||
} | ||
}, | ||
[internalWidth], | ||
); | ||
|
||
const onHeightChange = useCallback( | ||
(event) => { | ||
const value = Number(event.detail.value); | ||
if (value !== internalHeight) { | ||
setInternalHeight(value); | ||
} | ||
}, | ||
[internalHeight], | ||
); | ||
|
||
return ( | ||
<SpaceBetween size='s'> | ||
<FormField label={intl.formatMessage({ defaultMessage: 'Width', description: 'Form Field label' })}> | ||
<Input | ||
data-testid='plane-width-input' | ||
value={String(internalWidth)} | ||
type='number' | ||
onBlur={onInputBlur} | ||
onChange={onWidthChange} | ||
onKeyDown={(e) => { | ||
if (e.detail.key === 'Enter') onInputBlur(); | ||
}} | ||
/> | ||
</FormField> | ||
<FormField label={intl.formatMessage({ defaultMessage: 'Height', description: 'Form Field label' })}> | ||
<Input | ||
data-testid='plane-height-input' | ||
value={String(internalHeight)} | ||
type='number' | ||
onBlur={onInputBlur} | ||
onChange={onHeightChange} | ||
onKeyDown={(e) => { | ||
if (e.detail.key === 'Enter') onInputBlur(); | ||
}} | ||
/> | ||
</FormField> | ||
<ColorSelectorCombo | ||
color={internalColor} | ||
onSelectColor={(pickedColor) => onColorChange(pickedColor)} | ||
onUpdateCustomColors={(chosenCustomColors) => | ||
setGeometryColorsSceneProperty(KnownSceneProperty.GeometryCustomColors, chosenCustomColors) | ||
} | ||
customColors={geometryColors} | ||
colorPickerLabel={intl.formatMessage({ defaultMessage: 'Color', description: 'Color' })} | ||
customColorLabel={intl.formatMessage({ defaultMessage: 'Custom colors', description: 'Custom colors' })} | ||
/> | ||
</SpaceBetween> | ||
); | ||
}; |
62 changes: 62 additions & 0 deletions
62
...composer/src/components/panels/scene-components/PlaneGeometryComponentEditorSnap.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,62 @@ | ||
import { render } from '@testing-library/react'; | ||
import React from 'react'; | ||
|
||
import { IPlaneGeometryComponentInternal, useStore } from '../../../store'; | ||
import { KnownComponentType } from '../../../interfaces'; | ||
import { mockNode, mockComponent } from '../../../../tests/components/panels/scene-components/MockComponents'; | ||
|
||
import { PlaneGeometryComponentEditor } from './PlaneGeometryComponentEditor'; | ||
|
||
jest.mock('../scene-components/tag-style/ColorSelectorCombo/ColorSelectorCombo', () => { | ||
return { | ||
ColorSelectorCombo: (...props: []) => <div id='ColorSelectorCombo'>{JSON.stringify(props)}</div>, | ||
}; | ||
}); | ||
|
||
describe('PlaneGeometryComponentEditor', () => { | ||
const component: IPlaneGeometryComponentInternal = { | ||
...mockComponent, | ||
type: KnownComponentType.PlaneGeometry, | ||
width: 10, | ||
height: 20, | ||
}; | ||
|
||
const componentWithColor: IPlaneGeometryComponentInternal = { | ||
...mockComponent, | ||
type: KnownComponentType.PlaneGeometry, | ||
width: 10, | ||
height: 20, | ||
color: '#abcdef', | ||
}; | ||
|
||
const updateComponentInternalFn = jest.fn(); | ||
|
||
const baseState = { | ||
updateComponentInternal: updateComponentInternalFn, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should render correctly', () => { | ||
useStore('default').setState(baseState); | ||
|
||
const { container } = render( | ||
<PlaneGeometryComponentEditor node={{ ...mockNode, components: [component] }} component={component} />, | ||
); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render correctly with color', () => { | ||
useStore('default').setState(baseState); | ||
|
||
const { container } = render( | ||
<PlaneGeometryComponentEditor | ||
node={{ ...mockNode, components: [componentWithColor] }} | ||
component={componentWithColor} | ||
/>, | ||
); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
}); |
Oops, something went wrong.