diff --git a/packages/spectrum/src/additional/ListWithDetailMasterItem.tsx b/packages/spectrum/src/additional/ListWithDetailMasterItem.tsx
index 474033ec..42cf8963 100644
--- a/packages/spectrum/src/additional/ListWithDetailMasterItem.tsx
+++ b/packages/spectrum/src/additional/ListWithDetailMasterItem.tsx
@@ -27,7 +27,16 @@
THE SOFTWARE.
*/
import React from 'react';
-import { Text, Flex, ActionButton, View } from '@adobe/react-spectrum';
+import {
+ Text,
+ Flex,
+ ActionButton,
+ View,
+ DialogTrigger,
+ TooltipTrigger,
+ Tooltip,
+ AlertDialog,
+} from '@adobe/react-spectrum';
import { StatePropsOfMasterItem } from '@jsonforms/core';
import { withJsonFormsMasterListItemProps } from '@jsonforms/react';
import Delete from '@spectrum-icons/workflow/Delete';
@@ -62,12 +71,24 @@ const ListWithDetailMasterItem = ({
{childLabel}
-
-
-
+
+
+
+
+
+ Delete
+
+
+ Are you sure you wish to delete this item?
+
+
diff --git a/packages/spectrum/src/complex/array/SpectrumArrayControl.tsx b/packages/spectrum/src/complex/array/SpectrumArrayControl.tsx
index 2a6eead2..5b960848 100644
--- a/packages/spectrum/src/complex/array/SpectrumArrayControl.tsx
+++ b/packages/spectrum/src/complex/array/SpectrumArrayControl.tsx
@@ -26,15 +26,10 @@
THE SOFTWARE.
*/
import range from 'lodash/range';
-import React from 'react';
-import {
- ArrayControlProps,
- composePaths,
- createDefaultValue,
- findUISchema,
-} from '@jsonforms/core';
-import { ResolvedJsonFormsDispatch } from '@jsonforms/react';
+import React, { useCallback, useState } from 'react';
+import { ArrayControlProps, createDefaultValue } from '@jsonforms/core';
import { Button, Flex, Heading, Text, View } from '@adobe/react-spectrum';
+import SpectrumArrayItem from './SpectrumArrayItem';
export const SpectrumArrayControl = ({
data,
@@ -42,10 +37,25 @@ export const SpectrumArrayControl = ({
path,
schema,
addItem,
+ removeItems,
uischema,
uischemas,
renderers,
}: ArrayControlProps) => {
+ const handleRemoveItem = useCallback(
+ (p: string, value: any) => () => {
+ removeItems(p, [value])();
+ },
+ [removeItems]
+ );
+
+ const [expaned, setExpaned] = useState();
+
+ const isExpaned = (index: number) => expaned === index;
+
+ const onExpand = (index: number) => () =>
+ setExpaned((current) => (current === index ? null : index));
+
return (
@@ -58,31 +68,28 @@ export const SpectrumArrayControl = ({
+
-
- {data ? (
+
+ {data && data.length ? (
range(0, data.length).map((index) => {
- const foundUISchema = findUISchema(
- uischemas,
- schema,
- uischema.scope,
- path
- );
- const childPath = composePaths(path, `${index}`);
-
return (
-
+ key={index}
+ >
);
})
) : (
No data
)}
-
+
);
};
diff --git a/packages/spectrum/src/complex/array/SpectrumArrayItem.css b/packages/spectrum/src/complex/array/SpectrumArrayItem.css
new file mode 100644
index 00000000..ba997d3f
--- /dev/null
+++ b/packages/spectrum/src/complex/array/SpectrumArrayItem.css
@@ -0,0 +1,17 @@
+.spectrum-array-item-number {
+ --spectrum-array-item-number-size: 1.2rem;
+
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ width: var(--spectrum-array-item-number-size);
+ height: var(--spectrum-array-item-number-size);
+ border-radius: 50%;
+ overflow: hidden;
+ line-height: var(--spectrum-array-item-number-size);
+ font-size: 0.75em;
+ font-weight: 500;
+ text-align: center;
+ background-color: var(--spectrum-global-color-gray-500);
+ color: var(--spectrum-global-color-gray-200);
+}
diff --git a/packages/spectrum/src/complex/array/SpectrumArrayItem.tsx b/packages/spectrum/src/complex/array/SpectrumArrayItem.tsx
new file mode 100644
index 00000000..e356edb1
--- /dev/null
+++ b/packages/spectrum/src/complex/array/SpectrumArrayItem.tsx
@@ -0,0 +1,242 @@
+/*
+ The MIT License
+
+ Copyright (c) 2017-2019 EclipseSource Munich
+ https://github.com/eclipsesource/jsonforms
+
+ Copyright (c) 2020 headwire.com, Inc
+ https://github.com/headwirecom/jsonforms-react-spectrum-renderers
+
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+*/
+import React, { ComponentType } from 'react';
+import {
+ Text,
+ Flex,
+ ActionButton,
+ View,
+ DialogTrigger,
+ TooltipTrigger,
+ Tooltip,
+ AlertDialog,
+} from '@adobe/react-spectrum';
+import {
+ composePaths,
+ ControlElement,
+ findUISchema,
+ getData,
+ JsonFormsRendererRegistryEntry,
+ JsonFormsState,
+ JsonSchema,
+ Resolve,
+ UISchemaElement,
+ UISchemaTester,
+} from '@jsonforms/core';
+import {
+ areEqual,
+ JsonFormsStateContext,
+ ResolvedJsonFormsDispatch,
+ withJsonFormsContext,
+} from '@jsonforms/react';
+import Delete from '@spectrum-icons/workflow/Delete';
+import ChevronDown from '@spectrum-icons/workflow/ChevronDown';
+import ChevronUp from '@spectrum-icons/workflow/ChevronUp';
+import { find, omit } from 'lodash';
+
+import './SpectrumArrayItem.css';
+
+export interface OwnPropsOfSpectrumArrayItem {
+ index: number;
+ expanded: boolean;
+ path: string;
+ schema: JsonSchema;
+ handleExpand(index: number): () => void;
+ removeItem(path: string, value: number): () => void;
+ uischema: ControlElement;
+ renderers?: JsonFormsRendererRegistryEntry[];
+ uischemas?: {
+ tester: UISchemaTester;
+ uischema: UISchemaElement;
+ }[];
+}
+
+export interface StatePropsOfSpectrumArrayItem
+ extends OwnPropsOfSpectrumArrayItem {
+ childLabel: string;
+}
+
+const SpectrumArrayItem = ({
+ index,
+ childLabel,
+ expanded,
+ removeItem,
+ path,
+ handleExpand,
+ schema,
+ uischema,
+ uischemas,
+ renderers,
+}: StatePropsOfSpectrumArrayItem) => {
+ const foundUISchema = findUISchema(uischemas, schema, uischema.scope, path);
+ const childPath = composePaths(path, `${index}`);
+ return (
+
+
+
+
+ {index + 1}
+
+
+ {childLabel}
+
+
+
+ {expanded ? : }
+
+
+
+
+
+
+ Delete
+
+
+ Are you sure you wish to delete this item?
+
+
+
+
+
+ {expanded ? (
+
+
+
+ ) : (
+ ''
+ )}
+
+ );
+};
+
+/**
+ * Map state to control props.
+ * @param state the store's state
+ * @param ownProps any own props
+ * @returns {StatePropsOfControl} state props for a control
+ */
+export const mapStateToSpectrumArrayItemProps = (
+ state: JsonFormsState,
+ ownProps: OwnPropsOfSpectrumArrayItem
+): StatePropsOfSpectrumArrayItem => {
+ const { schema, path, index, uischema } = ownProps;
+ const firstPrimitiveProp = schema.properties
+ ? find(Object.keys(schema.properties), (propName: any) => {
+ const prop = schema.properties[propName];
+ return (
+ prop.type === 'string' ||
+ prop.type === 'number' ||
+ prop.type === 'integer'
+ );
+ })
+ : undefined;
+ const childPath = composePaths(path, `${index}`);
+ const childData = Resolve.data(getData(state), childPath);
+ const childLabel = uischema.options?.elementLabelProp
+ ? childData[uischema.options.elementLabelProp]
+ : firstPrimitiveProp
+ ? childData[firstPrimitiveProp]
+ : '';
+
+ return {
+ ...ownProps,
+ childLabel,
+ };
+};
+
+export const ctxToSpectrumArrayItemProps = (
+ ctx: JsonFormsStateContext,
+ ownProps: OwnPropsOfSpectrumArrayItem
+) => mapStateToSpectrumArrayItemProps({ jsonforms: { ...ctx } }, ownProps);
+
+const withContextToSpectrumArrayItemProps = (
+ Component: ComponentType
+): ComponentType => ({
+ ctx,
+ props,
+}: JsonFormsStateContext & StatePropsOfSpectrumArrayItem) => {
+ const stateProps = ctxToSpectrumArrayItemProps(ctx, props);
+ return ;
+};
+
+export const withJsonFormsSpectrumArrayItemProps = (
+ Component: ComponentType
+): ComponentType =>
+ withJsonFormsContext(
+ withContextToSpectrumArrayItemProps(
+ React.memo(
+ Component,
+ (
+ prevProps: StatePropsOfSpectrumArrayItem,
+ nextProps: StatePropsOfSpectrumArrayItem
+ ) =>
+ areEqual(
+ omit(prevProps, ['handleExpand', 'removeItem']),
+ omit(nextProps, ['handleExpand', 'removeItem'])
+ )
+ )
+ )
+ );
+
+export default withJsonFormsSpectrumArrayItemProps(SpectrumArrayItem);
diff --git a/packages/spectrum/src/complex/array/index.ts b/packages/spectrum/src/complex/array/index.ts
index b32c9126..40a9c642 100644
--- a/packages/spectrum/src/complex/array/index.ts
+++ b/packages/spectrum/src/complex/array/index.ts
@@ -36,7 +36,7 @@ import { SpectrumArrayControl } from './SpectrumArrayControl';
export { SpectrumArrayControl, ArrayControlRenderer };
export const arrayControlTester: RankedTester = rankWith(
- 2,
+ 4,
isObjectArrayWithNesting
);
diff --git a/packages/spectrum/test/renderers/ArrayControl.test.tsx b/packages/spectrum/test/renderers/ArrayControl.test.tsx
index 5ce3d459..c2e52a9b 100644
--- a/packages/spectrum/test/renderers/ArrayControl.test.tsx
+++ b/packages/spectrum/test/renderers/ArrayControl.test.tsx
@@ -1,22 +1,22 @@
/*
The MIT License
-
+
Copyright (c) 2017-2019 EclipseSource Munich
https://github.com/eclipsesource/jsonforms
-
+
Copyright (c) 2020 headwire.com, Inc
https://github.com/headwirecom/jsonforms-react-spectrum-renderers
-
+
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
-
+
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
-
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -25,71 +25,144 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
-import { JsonFormsReduxContext } from '@jsonforms/react';
-import * as React from 'react';
-import { Provider } from 'react-redux';
-import { ControlElement, JsonSchema } from '@jsonforms/core';
-import ArrayControl from '../../src/complex/array/SpectrumArrayControlRenderer';
-import { spectrumRenderers } from '../../src/index';
-import { initJsonFormsSpectrumStore } from '../spectrumStore';
-import SpectrumIntegerCell, {
- spectrumIntegerCellTester,
-} from '../../src/cells/SpectrumIntegerCell';
-
-import Adapter from 'enzyme-adapter-react-16';
-import Enzyme, { mount } from 'enzyme';
-
-Enzyme.configure({ adapter: new Adapter() });
-
-const fixture: { schema: JsonSchema; uischema: ControlElement; data: any } = {
+
+import { fireEvent } from '@testing-library/dom';
+import { clone } from 'lodash';
+import { renderForm } from '../util';
+
+const fixture = {
+ data: {
+ comments: [
+ {
+ message1: 'example',
+ message2: 'example2',
+ },
+ {
+ message1: 'booohay1',
+ message2: 'booohay2',
+ },
+ ],
+ },
schema: {
type: 'object',
properties: {
- test: {
+ comments: {
type: 'array',
+ title: 'Messages',
items: {
type: 'object',
properties: {
- x: { type: 'integer' },
- y: { type: 'integer' },
+ message1: {
+ type: 'string',
+ },
+ message2: {
+ type: 'string',
+ },
},
},
},
},
},
uischema: {
- type: 'Control',
- scope: '#/properties/test',
- },
- data: {
- test: [{ x: 1, y: 3 }],
+ type: 'VerticalLayout',
+ elements: [
+ {
+ type: 'Control',
+ scope: '#/properties/comments',
+ options: {
+ detail: {
+ type: 'VerticalLayout',
+ elements: [
+ {
+ type: 'Control',
+ scope: '#/properties/message1',
+ },
+ {
+ type: 'Control',
+ scope: '#/properties/message2',
+ },
+ ],
+ },
+ },
+ },
+ ],
},
};
-describe('Array control renderer', () => {
- test('render two children', () => {
- const store = initJsonFormsSpectrumStore({
- data: fixture.data,
- schema: fixture.schema,
- uischema: fixture.uischema,
- renderers: spectrumRenderers,
- cells: [
- {
- tester: spectrumIntegerCellTester,
- cell: SpectrumIntegerCell,
- },
- ],
+describe('SpectrumArrayControlRenderer', () => {
+ test('render', () => {
+ const { getByRole, getByText } = renderForm(
+ fixture.uischema,
+ fixture.schema,
+ fixture.data
+ );
+ const button1 = getByRole('button', { name: /delete-item-example/ });
+ const button2 = getByRole('button', { name: /delete-item-booohay1/ });
+ const heading1 = getByText('example');
+
+ expect(button1).toBeDefined();
+ expect(button2).toBeDefined();
+ expect(heading1).toBeDefined();
+ expect(heading1.tagName).toBe('SPAN');
+ });
+
+ test('expand details', () => {
+ const { getAllByRole, getByLabelText } = renderForm(
+ fixture.uischema,
+ fixture.schema,
+ fixture.data
+ );
+ const expandButtons = getAllByRole('button', {
+ name: /expand-item-example/,
});
- const wrapper = mount(
-
-
-
-
-
+
+ // form should not be displayed yet
+ expect(() => getByLabelText(/Message 1/)).toThrow();
+
+ expandButtons[0].click();
+
+ const input = getByLabelText(/Message 1/) as HTMLInputElement;
+
+ expect(input).toBeDefined();
+ expect(input.value).toEqual('example');
+ });
+
+ test('change heading label', () => {
+ const { getAllByRole, getByLabelText, getByText } = renderForm(
+ fixture.uischema,
+ fixture.schema,
+ fixture.data
);
+ const expandButtons = getAllByRole('button', {
+ name: /expand-item-example/,
+ });
+
+ expandButtons[0].click();
+
+ const input = getByLabelText(/Message 1/) as HTMLInputElement;
+
+ fireEvent.change(input, { target: { value: 'New Title' } });
+
+ const heading1 = getByText('New Title');
- const controls = wrapper.find('input');
+ expect(heading1).toBeDefined();
+ expect(heading1.tagName).toBe('SPAN');
+ expect(heading1.parentElement.tagName).toBe('BUTTON');
+ });
+
+ test('with elementLabelProp', () => {
+ const localUiSchema = clone(fixture.uischema);
+ (localUiSchema.elements[0].options as any).elementLabelProp = 'message2';
+
+ const { getByText } = renderForm(
+ fixture.uischema,
+ fixture.schema,
+ fixture.data
+ );
+ const heading1 = getByText('example2');
- expect(controls).toHaveLength(2);
+ expect(heading1).toBeDefined();
+ expect(heading1.tagName).toBe('SPAN');
+ expect(heading1.parentElement.tagName).toBe('BUTTON');
});
});
diff --git a/packages/spectrum/test/renderers/SpectrumListWithDetailRenderer.test.tsx b/packages/spectrum/test/renderers/SpectrumListWithDetailRenderer.test.tsx
index 7ab09d1a..2d99c1dd 100644
--- a/packages/spectrum/test/renderers/SpectrumListWithDetailRenderer.test.tsx
+++ b/packages/spectrum/test/renderers/SpectrumListWithDetailRenderer.test.tsx
@@ -202,6 +202,11 @@ describe('Spectrum List With Detail Renderer', () => {
const deleteYoloButton = getByRole('button', { name: /delete-item-Yolo/ });
userEvent.click(deleteYoloButton);
+
+ const confirmButton = getByRole('button', { name: 'Delete' });
+
+ userEvent.click(confirmButton);
+
expect(getAllByRole('button', { name: /select-item/ })).toHaveLength(1);
});
});