Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as React from "react"; import styled from "styled-components"; import { Box, Button, Checkbox, Flex, Input } from "rimble-ui"; import { JsonSchemaNode, nodeToTypeName } from "vc-schema-tools"; import { fonts, colors } from "../../../../themes"; import { NESTED_TYPE_KEY, typeOptions } from "../utils"; import { convertToCamelCase } from "../../../../utils"; import { DropDown } from "../../../elements/DropDown/DropDown"; import { newSchemaAttribute } from "../types"; const AttributeBox = styled(Box)` &:first-child { margin-top: 4px; } &:last-child { margin-bottom: 0; } `; const IconButton = styled(Button.Text)` color: ${colors.midGray}; &:hover { color: ${colors.primary.base}; } `; export interface SchemaAttributeProps { attr: Partial<JsonSchemaNode>; attrName?: string; /** For the credential subject attribute we only want to display the nested attributes. */ isCredSubject?: boolean; parentRequired?: string[]; readOnly?: boolean; setParentRequired?(required: string[]): void; updateAttribute?(attribute: Partial<JsonSchemaNode>): void; removeAttribute?(): void; } export const SchemaAttribute: React.FunctionComponent<SchemaAttributeProps> = (props) => { const { attr, attrName, isCredSubject, parentRequired, readOnly, setParentRequired, updateAttribute, removeAttribute, } = props; function updateAttrProperty(attrProperty: keyof JsonSchemaNode, value: any) { const updatedProperty = { ...attr, [attrProperty]: value }; if (attrProperty === "title") { updatedProperty.$linkedData = { "@id": updatedProperty.$linkedData?.["@id"] || "", term: convertToCamelCase(value), }; } updateAttribute?.(updatedProperty); } function toggleRequired() { const term = attr.$linkedData?.term || ""; if (parentRequired?.includes(term)) { const requiredI = parentRequired.indexOf(term); setParentRequired?.([...parentRequired.slice(0, requiredI), ...parentRequired.slice(requiredI + 1)]); } else { setParentRequired?.([...(parentRequired || []), term]); } } function updateType(type: string) { const newType = typeOptions[type]; const updatedAttr = { ...attr, ...newType, $linkedData: { term: attr.$linkedData?.term || "", "@id": typeOptions[type].$linkedData?.["@id"] || "", }, }; if (!newType.format) { delete updatedAttr.format; } if (type === NESTED_TYPE_KEY) { updatedAttr.$linkedData["@id"] = updatedAttr.$linkedData.term; } else if (type !== NESTED_TYPE_KEY) { delete updatedAttr.properties; } updateAttribute?.(updatedAttr); } function addNestedAttribute(e: Event) { if (attr.properties?.[""]) { // Do nothing, an un-preventDefaulted event will trigger browser UI on empty required title field return; } e.preventDefault(); updateAttrProperty("properties", { ...attr.properties, "": { ...newSchemaAttribute, }, }); } const updateNestedAttribute = (key: string, updatedAttr: Partial<JsonSchemaNode>) => { let updatedProperties: { [key: string]: Partial<JsonSchemaNode> } = {}; let updatedRequired = attr.required || []; if (updatedAttr.$linkedData?.term !== key) { // Key has changed. Since nested attributes are listed by iterating over object keys, if we rename object key by deleting and adding, React will reorder inputs while user is typing and lose focus. So here we re-create object key by key in order to rename the updated key while preserving order. (Yes order isn't necessarily stable in JS objects but it's better than nothing and converting everything to use Maps would be a pain.) const oldKey = key; let newKey = updatedAttr.$linkedData?.term || ""; if (attr.properties && newKey in attr.properties) { // Edge case but will clobber existing property with that key so let's give it a different one. newKey = `${newKey}2`; } Object.keys(attr.properties || {}).forEach((existingKey) => { if (existingKey === oldKey) { updatedProperties[newKey] = updatedAttr; } else { updatedProperties[existingKey] = attr.properties![existingKey]; } }); if (attr.required?.includes(oldKey)) { // We need to update this too when the key changes const requiredI = attr.required.indexOf(oldKey); updatedRequired = [...attr.required.slice(0, requiredI), ...attr.required.slice(requiredI + 1), newKey]; } } else { updatedProperties = { ...attr.properties, [key]: updatedAttr, }; } updateAttribute?.({ ...attr, properties: updatedProperties, required: updatedRequired, }); }; const removeNestedAttribute = (key: string) => { const updatedProperties = { ...attr.properties }; delete updatedProperties[key]; let required = attr.required || []; const requiredI = required.indexOf(key); if (requiredI !== -1) { required = [...required.slice(0, requiredI), ...required.slice(requiredI + 1)]; } updateAttribute?.({ ...attr, properties: updatedProperties, required, }); if (!Object.keys(updatedProperties).length) { if (!isCredSubject) { // No nested attributes left so delete the parent one. Maybe a weird flow but certainly simpler than either allowing empty nested attributes or performing a recursive check for empty nested attributes before letting user continue. removeAttribute?.(); } else { // Gotta leave at least one in updateAttrProperty("properties", { "": { ...newSchemaAttribute } }); } } }; function renderNestedAttributes(): JSX.Element { return ( <> {Object.entries(attr.properties || {}).map(([key, node], i) => { return ( <SchemaAttribute key={i} attrName={key} attr={node} updateAttribute={(attr) => updateNestedAttribute(key, attr)} removeAttribute={() => removeNestedAttribute(key)} parentRequired={attr.required} setParentRequired={(required) => updateAttrProperty("required", required)} /> ); })} </> ); } if (isCredSubject) { return renderNestedAttributes(); } const currentTypeValue = attr.type === "object" ? NESTED_TYPE_KEY : nodeToTypeName(attr); return ( <AttributeBox border={1} borderRadius={1} p={3} pb={readOnly ? 3 : 1} my={3} backgroundColor={readOnly ? colors.nearWhite : "transparent"} > <Flex> <Input width="100%" type="text" required={true} placeholder="Attribute Name" mr={2} value={attr.title || attr.$linkedData?.term || attrName || ""} onChange={(event: any) => updateAttrProperty("title", event.target.value)} disabled={readOnly} style={{ borderColor: colors.lightGray }} /> {currentTypeValue && currentTypeValue in typeOptions ? ( <DropDown onChange={(value) => updateType(value)} defaultSelectedValue={currentTypeValue} disabled={readOnly} options={Object.keys(typeOptions).map((typeName) => ({ name: typeName === NESTED_TYPE_KEY ? "[nested properties]" : typeName, value: typeName, }))} style={{ borderColor: colors.lightGray }} /> ) : ( <Box width="100%" maxWidth="50%" pt={1} px={2} style={{ overflow: "hidden", textOverflow: "ellipsis" }} title={currentTypeValue} > {currentTypeValue} </Box> )} {/* @TODO/tobek Add "custom" option that opens fields to manually enter type details. */} </Flex> {!readOnly && ( <Input width="100%" placeholder="Description" value={attr.description || ""} style={{ borderColor: colors.lightGray }} onChange={(event: any) => updateAttrProperty("description", event.target.value)} /> )} {attr.type === "object" && <Box mt={3}>{renderNestedAttributes()}</Box>} <Flex justifyContent="space-between"> <Checkbox fontFamily={fonts.sansSerif} label="Required" checked={attr.$linkedData?.term && parentRequired?.includes(attr.$linkedData?.term)} onChange={toggleRequired} disabled={readOnly} mt={readOnly ? 2 : 0} /> {attr.type === "object" && ( <Button.Text onClick={addNestedAttribute} fontSize={1}> Add Nested Attribute </Button.Text> )} {!readOnly && ( <IconButton icononly icon="DeleteForever" onClick={(e: Event) => { e.preventDefault(); removeAttribute?.(); }} /> )} </Flex> </AttributeBox> ); }; |