Skip to content

Commit

Permalink
fix(radio-group): add forwardRef
Browse files Browse the repository at this point in the history
BREAKING CHANGE: component is now using fowardRef
  • Loading branch information
richbachman committed Dec 17, 2020
1 parent 6061bfe commit d467043
Showing 1 changed file with 27 additions and 33 deletions.
60 changes: 27 additions & 33 deletions packages/paste-core/components/radio-group/src/RadioGroup.tsx
Expand Up @@ -8,41 +8,35 @@ export interface RadioGroupProps extends InlineControlGroupProps {
onChange: (value: string) => void;
}

const RadioGroup: React.FC<RadioGroupProps> = ({
name,
value,
onChange,
disabled = false,
errorText,
children,
...props
}) => {
const onChangeHandler = React.useMemo(() => {
return (event: React.ChangeEvent<HTMLInputElement>): void => {
if (onChange != null) {
onChange(event.target.value);
}
};
}, [onChange]);
const RadioGroup = React.forwardRef<HTMLFieldSetElement, RadioGroupProps>(
({name, value, onChange, disabled = false, errorText, children, ...props}, ref) => {
const onChangeHandler = React.useMemo(() => {
return (event: React.ChangeEvent<HTMLInputElement>): void => {
if (onChange != null) {
onChange(event.target.value);
}
};
}, [onChange]);

const contextValue = React.useMemo(() => {
return {
name,
value: value || '',
disabled,
hasError: errorText != null,
onChange: onChangeHandler,
};
}, [name, value, disabled, errorText, onChangeHandler]);
const contextValue = React.useMemo(() => {
return {
name,
value: value || '',
disabled,
hasError: errorText != null,
onChange: onChangeHandler,
};
}, [name, value, disabled, errorText, onChangeHandler]);

return (
<RadioContext.Provider value={contextValue}>
<InlineControlGroup {...props} disabled={disabled} errorText={errorText} name={name}>
{children}
</InlineControlGroup>
</RadioContext.Provider>
);
};
return (
<RadioContext.Provider value={contextValue}>
<InlineControlGroup {...props} disabled={disabled} errorText={errorText} name={name} ref={ref}>
{children}
</InlineControlGroup>
</RadioContext.Provider>
);
}
);

RadioGroup.displayName = 'RadioGroup';

Expand Down

0 comments on commit d467043

Please sign in to comment.