|
| 1 | +/* |
| 2 | + The MIT License |
| 3 | +
|
| 4 | + Copyright (c) 2017-2019 EclipseSource Munich |
| 5 | + https://github.com/eclipsesource/jsonforms |
| 6 | +
|
| 7 | + Copyright (c) 2020 headwire.com, Inc |
| 8 | + https://github.com/headwirecom/jsonforms-react-spectrum-renderers |
| 9 | +
|
| 10 | +
|
| 11 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | + of this software and associated documentation files (the "Software"), to deal |
| 13 | + in the Software without restriction, including without limitation the rights |
| 14 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | + copies of the Software, and to permit persons to whom the Software is |
| 16 | + furnished to do so, subject to the following conditions: |
| 17 | +
|
| 18 | + The above copyright notice and this permission notice shall be included in |
| 19 | + all copies or substantial portions of the Software. |
| 20 | +
|
| 21 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | + THE SOFTWARE. |
| 28 | +*/ |
| 29 | +import React, { ComponentType } from 'react'; |
| 30 | +import { |
| 31 | + Text, |
| 32 | + Flex, |
| 33 | + ActionButton, |
| 34 | + View, |
| 35 | + DialogTrigger, |
| 36 | + TooltipTrigger, |
| 37 | + Tooltip, |
| 38 | + AlertDialog, |
| 39 | +} from '@adobe/react-spectrum'; |
| 40 | +import { |
| 41 | + composePaths, |
| 42 | + ControlElement, |
| 43 | + findUISchema, |
| 44 | + getData, |
| 45 | + JsonFormsRendererRegistryEntry, |
| 46 | + JsonFormsState, |
| 47 | + JsonSchema, |
| 48 | + Resolve, |
| 49 | + UISchemaElement, |
| 50 | + UISchemaTester, |
| 51 | +} from '@jsonforms/core'; |
| 52 | +import { |
| 53 | + areEqual, |
| 54 | + JsonFormsStateContext, |
| 55 | + ResolvedJsonFormsDispatch, |
| 56 | + withJsonFormsContext, |
| 57 | +} from '@jsonforms/react'; |
| 58 | +import Delete from '@spectrum-icons/workflow/Delete'; |
| 59 | +import ChevronDown from '@spectrum-icons/workflow/ChevronDown'; |
| 60 | +import ChevronUp from '@spectrum-icons/workflow/ChevronUp'; |
| 61 | +import { find, omit } from 'lodash'; |
| 62 | + |
| 63 | +import './SpectrumArrayItem.css'; |
| 64 | + |
| 65 | +export interface OwnPropsOfSpectrumArrayItem { |
| 66 | + index: number; |
| 67 | + expanded: boolean; |
| 68 | + path: string; |
| 69 | + schema: JsonSchema; |
| 70 | + handleExpand(index: number): () => void; |
| 71 | + removeItem(path: string, value: number): () => void; |
| 72 | + uischema: ControlElement; |
| 73 | + renderers?: JsonFormsRendererRegistryEntry[]; |
| 74 | + uischemas?: { |
| 75 | + tester: UISchemaTester; |
| 76 | + uischema: UISchemaElement; |
| 77 | + }[]; |
| 78 | +} |
| 79 | + |
| 80 | +export interface StatePropsOfSpectrumArrayItem |
| 81 | + extends OwnPropsOfSpectrumArrayItem { |
| 82 | + childLabel: string; |
| 83 | +} |
| 84 | + |
| 85 | +const SpectrumArrayItem = ({ |
| 86 | + index, |
| 87 | + childLabel, |
| 88 | + expanded, |
| 89 | + removeItem, |
| 90 | + path, |
| 91 | + handleExpand, |
| 92 | + schema, |
| 93 | + uischema, |
| 94 | + uischemas, |
| 95 | + renderers, |
| 96 | +}: StatePropsOfSpectrumArrayItem) => { |
| 97 | + const foundUISchema = findUISchema(uischemas, schema, uischema.scope, path); |
| 98 | + const childPath = composePaths(path, `${index}`); |
| 99 | + return ( |
| 100 | + <View |
| 101 | + borderWidth='thin' |
| 102 | + borderColor='dark' |
| 103 | + borderRadius='medium' |
| 104 | + padding='size-250' |
| 105 | + > |
| 106 | + <View aria-selected={expanded}> |
| 107 | + <Flex |
| 108 | + direction='row' |
| 109 | + margin='size-50' |
| 110 | + justifyContent='space-between' |
| 111 | + alignItems='center' |
| 112 | + > |
| 113 | + <View UNSAFE_className='spectrum-array-item-number'> |
| 114 | + <Text>{index + 1}</Text> |
| 115 | + </View> |
| 116 | + <ActionButton |
| 117 | + flex='auto' |
| 118 | + isQuiet |
| 119 | + onPress={handleExpand(index)} |
| 120 | + aria-label={`expand-item-${childLabel}`} |
| 121 | + > |
| 122 | + <Text UNSAFE_style={{ textAlign: 'left' }}>{childLabel}</Text> |
| 123 | + </ActionButton> |
| 124 | + <View> |
| 125 | + <ActionButton |
| 126 | + onPress={handleExpand(index)} |
| 127 | + isQuiet={true} |
| 128 | + aria-label={`expand-item-${childLabel}`} |
| 129 | + > |
| 130 | + {expanded ? <ChevronUp /> : <ChevronDown />} |
| 131 | + </ActionButton> |
| 132 | + <DialogTrigger> |
| 133 | + <TooltipTrigger delay={0}> |
| 134 | + <ActionButton |
| 135 | + isQuiet={true} |
| 136 | + aria-label={`delete-item-${childLabel}`} |
| 137 | + > |
| 138 | + <Delete /> |
| 139 | + </ActionButton> |
| 140 | + <Tooltip>Delete</Tooltip> |
| 141 | + </TooltipTrigger> |
| 142 | + <AlertDialog |
| 143 | + variant='confirmation' |
| 144 | + title='Delete' |
| 145 | + primaryActionLabel='Delete' |
| 146 | + cancelLabel='Cancel' |
| 147 | + autoFocusButton='primary' |
| 148 | + onPrimaryAction={removeItem(path, index)} |
| 149 | + > |
| 150 | + Are you sure you wish to delete this item? |
| 151 | + </AlertDialog> |
| 152 | + </DialogTrigger> |
| 153 | + </View> |
| 154 | + </Flex> |
| 155 | + </View> |
| 156 | + {expanded ? ( |
| 157 | + <View> |
| 158 | + <ResolvedJsonFormsDispatch |
| 159 | + schema={schema} |
| 160 | + uischema={foundUISchema || uischema} |
| 161 | + path={childPath} |
| 162 | + key={childPath} |
| 163 | + renderers={renderers} |
| 164 | + /> |
| 165 | + </View> |
| 166 | + ) : ( |
| 167 | + '' |
| 168 | + )} |
| 169 | + </View> |
| 170 | + ); |
| 171 | +}; |
| 172 | + |
| 173 | +/** |
| 174 | + * Map state to control props. |
| 175 | + * @param state the store's state |
| 176 | + * @param ownProps any own props |
| 177 | + * @returns {StatePropsOfControl} state props for a control |
| 178 | + */ |
| 179 | +export const mapStateToSpectrumArrayItemProps = ( |
| 180 | + state: JsonFormsState, |
| 181 | + ownProps: OwnPropsOfSpectrumArrayItem |
| 182 | +): StatePropsOfSpectrumArrayItem => { |
| 183 | + const { schema, path, index, uischema } = ownProps; |
| 184 | + const firstPrimitiveProp = schema.properties |
| 185 | + ? find(Object.keys(schema.properties), (propName: any) => { |
| 186 | + const prop = schema.properties[propName]; |
| 187 | + return ( |
| 188 | + prop.type === 'string' || |
| 189 | + prop.type === 'number' || |
| 190 | + prop.type === 'integer' |
| 191 | + ); |
| 192 | + }) |
| 193 | + : undefined; |
| 194 | + const childPath = composePaths(path, `${index}`); |
| 195 | + const childData = Resolve.data(getData(state), childPath); |
| 196 | + const childLabel = uischema.options?.elementLabelProp |
| 197 | + ? childData[uischema.options.elementLabelProp] |
| 198 | + : firstPrimitiveProp |
| 199 | + ? childData[firstPrimitiveProp] |
| 200 | + : ''; |
| 201 | + |
| 202 | + return { |
| 203 | + ...ownProps, |
| 204 | + childLabel, |
| 205 | + }; |
| 206 | +}; |
| 207 | + |
| 208 | +export const ctxToSpectrumArrayItemProps = ( |
| 209 | + ctx: JsonFormsStateContext, |
| 210 | + ownProps: OwnPropsOfSpectrumArrayItem |
| 211 | +) => mapStateToSpectrumArrayItemProps({ jsonforms: { ...ctx } }, ownProps); |
| 212 | + |
| 213 | +const withContextToSpectrumArrayItemProps = ( |
| 214 | + Component: ComponentType<StatePropsOfSpectrumArrayItem> |
| 215 | +): ComponentType<OwnPropsOfSpectrumArrayItem> => ({ |
| 216 | + ctx, |
| 217 | + props, |
| 218 | +}: JsonFormsStateContext & StatePropsOfSpectrumArrayItem) => { |
| 219 | + const stateProps = ctxToSpectrumArrayItemProps(ctx, props); |
| 220 | + return <Component {...stateProps} />; |
| 221 | +}; |
| 222 | + |
| 223 | +export const withJsonFormsSpectrumArrayItemProps = ( |
| 224 | + Component: ComponentType<StatePropsOfSpectrumArrayItem> |
| 225 | +): ComponentType<OwnPropsOfSpectrumArrayItem> => |
| 226 | + withJsonFormsContext( |
| 227 | + withContextToSpectrumArrayItemProps( |
| 228 | + React.memo( |
| 229 | + Component, |
| 230 | + ( |
| 231 | + prevProps: StatePropsOfSpectrumArrayItem, |
| 232 | + nextProps: StatePropsOfSpectrumArrayItem |
| 233 | + ) => |
| 234 | + areEqual( |
| 235 | + omit(prevProps, ['handleExpand', 'removeItem']), |
| 236 | + omit(nextProps, ['handleExpand', 'removeItem']) |
| 237 | + ) |
| 238 | + ) |
| 239 | + ) |
| 240 | + ); |
| 241 | + |
| 242 | +export default withJsonFormsSpectrumArrayItemProps(SpectrumArrayItem); |
0 commit comments