@kinvolk/headlamp-plugin
Version:
The needed infrastructure for building Headlamp plugins.
44 lines (43 loc) • 2.29 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
/*
* Copyright 2025 The Kubernetes Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Icon } from '@iconify/react';
import Box from '@mui/material/Box';
import IconButton from '@mui/material/IconButton';
import React from 'react';
import { useTranslation } from 'react-i18next';
import { useId } from '../../../lib/util';
export default function ShowHideLabel(props) {
const { show = false, labelId = '', maxChars = 256, children } = props;
const { t } = useTranslation();
const [expanded, setExpanded] = React.useState(show);
const generatedId = useId('show-hide-label-');
const labelIdOrRandom = labelId || generatedId;
const [actualText, needsButton] = React.useMemo(() => {
if (typeof children !== 'string') {
return ['', false];
}
if (expanded) {
return [children, true];
}
return [children.substr(0, maxChars), children.length > maxChars];
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [children, expanded]);
if (!children) {
return null;
}
return (_jsx(Box, { display: expanded ? 'block' : 'flex', children: _jsxs("span", { id: labelIdOrRandom, style: { wordBreak: 'break-all', whiteSpace: 'normal' }, children: [actualText, needsButton && (_jsxs(_Fragment, { children: [!expanded && '…', _jsx(IconButton, { "aria-controls": labelIdOrRandom, "aria-expanded": expanded, sx: { display: 'inline' }, onClick: () => setExpanded(expandedVal => !expandedVal), size: "small", "aria-label": expanded ? t('translation|Collapse') : t('translation|Expand'), children: _jsx(Icon, { icon: expanded ? 'mdi:menu-up' : 'mdi:menu-down' }) })] }))] }) }));
}