Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 66 additions & 34 deletions app/client/src/components/propertyControls/KeyValueComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ function updateOptionValue<T>(
};
});
}
const FlexBox = styled.div`
display: flex;
flex-direction: column;
align-items: center;
`;

const ErrorMessageBox = styled.div`
color: ${props => props.theme.colors.error};
`;

const StyledBox = styled.div`
width: 10px;
Expand Down Expand Up @@ -69,6 +78,7 @@ export function KeyValueComponent(props: KeyValueComponentProps) {
SegmentedControlOptionWithKey[]
>([]);
const [typing, setTyping] = useState<boolean>(false);
const [errorMessages, setErrorMessages] = useState<string[]>([]);
const { pairs } = props;
useEffect(() => {
let { pairs } = props;
Expand All @@ -84,6 +94,7 @@ export function KeyValueComponent(props: KeyValueComponentProps) {
);

pairs.length !== 0 && !typing && setRenderPairs(newRenderPairs);
validatePairs(newRenderPairs);
}, [props, pairs.length, renderPairs.length]);

const debouncedUpdatePairs = useCallback(
Expand All @@ -105,6 +116,7 @@ export function KeyValueComponent(props: KeyValueComponentProps) {

setRenderPairs(updatedRenderPairs);
debouncedUpdatePairs(updatedPairs);
validatePairs(updatedRenderPairs);
}

function updateValue(index: number, updatedValue: string) {
Expand All @@ -119,6 +131,17 @@ export function KeyValueComponent(props: KeyValueComponentProps) {

setRenderPairs(updatedRenderPairs);
debouncedUpdatePairs(updatedPairs);
validatePairs(updatedRenderPairs);
}

function validatePairs(pairs: SegmentedControlOptionWithKey[]) {
const newErrorMessages = pairs.map((pair) => {
if (!pair.label && !pair.value) {
return "Both Name and Value can't be empty";
}
return "";
});
setErrorMessages(newErrorMessages);
}

function deletePair(index: number, isUpdatedViaKeyboard = false) {
Expand All @@ -129,6 +152,7 @@ export function KeyValueComponent(props: KeyValueComponentProps) {
const newRenderPairs = renderPairs.filter((o, i) => i !== index);

setRenderPairs(newRenderPairs);
validatePairs(newRenderPairs);
props.updatePairs(newPairs, isUpdatedViaKeyboard);
}

Expand Down Expand Up @@ -162,6 +186,7 @@ export function KeyValueComponent(props: KeyValueComponentProps) {
});

setRenderPairs(updatedRenderPairs);
validatePairs(updatedRenderPairs);
props.updatePairs(pairs, e.detail === 0);
}

Expand All @@ -177,40 +202,47 @@ export function KeyValueComponent(props: KeyValueComponentProps) {
<>
{renderPairs.map((pair: SegmentedControlOptionWithKey, index) => {
return (
<ControlWrapper key={pair.key} orientation={"HORIZONTAL"}>
<StyledInputGroup
dataType={"text"}
onBlur={onInputBlur}
onChange={(value: string) => {
updateKey(index, value);
}}
onFocus={onInputFocus}
placeholder={"Name"}
value={pair.label}
/>
<StyledBox />
<StyledInputGroup
dataType={"text"}
onBlur={onInputBlur}
onChange={(value: string) => {
updateValue(index, value);
}}
onFocus={onInputFocus}
placeholder={"Value"}
value={pair.value}
/>
<StyledBox />
<Button
isIconButton
kind="tertiary"
onClick={(e: React.MouseEvent) => {
deletePair(index, e.detail === 0);
}}
size="sm"
startIcon="delete-bin-line"
style={{ width: "50px" }}
/>
</ControlWrapper>
<FlexBox key={pair.key}>
<ControlWrapper orientation={"HORIZONTAL"}>
<StyledInputGroup
dataType={"text"}
onBlur={onInputBlur}
onChange={(value: string) => {
updateKey(index, value);
}}
onFocus={onInputFocus}
placeholder={"Name"}
value={pair.label}
/>
<StyledBox />
<StyledInputGroup
dataType={"text"}
onBlur={onInputBlur}
onChange={(value: string) => {
updateValue(index, value);
}}
onFocus={onInputFocus}
placeholder={"Value"}
value={pair.value}
/>
<StyledBox />
<Button
isIconButton
kind="tertiary"
onClick={(e: React.MouseEvent) => {
deletePair(index, e.detail === 0);
}}
size="sm"
startIcon="delete-bin-line"
style={{ width: "50px" }}
/>
</ControlWrapper>
{errorMessages[index] && (
<ErrorMessageBox>
{errorMessages[index]}
</ErrorMessageBox>
)}
</FlexBox>
);
})}

Expand Down
10 changes: 10 additions & 0 deletions app/client/src/widgets/RadioGroupWidget/widget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ export function optionsCustomValidation(
break;
}

if (!label && !value) {
_isValid = false;
message = {
name: "ValidationError",
message:
"Both Name and Value can't be empty",
};
break;
}

//Check if the required field "label" is present:
if (!label) {
_isValid = false;
Expand Down