Skip to content

Commit c1a6e05

Browse files
authored
Merge pull request #1618 from dxc-technology/gomezivann-radio-group-fix
Radio Group update
2 parents ede733c + d20a4b0 commit c1a6e05

File tree

3 files changed

+54
-36
lines changed

3 files changed

+54
-36
lines changed

lib/src/radio-group/Radio.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import React, { useLayoutEffect, useRef, useState } from "react";
1+
import React, { useEffect, useRef, useState } from "react";
22
import styled, { ThemeProvider } from "styled-components";
33
import { RadioProps } from "./types";
44
import { v4 as uuidv4 } from "uuid";
55
import useTheme from "../useTheme";
66
import { AdvancedTheme } from "../common/variables";
7+
import DxcFlex from "../flex/Flex";
78

89
const DxcRadio = ({
910
label,
@@ -25,7 +26,7 @@ const DxcRadio = ({
2526
};
2627

2728
const [firstUpdate, setFirstUpdate] = useState(true);
28-
useLayoutEffect(() => {
29+
useEffect(() => {
2930
// Don't apply in the first render
3031
if (firstUpdate) {
3132
setFirstUpdate(false);
@@ -36,7 +37,7 @@ const DxcRadio = ({
3637

3738
return (
3839
<ThemeProvider theme={colorsTheme.radioGroup}>
39-
<RadioMainContainer>
40+
<DxcFlex>
4041
<RadioContainer
4142
error={error}
4243
disabled={disabled}
@@ -62,7 +63,7 @@ const DxcRadio = ({
6263
{label}
6364
</Label>
6465
</RadioContainer>
65-
</RadioMainContainer>
66+
</DxcFlex>
6667
</ThemeProvider>
6768
);
6869
};
@@ -100,10 +101,6 @@ const getRadioInputStateColor = (
100101
}
101102
};
102103

103-
const RadioMainContainer = styled.div`
104-
display: flex;
105-
`;
106-
107104
const RadioInputContainer = styled.span`
108105
display: flex;
109106
align-items: center;

lib/src/radio-group/RadioGroup.tsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,21 @@ const DxcRadioGroup = React.forwardRef<RefType, RadioGroupPropsType>(
4242
const colorsTheme = useTheme();
4343
const translatedLabels = useTranslatedLabels();
4444

45-
const optionalItem = {
46-
label: optionalItemLabel || translatedLabels.radioGroup.optionalItemLabelDefault,
47-
value: "",
48-
disabled,
49-
};
50-
const innerOptions = useMemo(() => (optional ? [...options, optionalItem] : options), [optional, options]);
45+
const innerOptions = useMemo(
46+
() =>
47+
optional
48+
? [
49+
...options,
50+
{
51+
label: optionalItemLabel ?? translatedLabels.radioGroup.optionalItemLabelDefault,
52+
value: "",
53+
disabled,
54+
},
55+
]
56+
: options,
57+
[optional, options, optionalItemLabel, translatedLabels]
58+
);
59+
5160
const [currentFocusIndex, setCurrentFocusIndex] = useState(getInitialFocusIndex(innerOptions, value ?? innerValue));
5261

5362
const handleOnChange = useCallback(
@@ -64,7 +73,6 @@ const DxcRadioGroup = React.forwardRef<RefType, RadioGroupPropsType>(
6473
// If the radio group loses the focus to an element not contained inside it...
6574
if (!event.currentTarget.contains(event.relatedTarget as Node)) {
6675
setFirstTimeFocus(true);
67-
6876
const currentValue = value ?? innerValue;
6977
!optional && !Boolean(currentValue)
7078
? onBlur?.({ value: currentValue, error: translatedLabels.formFields.requiredSelectionErrorMessage })
@@ -142,7 +150,7 @@ const DxcRadioGroup = React.forwardRef<RefType, RadioGroupPropsType>(
142150
aria-readonly={readonly}
143151
aria-orientation={stacking === "column" ? "vertical" : "horizontal"}
144152
>
145-
<ValueInput name={name} disabled={disabled} value={value ?? innerValue ?? ""} readOnly aria-hidden="true" />
153+
<ValueInput name={name} disabled={disabled} value={value ?? innerValue ?? ""} readOnly />
146154
{innerOptions.map((option, index) => (
147155
<DxcRadio
148156
key={`radio-${index}`}
@@ -172,9 +180,9 @@ const DxcRadioGroup = React.forwardRef<RefType, RadioGroupPropsType>(
172180
);
173181

174182
const RadioGroupContainer = styled.div`
183+
box-sizing: border-box;
175184
display: inline-flex;
176185
flex-direction: column;
177-
box-sizing: border-box;
178186
`;
179187

180188
const Label = styled.span<{ helperText: RadioGroupPropsType["helperText"]; disabled: RadioGroupPropsType["disabled"] }>`
Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,47 @@
1-
import { DxcRadioGroup, DxcInset } from "@dxc-technology/halstack-react";
2-
import { useState } from "react";
1+
import {
2+
DxcRadioGroup,
3+
DxcInset,
4+
DxcFlex,
5+
DxcButton,
6+
} from "@dxc-technology/halstack-react";
7+
import { useRef } from "react";
38

49
const code = `() => {
5-
const [value, setValue] = useState("");
6-
const onChange = (value) => {
7-
console.log(value);
8-
};
10+
const radioGroupRef = useRef();
11+
12+
const handleSubmit = () => {
13+
const radioGroup =
14+
radioGroupRef.current.getElementsByTagName("input")[0];
15+
console.log(radioGroup.value);
16+
};
17+
18+
const options = [
19+
{ label: "Orange", value: "orange" },
20+
{ label: "Apple", value: "apple" },
21+
{ label: "Pear", value: "pear" },
22+
];
923
10-
const options = [
11-
{ label: "Orange", value: "orange" },
12-
{ label: "Apple", value: "apple" },
13-
{ label: "Pear", value: "pear" },
14-
];
15-
16-
return (
17-
<DxcInset space="2rem">
24+
return (
25+
<DxcInset space="2rem">
26+
<DxcFlex direction="column" gap="2rem">
1827
<DxcRadioGroup
1928
label="Fruit"
2029
defaultValue="apple"
2130
options={options}
22-
onChange={onChange}
31+
ref={radioGroupRef}
2332
/>
24-
</DxcInset>
25-
);
26-
}`;
33+
<DxcButton label="Submit" type="submit" onClick={handleSubmit} />
34+
</DxcFlex>
35+
</DxcInset>
36+
);
37+
}`;
2738

2839
const scope = {
2940
DxcRadioGroup,
3041
DxcInset,
31-
useState,
42+
DxcFlex,
43+
DxcButton,
44+
useRef,
3245
};
3346

3447
export default { code, scope };

0 commit comments

Comments
 (0)