| 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 |
// --- Tabs |
| 25 |
// -------------------------------------------------------------------------- |
| 26 |
|
| 27 |
/** |
| 28 |
@packageDocumentation |
| 29 |
@module dome/frame/tabs |
| 30 |
*/ |
| 31 |
|
| 32 |
import React from 'react'; |
| 33 |
import { Icon } from 'dome/controls/icons'; |
| 34 |
|
| 35 |
import './style.css'; |
| 36 |
|
| 37 |
// -------------------------------------------------------------------------- |
| 38 |
// --- Tabs Bar |
| 39 |
// -------------------------------------------------------------------------- |
| 40 |
|
| 41 |
export interface TabsBarProps { |
| 42 |
children?: React.ReactNode; |
| 43 |
} |
| 44 |
|
| 45 |
/** Container for Tabs. */ |
| 46 |
export function TabsBar(props: TabsBarProps): JSX.Element { |
| 47 |
return ( |
| 48 |
<div className="dome-xTabsBar dome-color-frame"> |
| 49 |
{props.children} |
| 50 |
</div> |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
// -------------------------------------------------------------------------- |
| 55 |
// --- Single Tab |
| 56 |
// -------------------------------------------------------------------------- |
| 57 |
|
| 58 |
export interface TabProps<A> { |
| 59 |
/** Tab's identifier. */ |
| 60 |
value: A; |
| 61 |
/** Tab's label. */ |
| 62 |
label?: string; |
| 63 |
/** Tab's tooltip text. */ |
| 64 |
title?: string; |
| 65 |
/** Close tab's tooltip text. */ |
| 66 |
closing?: string; |
| 67 |
/** Currently selected tab. */ |
| 68 |
selection?: A; |
| 69 |
/** Selection callback. */ |
| 70 |
onSelection?: (value: A) => void; |
| 71 |
/** Closing callback. */ |
| 72 |
onClose?: (value: A) => void; |
| 73 |
} |
| 74 |
|
| 75 |
/** Tab Selector. */ |
| 76 |
export function Tab<A>(props: TabProps<A>): JSX.Element { |
| 77 |
const { value, selection, onSelection, onClose } = props; |
| 78 |
const selected = value === selection; |
| 79 |
// --- Tab Rendering |
| 80 |
const onSelect = onSelection && (() => onSelection(value)); |
| 81 |
const onClosed = onClose && (() => onClose(value)); |
| 82 |
const closing = onClose ? ( |
| 83 |
<Icon |
| 84 |
className="dome-xTab-closing" |
| 85 |
title={props.closing || 'Close Tab'} |
| 86 |
id="CROSS" |
| 87 |
onClick={onClosed} |
| 88 |
/> |
| 89 |
) : undefined; |
| 90 |
const classes = `dome-xTab${selected ? ' dome-active' : ' dome-inactive'}`; |
| 91 |
return ( |
| 92 |
<label |
| 93 |
className={classes} |
| 94 |
title={props.title} |
| 95 |
onClick={onSelect} |
| 96 |
> |
| 97 |
{props.label} |
| 98 |
{closing} |
| 99 |
</label> |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
// -------------------------------------------------------------------------- |