Skip to content

Commit

Permalink
refactor: refactoring after review
Browse files Browse the repository at this point in the history
  • Loading branch information
PKulkoRaccoonGang committed Oct 12, 2023
1 parent 649baf4 commit b0752da
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 39 deletions.
18 changes: 11 additions & 7 deletions src/Form/FormCheckbox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,27 @@ const FormCheckbox = React.forwardRef(({
floatLabelLeft,
...props
}, ref) => {
const { getCheckboxControlProps, hasCheckboxSetProvider } = useCheckboxSetContext();
const { hasCheckboxSetProvider } = useCheckboxSetContext();
const { hasFormGroupProvider, useSetIsControlGroupEffect, getControlProps } = useFormGroupContext();
useSetIsControlGroupEffect(true);
const shouldActAsGroup = hasFormGroupProvider && !hasCheckboxSetProvider;
const groupProps = shouldActAsGroup ? {
...getControlProps({}),
role: 'group',
} : {};
const checkboxInputProps = getCheckboxControlProps({
...props,
className: controlClassName,
});

const control = React.createElement(controlAs, { ...props, className: controlClassName, ref });
return (
<FormGroupContextProvider
controlId={checkboxInputProps.id}
controlId={props.id}
isInvalid={isInvalid}
isValid={isValid}
>
<div
className={classNames('pgn__form-checkbox', className, {
'pgn__form-control-valid': isValid,
'pgn__form-control-invalid': isInvalid,
'pgn__form-control-disabled': checkboxInputProps.disabled,
'pgn__form-control-disabled': props.disabled,
'pgn__form-control-label-left': !!floatLabelLeft,
})}
{...groupProps}
Expand All @@ -107,6 +104,8 @@ const FormCheckbox = React.forwardRef(({
});

FormCheckbox.propTypes = {
/** Specifies id of the FormCheckbox component. */
id: PropTypes.string,
/** Specifies contents of the component. */
children: PropTypes.node.isRequired,
/** Specifies class name to append to the base element. */
Expand All @@ -123,10 +122,14 @@ FormCheckbox.propTypes = {
isValid: PropTypes.bool,
/** Specifies control element. */
controlAs: PropTypes.elementType,
/** Specifies whether the floating label should be aligned to the left. */
floatLabelLeft: PropTypes.bool,
/** Specifies whether the `FormCheckbox` is disabled. */
disabled: PropTypes.bool,
};

FormCheckbox.defaultProps = {
id: undefined,
className: undefined,
controlClassName: undefined,
labelClassName: undefined,
Expand All @@ -135,6 +138,7 @@ FormCheckbox.defaultProps = {
isValid: false,
controlAs: CheckboxControl,
floatLabelLeft: false,
disabled: false,
};

export { CheckboxControl };
Expand Down
63 changes: 31 additions & 32 deletions src/Form/FormRadio.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,42 +40,37 @@ const FormRadio = React.forwardRef(({
isInvalid,
isValid,
...props
}, ref) => {
const { getRadioControlProps } = useRadioSetContext();
const radioInputProps = getRadioControlProps({
...props,
className: controlClassName,
});
return (
<FormGroupContextProvider
controlId={radioInputProps.id}
isInvalid={isInvalid}
isValid={isValid}
}, ref) => (
<FormGroupContextProvider
controlId={props.id}
isInvalid={isInvalid}
isValid={isValid}
>
<div
className={classNames('pgn__form-radio', className, {
'pgn__form-control-valid': isValid,
'pgn__form-control-invalid': isInvalid,
'pgn__form-control-disabled': props.disabled,
})}
>
<div
className={classNames('pgn__form-radio', className, {
'pgn__form-control-valid': isValid,
'pgn__form-control-invalid': isInvalid,
'pgn__form-control-disabled': radioInputProps.disabled,
})}
>
<RadioControl ref={ref} className={controlClassName} {...props} />
<div>
<FormLabel className={labelClassName}>
{children}
</FormLabel>
{description && (
<FormControlFeedback hasIcon={false}>
{description}
</FormControlFeedback>
)}
</div>
<RadioControl ref={ref} className={controlClassName} {...props} />
<div>
<FormLabel className={labelClassName}>
{children}
</FormLabel>
{description && (
<FormControlFeedback hasIcon={false}>
{description}
</FormControlFeedback>
)}
</div>
</FormGroupContextProvider>
);
});
</div>
</FormGroupContextProvider>
));

FormRadio.propTypes = {
/** Specifies id of the FormRadio component. */
id: PropTypes.string,
/** Specifies contents of the component. */
children: PropTypes.node.isRequired,
/** Specifies class name to append to the base element. */
Expand All @@ -90,15 +85,19 @@ FormRadio.propTypes = {
isInvalid: PropTypes.bool,
/** Specifies whether to display component in valid state, this affects styling. */
isValid: PropTypes.bool,
/** Specifies whether the `FormRadio` is disabled. */
disabled: PropTypes.bool,
};

FormRadio.defaultProps = {
id: undefined,
className: undefined,
controlClassName: undefined,
labelClassName: undefined,
description: undefined,
isInvalid: false,
isValid: false,
disabled: false,
};

export { RadioControl };
Expand Down
1 change: 1 addition & 0 deletions src/Menu/__snapshots__/Menu.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ exports[`Menu Item renders correctly renders as expected with menu items 1`] = `
>
<input
className="pgn__form-checkbox-input"
disabled={false}
id="form-field1"
type="checkbox"
/>
Expand Down

0 comments on commit b0752da

Please sign in to comment.