Skip to content

Commit

Permalink
[v17] Support sticky tooltips in FieldInput and legend in LabelsInput (
Browse files Browse the repository at this point in the history
…#51552)

* Support sticky tooltips in FieldInput and legend in LabelsInput (#51457)

* Support sticky tooltips in FieldInput and legend in LabelsInput

* review

* Revert "review" (#51510)

This reverts commit ffe0554.
  • Loading branch information
bl-nero authored Jan 29, 2025
1 parent 8c3b25a commit 96f6927
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
7 changes: 6 additions & 1 deletion web/packages/shared/components/FieldInput/FieldInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const FieldInput = forwardRef<HTMLInputElement, FieldInputProps>(
spellCheck,
readonly = false,
toolTipContent = null,
tooltipSticky = false,
disabled = false,
markAsError = false,
required = false,
Expand Down Expand Up @@ -114,7 +115,10 @@ const FieldInput = forwardRef<HTMLInputElement, FieldInputProps>(
>
{label}
</span>
<IconTooltip children={toolTipContent} />
<IconTooltip
sticky={tooltipSticky}
children={toolTipContent}
/>
</>
) : (
<>{label}</>
Expand Down Expand Up @@ -242,6 +246,7 @@ export type FieldInputProps = BoxProps & {
min?: number;
max?: number;
toolTipContent?: React.ReactNode;
tooltipSticky?: boolean;
disabled?: boolean;
// markAsError is a flag to highlight an
// input box as error color before validator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export const Custom = () => {
<LabelsInput
labels={labels}
setLabels={setLables}
legend="List of Labels"
tooltipContent="List of labels, 'nuff said"
labelKey={{
fieldName: 'Custom Key Name',
placeholder: 'custom key placeholder',
Expand Down
57 changes: 44 additions & 13 deletions web/packages/teleport/src/components/LabelsInput/LabelsInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

import styled from 'styled-components';

import { Box, ButtonIcon, ButtonSecondary, Flex } from 'design';
import { Box, ButtonIcon, ButtonSecondary, Flex, Text } from 'design';
import * as Icons from 'design/Icon';
import { inputGeometry } from 'design/Input/Input';
import { IconTooltip } from 'design/Tooltip';
import FieldInput from 'shared/components/FieldInput';
import {
useRule,
Expand Down Expand Up @@ -63,6 +64,8 @@ type LabelValidationResult = {
export type LabelsRule = Rule<Label[], LabelListValidationResult>;

export function LabelsInput({
legend,
tooltipContent,
labels = [],
setLabels,
disableBtns = false,
Expand All @@ -74,6 +77,8 @@ export function LabelsInput({
inputWidth = 200,
rule = defaultRule,
}: {
legend?: string;
tooltipContent?: string;
labels: Label[];
setLabels(l: Label[]): void;
disableBtns?: boolean;
Expand Down Expand Up @@ -141,15 +146,34 @@ export function LabelsInput({
const width = `${inputWidth}px`;
const inputSize = 'medium';
return (
<>
<Fieldset>
{legend && (
<Legend>
{tooltipContent ? (
<>
<span
css={{
marginRight: '4px',
verticalAlign: 'middle',
}}
>
{legend}
</span>
<IconTooltip children={tooltipContent} />
</>
) : (
legend
)}
</Legend>
)}
{labels.length > 0 && (
<Flex mt={2}>
<Flex mt={legend ? 1 : 0} mb={1}>
<Box width={width} mr="3">
{labelKey.fieldName} <SmallText>(required field)</SmallText>
</Box>
<Box>
{labelVal.fieldName} <SmallText>(required field)</SmallText>
<Text typography="body3">
{labelKey.fieldName} (required field)
</Text>
</Box>
<Text typography="body3">{labelVal.fieldName} (required field)</Text>
</Flex>
)}
<Box>
Expand Down Expand Up @@ -229,17 +253,12 @@ export function LabelsInput({
<Icons.Add className="icon-add" disabled={disableBtns} size="small" />
{labels.length > 0 ? `Add another ${adjective}` : `Add a ${adjective}`}
</ButtonSecondary>
</>
</Fieldset>
);
}

const defaultRule = () => () => ({ valid: true });

const SmallText = styled.span`
font-size: ${p => p.theme.fontSizes[1]}px;
font-weight: lighter;
`;

export const nonEmptyLabels: LabelsRule = labels => () => {
const results = labels.map(label => ({
name: requiredField('required')(label.name)(),
Expand All @@ -250,3 +269,15 @@ export const nonEmptyLabels: LabelsRule = labels => () => {
results: results,
};
};

const Fieldset = styled.fieldset`
border: none;
margin: 0;
padding: 0;
`;

const Legend = styled.legend`
margin: 0 0 ${props => props.theme.space[1]}px 0;
padding: 0;
${props => props.theme.typography.body3}
`;

0 comments on commit 96f6927

Please sign in to comment.