|
| 1 | +import { ITreeListNode, TreeStore } from '@stoplight/tree-list'; |
| 2 | +import { Omit } from '@stoplight/types'; |
| 3 | +import { Button, Checkbox, Icon } from '@stoplight/ui-kit'; |
| 4 | +import * as cn from 'classnames'; |
| 5 | +import * as pluralize from 'pluralize'; |
| 6 | +import * as React from 'react'; |
| 7 | + |
| 8 | +import { IMasking, SchemaNodeWithMeta } from '../types'; |
| 9 | +import { formatRef, isCombiner, isRef, pathToString } from '../utils'; |
| 10 | +import { Divider, Types } from './'; |
| 11 | + |
| 12 | +export interface ISchemaRow extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onClick' | 'onSelect'>, IMasking { |
| 13 | + node: ITreeListNode<object>; |
| 14 | + onMaskEdit(node: SchemaNodeWithMeta): void; |
| 15 | + treeStore: TreeStore; |
| 16 | +} |
| 17 | + |
| 18 | +export const SchemaRow: React.FunctionComponent<ISchemaRow> = ({ |
| 19 | + node, |
| 20 | + treeStore, |
| 21 | + canSelect, |
| 22 | + onSelect, |
| 23 | + onMaskEdit, |
| 24 | + selected, |
| 25 | +}) => { |
| 26 | + const schemaNode = node.metadata as SchemaNodeWithMeta; |
| 27 | + const { showDivider, name, $ref, subtype, required, path, inheritedFrom } = schemaNode; |
| 28 | + |
| 29 | + const handleChange = React.useCallback( |
| 30 | + () => { |
| 31 | + if (onSelect !== undefined) { |
| 32 | + onSelect(pathToString(path)); |
| 33 | + } |
| 34 | + }, |
| 35 | + [onSelect] |
| 36 | + ); |
| 37 | + |
| 38 | + const handleEditMask = React.useCallback<React.MouseEventHandler<HTMLButtonElement>>( |
| 39 | + e => { |
| 40 | + e.stopPropagation(); |
| 41 | + onMaskEdit(schemaNode); |
| 42 | + }, |
| 43 | + [onMaskEdit] |
| 44 | + ); |
| 45 | + |
| 46 | + const type = isRef(schemaNode) ? '$ref' : isCombiner(schemaNode) ? schemaNode.combiner : schemaNode.type; |
| 47 | + const description = 'annotations' in schemaNode && schemaNode.annotations.description; |
| 48 | + |
| 49 | + const validationCount = 'validations' in schemaNode ? Object.keys(schemaNode.validations).length : 0; |
| 50 | + |
| 51 | + return ( |
| 52 | + <div className="flex flex-1 items-center text-sm leading-tight relative select-none mr-3"> |
| 53 | + {showDivider && <Divider>or</Divider>} |
| 54 | + |
| 55 | + <div className="flex-1 truncate"> |
| 56 | + <div className="flex items-baseline"> |
| 57 | + {name && <span className="mr-3">{name}</span>} |
| 58 | + |
| 59 | + <Types type={type} subtype={subtype}> |
| 60 | + {type === '$ref' ? `[${$ref}]` : null} |
| 61 | + </Types> |
| 62 | + |
| 63 | + {inheritedFrom ? ( |
| 64 | + <> |
| 65 | + <span className="text-darken-7 mx-2">{`{${formatRef(inheritedFrom)}}`}</span> |
| 66 | + {onMaskEdit !== undefined && <span onClick={handleEditMask}>(edit mask)</span>} |
| 67 | + </> |
| 68 | + ) : null} |
| 69 | + </div> |
| 70 | + |
| 71 | + {description && <span className="text-darken-7 text-xs">{description}</span>} |
| 72 | + </div> |
| 73 | + |
| 74 | + {(canSelect || validationCount || required) && ( |
| 75 | + <div className="items-center text-right ml-auto text-xs"> |
| 76 | + {canSelect ? ( |
| 77 | + <Checkbox onChange={handleChange} checked={selected && selected.includes(pathToString(path))} /> |
| 78 | + ) : ( |
| 79 | + <> |
| 80 | + {validationCount ? ( |
| 81 | + <span className="mr-2 text-darken-7"> |
| 82 | + {validationCount} {pluralize('validation', validationCount)} |
| 83 | + </span> |
| 84 | + ) : null} |
| 85 | + |
| 86 | + {required && <span className="font-semibold">required</span>} |
| 87 | + </> |
| 88 | + )} |
| 89 | + </div> |
| 90 | + )} |
| 91 | + |
| 92 | + {(validationCount || description) && |
| 93 | + node.canHaveChildren && ( |
| 94 | + <Button |
| 95 | + small |
| 96 | + className={cn(required && 'ml-2')} |
| 97 | + id={`${node.id}-showMore`} |
| 98 | + icon={<Icon icon="info-sign" className="opacity-75" iconSize={12} />} |
| 99 | + onClick={(e: React.MouseEvent) => { |
| 100 | + e.stopPropagation(); |
| 101 | + treeStore.setActiveNode(node.id); |
| 102 | + }} |
| 103 | + /> |
| 104 | + )} |
| 105 | + </div> |
| 106 | + ); |
| 107 | +}; |
0 commit comments