1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-10-27 11:02:16 +01:00
unleash.unleash/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints/AddSingleValueWidget.tsx
Thomas Heartman 832e3f2e79
Add input help text (#9883)
Adds help text to the popover input for free text values, single numeric
and semver values. The help text is in addition to the error text (so
you can get both).

Also makes the add button a little narrower to better match sketches.

## Rendered

Multiple values (free text):
<img width="953" alt="image"
src="https://github.com/user-attachments/assets/1d9bf7da-af8c-46b6-8eae-ae4f8a687363"
/>

With error
<img width="936" alt="image"
src="https://github.com/user-attachments/assets/aa9dc2da-ad9f-43da-9e44-c36fd8344df1"
/>

Numeric operators:
<img width="927" alt="image"
src="https://github.com/user-attachments/assets/f1e8afd8-7051-4691-bdd2-810929ccd4fa"
/>



SemVer operators:

<img width="944" alt="image"
src="https://github.com/user-attachments/assets/655a7a7b-a4a4-468c-8a5d-23e5d38375b8"
/>
2025-05-02 15:47:29 +02:00

69 lines
2.3 KiB
TypeScript

import Add from '@mui/icons-material/Add';
import { styled } from '@mui/material';
import { forwardRef, useImperativeHandle, useRef, useState } from 'react';
import { ValueChip } from './ValueList';
import { AddValuesPopover, type OnAddActions } from './AddValuesPopover';
const StyledChip = styled(ValueChip, {
shouldForwardProp: (prop) => prop !== 'hasValue',
})<{ hasValue: boolean }>(({ theme, hasValue }) => ({
color: hasValue ? 'inherit' : theme.palette.primary.main,
width: 'max-content',
'.MuiChip-icon': {
transform: 'translateX(50%)',
fill: theme.palette.primary.main,
height: theme.fontSizes.smallerBody,
width: theme.fontSizes.smallerBody,
},
}));
interface AddValuesProps {
onAddValue: (newValue: string) => void;
removeValue: () => void;
currentValue?: string;
helpText?: string;
}
export const AddSingleValueWidget = forwardRef<HTMLDivElement, AddValuesProps>(
({ currentValue, onAddValue, removeValue, helpText }, ref) => {
const [open, setOpen] = useState(false);
const positioningRef = useRef<HTMLDivElement>(null);
useImperativeHandle(
ref,
() => positioningRef.current as HTMLDivElement,
);
const handleAdd = (newValue: string, { setError }: OnAddActions) => {
if (newValue.length > 100) {
setError('Values cannot be longer than 100 characters');
return;
}
onAddValue(newValue);
setError('');
setOpen(false);
};
return (
<>
<StyledChip
hasValue={!!currentValue}
ref={positioningRef}
label={currentValue || 'Add value'}
onClick={() => setOpen(true)}
icon={currentValue ? undefined : <Add />}
onDelete={currentValue ? removeValue : undefined}
/>
<AddValuesPopover
initialValue={currentValue}
onAdd={handleAdd}
helpText={helpText}
open={open}
anchorEl={positioningRef.current}
onClose={() => setOpen(false)}
/>
</>
);
},
);