| 1 |
/* ************************************************************************ */ |
| 2 |
/* */ |
| 3 |
/* This file is part of Frama-C. */ |
| 4 |
/* */ |
| 5 |
/* Copyright (C) 2007-2022 */ |
| 6 |
/* CEA (Commissariat à l'énergie atomique et aux énergies */ |
| 7 |
/* alternatives) */ |
| 8 |
/* */ |
| 9 |
/* you can redistribute it and/or modify it under the terms of the GNU */ |
| 10 |
/* Lesser General Public License as published by the Free Software */ |
| 11 |
/* Foundation, version 2.1. */ |
| 12 |
/* */ |
| 13 |
/* It is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU Lesser General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* See the GNU Lesser General Public License version 2.1 */ |
| 19 |
/* for more details (enclosed in the file licenses/LGPLv2.1). */ |
| 20 |
/* */ |
| 21 |
/* ************************************************************************ */ |
| 22 |
|
| 23 |
// -------------------------------------------------------------------------- |
| 24 |
// --- Labels |
| 25 |
// -------------------------------------------------------------------------- |
| 26 |
|
| 27 |
/** |
| 28 |
@packageDocumentation |
| 29 |
@module dome/controls/labels |
| 30 |
*/ |
| 31 |
|
| 32 |
import React, { LegacyRef } from 'react'; |
| 33 |
import { classes } from 'dome/misc/utils'; |
| 34 |
import { Icon } from './icons'; |
| 35 |
import './style.css'; |
| 36 |
|
| 37 |
// -------------------------------------------------------------------------- |
| 38 |
// --- Generic Label |
| 39 |
// -------------------------------------------------------------------------- |
| 40 |
|
| 41 |
/** Labels support fowarding refs to their inner [<label/>] element. */ |
| 42 |
export interface LabelProps { |
| 43 |
/** Text of the label. Prepend to other children elements. */ |
| 44 |
label?: string; |
| 45 |
/** Icon identifier. Displayed on the left side of the label. */ |
| 46 |
icon?: string; |
| 47 |
/** Tool-tip description. */ |
| 48 |
title?: string; |
| 49 |
/** Additional class. */ |
| 50 |
className?: string; |
| 51 |
/** Additional style. */ |
| 52 |
style?: React.CSSProperties; |
| 53 |
/** If `false`, do not display the label. Default to `true`. */ |
| 54 |
display?: boolean; |
| 55 |
/** Additional content of the `<label/>` element. */ |
| 56 |
children?: React.ReactNode; |
| 57 |
/** Click event callback. */ |
| 58 |
onClick?: (evt: React.MouseEvent) => void; |
| 59 |
/** Click event callback. */ |
| 60 |
onDoubleClick?: (evt: React.MouseEvent) => void; |
| 61 |
/** Right-click event callback. */ |
| 62 |
onContextMenu?: (evt: React.MouseEvent) => void; |
| 63 |
} |
| 64 |
|
| 65 |
const makeLabel = (className: string) => |
| 66 |
function Label |
| 67 |
( |
| 68 |
props: LabelProps, |
| 69 |
ref: LegacyRef<HTMLLabelElement> | undefined |
| 70 |
): JSX.Element { |
| 71 |
const { display = true } = props; |
| 72 |
const allClasses = classes( |
| 73 |
className, |
| 74 |
!display && 'dome-control-erased', |
| 75 |
props.className, |
| 76 |
); |
| 77 |
return ( |
| 78 |
<label |
| 79 |
ref={ref} |
| 80 |
className={allClasses} |
| 81 |
title={props.title} |
| 82 |
style={props.style} |
| 83 |
onClick={props.onClick} |
| 84 |
onDoubleClick={props.onDoubleClick} |
| 85 |
onContextMenu={props.onContextMenu} |
| 86 |
> |
| 87 |
{props.icon && <Icon title={props.title} id={props.icon} />} |
| 88 |
{props.label} |
| 89 |
{props.children} |
| 90 |
</label> |
| 91 |
); |
| 92 |
}; |
| 93 |
|
| 94 |
// -------------------------------------------------------------------------- |
| 95 |
// --- CSS Classes |
| 96 |
// -------------------------------------------------------------------------- |
| 97 |
|
| 98 |
const LABEL = 'dome-xLabel dome-text-label'; |
| 99 |
const TITLE = 'dome-xLabel dome-text-title'; |
| 100 |
const DESCR = 'dome-xLabel dome-text-descr'; |
| 101 |
const TDATA = 'dome-xLabel dome-text-data'; |
| 102 |
const TCODE = 'dome-xLabel dome-text-code'; |
| 103 |
const TCELL = 'dome-xLabel dome-text-cell'; |
| 104 |
|
| 105 |
// -------------------------------------------------------------------------- |
| 106 |
// --- Components |
| 107 |
// -------------------------------------------------------------------------- |
| 108 |
|
| 109 |
/** Simple labels. */ |
| 110 |
export const Label = React.forwardRef(makeLabel(LABEL)); |
| 111 |
|
| 112 |
/** Title and headings. */ |
| 113 |
export const Title = React.forwardRef(makeLabel(TITLE)); |
| 114 |
|
| 115 |
/** Description, textbook content. */ |
| 116 |
export const Descr = React.forwardRef(makeLabel(DESCR)); |
| 117 |
|
| 118 |
/** Selectable textual information. */ |
| 119 |
export const Data = React.forwardRef(makeLabel(TDATA)); |
| 120 |
|
| 121 |
/** Selectable inlined source-code content. */ |
| 122 |
export const Code = React.forwardRef(makeLabel(TCODE)); |
| 123 |
|
| 124 |
/** Selectable inlined source-code content with default cursor. */ |
| 125 |
export const Cell = React.forwardRef(makeLabel(TCELL)); |
| 126 |
|
| 127 |
// -------------------------------------------------------------------------- |