Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui-react-native): add RadioGroup uncontrolled support #2816

Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 20 additions & 3 deletions examples/react-native/storybook/stories/RadioGroup.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,23 @@ const ControlledRadioGroup = ({ ...props }: any) => {
};

return (
<RadioGroup {...props} value={value} onChange={onChange}>
<RadioGroup {...props} initialValue={value} onChange={onChange}>
<Radio value="option-1" label="Option 1" />
<Radio value="option-2" label="Option 2" />
<Radio value="option-3" label="Option 3" />
</RadioGroup>
);
};

const UncontrolledRadioGroup = ({ ...props }: any) => {
const [selectedValue, setSelectedValue] = useState('Empty :(');

return (
<RadioGroup
{...props}
label={selectedValue}
onValueChange={setSelectedValue}
>
<Radio value="option-1" label="Option 1" />
<Radio value="option-2" label="Option 2" />
<Radio value="option-3" label="Option 3" />
Expand All @@ -28,7 +44,7 @@ const CustomRadioGroup = ({ ...props }: any) => {
};

return (
<RadioGroup {...props} value={value} onChange={onChange}>
<RadioGroup {...props} initialValue={value} onChange={onChange}>
<Radio value="small" label="Should be small" size="small" />
<Radio
value="red"
Expand All @@ -48,8 +64,9 @@ const CustomRadioGroup = ({ ...props }: any) => {
storiesOf('RadioGroup', module)
.add('default', () => <ControlledRadioGroup label="Basic RadioGroup" />)
.add('controlled', () => (
<ControlledRadioGroup label="Defaults to Option 2" value="option-2" />
<ControlledRadioGroup label="Defaults to Option 2" />
))
.add('uncontrolled', () => <UncontrolledRadioGroup initialValue="option-2" />)
.add('direction', () => (
<>
<ControlledRadioGroup label="Horizontal" direction="horizontal" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ Array [
"useAuthenticator",
"useAuthenticatorInitMachine",
"useAuthenticatorRoute",
"useHasValueUpdated",
"usePreviousValue",
]
`;
3 changes: 3 additions & 0 deletions packages/react-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ export {
UseAuthenticatorRoute,
FormFieldsComponent,
} from './Authenticator';

// components/hooks/utils
export { useHasValueUpdated, usePreviousValue } from './hooks';
5 changes: 4 additions & 1 deletion packages/react-native/src/primitives/Radio/Radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ export default function Radio<T>({
style={[styles.radioContainer, radioContainerSize, radioContainerStyle]}
>
{selected ? (
<View style={[styles.radioDot, radioDotSize, radioDotStyle]} />
<View
style={[styles.radioDot, radioDotSize, radioDotStyle]}
testID="amplify__radio-button__dot"
/>
) : null}
</View>
{label ? <Label style={labelStyle}>{label}</Label> : null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ exports[`Radio renders as expected when passing a number to the size prop 1`] =
undefined,
]
}
testID="amplify__radio-button__dot"
/>
</View>
<Text
Expand Down Expand Up @@ -257,6 +258,7 @@ exports[`Radio renders as expected when selected is true 1`] = `
undefined,
]
}
testID="amplify__radio-button__dot"
/>
</View>
<Text
Expand Down Expand Up @@ -334,6 +336,7 @@ exports[`Radio renders as expected when size is large 1`] = `
undefined,
]
}
testID="amplify__radio-button__dot"
/>
</View>
<Text
Expand Down Expand Up @@ -411,6 +414,7 @@ exports[`Radio renders as expected when size is medium 1`] = `
undefined,
]
}
testID="amplify__radio-button__dot"
/>
</View>
<Text
Expand Down Expand Up @@ -488,6 +492,7 @@ exports[`Radio renders as expected when size is small 1`] = `
undefined,
]
}
testID="amplify__radio-button__dot"
/>
</View>
<Text
Expand Down
39 changes: 36 additions & 3 deletions packages/react-native/src/primitives/RadioGroup/RadioGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import React, { Children, cloneElement, isValidElement, useMemo } from 'react';
import React, {
Children,
cloneElement,
isValidElement,
useCallback,
useEffect,
useMemo,
useState,
} from 'react';
import { View, ViewStyle } from 'react-native';

import { useHasValueUpdated } from '@aws-amplify/ui-react-core';

import { Label } from '../Label';
import { getFlexDirectionFromLabelPosition } from '../Label/utils';
import { RadioProps } from '../Radio';
Expand All @@ -13,15 +23,29 @@ export default function RadioGroup<T>({
children,
direction = 'vertical',
disabled,
initialValue,
label,
labelPosition = 'top',
labelStyle,
onChange,
onValueChange,
size,
style,
value,
...rest
}: RadioGroupProps<T>): JSX.Element {
const [value, setValue] = useState<T | undefined>(initialValue);

// track `hasValueUpdated` and `hasOnValueChangeUpdated`,
// only call `onValueChange` on `value` update
const hasValueUpdated = useHasValueUpdated(value);
const hasOnValueChangeUpdated = useHasValueUpdated(onValueChange);

useEffect(() => {
if (hasValueUpdated) {
onValueChange?.(value);
}
}, [hasOnValueChangeUpdated, hasValueUpdated, onValueChange, value]);

const containerStyle: ViewStyle = useMemo(
() => ({
flexDirection: getFlexDirectionFromLabelPosition(labelPosition),
Expand All @@ -34,6 +58,15 @@ export default function RadioGroup<T>({
[direction]
);

const handleChange = useCallback(
(nextValue: T | undefined) => {
setValue(nextValue);

onChange?.(nextValue);
},
[onChange]
);

return (
<View {...rest} style={[containerStyle, style]}>
<View accessibilityRole={accessibilityRole} style={childContainerStyle}>
Expand All @@ -50,7 +83,7 @@ export default function RadioGroup<T>({

return cloneElement<RadioProps<T>>(child, {
disabled: isChildDisabled,
onChange,
onChange: handleChange,
selected: isChildSelected,
size: childSize ?? size,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const ControlledRadioGroup = ({
return (
<RadioGroup
{...rest}
value={value}
initialValue={value}
onChange={(nextValue) => {
if (typeof nextValue === 'string') {
handleOnChange(nextValue);
Expand Down Expand Up @@ -94,4 +94,64 @@ describe('RadioGroup', () => {
const { toJSON } = render(<ControlledRadioGroup size={value} />);
expect(toJSON()).toMatchSnapshot();
});

it('updates the selected option in uncontrolled mode', () => {
const { getByTestId, queryByTestId } = render(
<RadioGroup>
<Radio value="option-1" label="Option 1" testID="option-1" />
<Radio value="option-2" label="Option 2" testID="option-2" />
</RadioGroup>
);

const selectedOption = queryByTestId('amplify__radio-button__dot');

expect(selectedOption).toBeNull();

const optionOne = getByTestId('option-1');

fireEvent.press(optionOne);

expect(queryByTestId('amplify__radio-button__dot')).toBeDefined();
});

it('only calls onValueChange when value changes', () => {
const onValueChange = jest.fn();

const { getByTestId, rerender } = render(
<RadioGroup onValueChange={onValueChange}>
<Radio value="option-1" label="Option 1" testID="option-1" />
<Radio value="option-2" label="Option 2" testID="option-2" />
</RadioGroup>
);

const optionOne = getByTestId('option-1');

fireEvent.press(optionOne);

expect(onValueChange).toHaveBeenCalledTimes(1);
expect(onValueChange).toHaveBeenCalledWith('option-1');

const updatedOnValueChange = jest.fn();

rerender(
<RadioGroup onValueChange={updatedOnValueChange}>
<Radio value="option-1" label="Option 1" testID="option-1" />
<Radio value="option-2" label="Option 2" testID="option-2" />
</RadioGroup>
);

expect(updatedOnValueChange).not.toHaveBeenCalled();
});

it('renders the label prop', () => {
const label = 'label';
const { getByText } = render(
<RadioGroup label={label}>
<Radio value="option-1" label="Option 1" testID="option-1" />
<Radio value="option-2" label="Option 1" testID="option-2" />
</RadioGroup>
);

expect(getByText(label)).toBeDefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ exports[`RadioGroup renders as expected when direction is horizontal 1`] = `
undefined,
]
}
testID="amplify__radio-button__dot"
/>
</View>
<Text
Expand Down Expand Up @@ -347,6 +348,7 @@ exports[`RadioGroup renders as expected when direction is vertical 1`] = `
undefined,
]
}
testID="amplify__radio-button__dot"
/>
</View>
<Text
Expand Down Expand Up @@ -619,6 +621,7 @@ exports[`RadioGroup renders as expected when size is large 1`] = `
undefined,
]
}
testID="amplify__radio-button__dot"
/>
</View>
<Text
Expand Down Expand Up @@ -891,6 +894,7 @@ exports[`RadioGroup renders as expected when size is medium 1`] = `
undefined,
]
}
testID="amplify__radio-button__dot"
/>
</View>
<Text
Expand Down Expand Up @@ -1163,6 +1167,7 @@ exports[`RadioGroup renders as expected when size is small 1`] = `
undefined,
]
}
testID="amplify__radio-button__dot"
/>
</View>
<Text
Expand Down Expand Up @@ -1435,6 +1440,7 @@ exports[`RadioGroup renders default RadioGroup as expected 1`] = `
undefined,
]
}
testID="amplify__radio-button__dot"
/>
</View>
<Text
Expand Down
3 changes: 2 additions & 1 deletion packages/react-native/src/primitives/RadioGroup/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ export interface RadioGroupProps<T>
| 'labelStyle'
| 'onChange'
| 'size'
| 'value'
>,
ViewProps {
children: React.ReactElement<RadioProps<T>>[];
direction?: Direction;
initialValue?: T;
onValueChange?: (value?: T) => void;
}

export interface RadioGroupStyles {
Expand Down