From 1d779500492970d99a003f47d81e0144c282c2bb Mon Sep 17 00:00:00 2001 From: PrasadMadine Date: Fri, 14 Jun 2024 12:19:02 +0530 Subject: [PATCH 1/3] fix/Added error message when label and value are empty in radi group widget --- .../propertyControls/KeyValueComponent.tsx | 102 ++++++++++++------ .../widgets/RadioGroupWidget/widget/index.tsx | 10 ++ 2 files changed, 77 insertions(+), 35 deletions(-) diff --git a/app/client/src/components/propertyControls/KeyValueComponent.tsx b/app/client/src/components/propertyControls/KeyValueComponent.tsx index ab9ef69223e1..e8b5e3a57fa2 100644 --- a/app/client/src/components/propertyControls/KeyValueComponent.tsx +++ b/app/client/src/components/propertyControls/KeyValueComponent.tsx @@ -38,6 +38,15 @@ function updateOptionValue( }; }); } +const FlexBox = styled.div` + display: flex; + flex-direction: column; + align-items: center; +`; + +const ErrorMessageBox = styled.div` + color: red; +`; const StyledBox = styled.div` width: 10px; @@ -69,6 +78,7 @@ export function KeyValueComponent(props: KeyValueComponentProps) { SegmentedControlOptionWithKey[] >([]); const [typing, setTyping] = useState(false); + const [errorMessages, setErrorMessages] = useState([]); const { pairs } = props; useEffect(() => { let { pairs } = props; @@ -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( @@ -105,6 +116,7 @@ export function KeyValueComponent(props: KeyValueComponentProps) { setRenderPairs(updatedRenderPairs); debouncedUpdatePairs(updatedPairs); + validatePairs(updatedRenderPairs); } function updateValue(index: number, updatedValue: string) { @@ -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) { @@ -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); } @@ -162,6 +186,7 @@ export function KeyValueComponent(props: KeyValueComponentProps) { }); setRenderPairs(updatedRenderPairs); + validatePairs(updatedRenderPairs); props.updatePairs(pairs, e.detail === 0); } @@ -177,40 +202,47 @@ export function KeyValueComponent(props: KeyValueComponentProps) { <> {renderPairs.map((pair: SegmentedControlOptionWithKey, index) => { return ( - - { - updateKey(index, value); - }} - onFocus={onInputFocus} - placeholder={"Name"} - value={pair.label} - /> - - { - updateValue(index, value); - }} - onFocus={onInputFocus} - placeholder={"Value"} - value={pair.value} - /> - -