Skip to content

Commit f037bf5

Browse files
author
Hector Arce De Las Heras
committed
Update checkbox
Update checkbox logic and states
1 parent 3385d73 commit f037bf5

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/components/checkbox/checkboxControlled.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const CheckboxControlledComponent = React.forwardRef(
1818
props.variant,
1919
props.ctv
2020
);
21-
const state = getCheckBoxState(props.checked, props.disabled, props.error);
21+
const state = getCheckBoxState(props.checked, props.disabled, props.error, styles);
2222

2323
return <CheckboxStandAlone ref={ref} state={state} styles={styles[state]} {...props} />;
2424
}

src/components/checkbox/types/state.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ export enum CheckboxStateType {
77
DEFAULT_SELECTED = 'DEFAULT_SELECTED',
88
DISABLED_UNSELECTED = 'DISABLED_UNSELECTED',
99
DISABLED_SELECTED = 'DISABLED_SELECTED',
10+
ERROR_UNSELECTED = 'ERROR_UNSELECTED',
11+
ERROR_SELECTED = 'ERROR_SELECTED',
12+
// deprecated - This `error` state will be deprecated in the future
1013
ERROR = 'ERROR',
1114
}

src/components/checkbox/utils/state.utils.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
import { CheckboxStateType } from '../types';
1+
import { CheckboxPropsStateStylesType, CheckboxStateType } from '../types';
22

33
// eslint-disable-next-line complexity
44
const getCheckBoxState = (
55
checked = false,
66
disabled = false,
7-
isError = false
7+
error = false,
8+
// deprecated - Remove style from de method when the deprecated ERROR state is removed
9+
styles?: CheckboxPropsStateStylesType
810
): CheckboxStateType => {
9-
if (isError) {
11+
if (error && !checked && styles?.[CheckboxStateType.ERROR_UNSELECTED]) {
12+
return CheckboxStateType.ERROR_UNSELECTED;
13+
}
14+
if (error && checked && styles?.[CheckboxStateType.ERROR_SELECTED]) {
15+
return CheckboxStateType.ERROR_SELECTED;
16+
}
17+
// deprecated - This `error` state will be deprecated in the future
18+
if (error) {
1019
return CheckboxStateType.ERROR;
1120
}
1221
if (disabled && !checked) {
@@ -36,11 +45,17 @@ export const checkboxState = (
3645
hasError: boolean;
3746
} => {
3847
const isChecked =
39-
state === CheckboxStateType.DEFAULT_SELECTED || state === CheckboxStateType.DISABLED_SELECTED;
48+
state === CheckboxStateType.DEFAULT_SELECTED ||
49+
state === CheckboxStateType.DISABLED_SELECTED ||
50+
state === CheckboxStateType.ERROR_SELECTED;
4051
const isDisabled =
4152
state === CheckboxStateType.DISABLED_UNSELECTED ||
4253
state === CheckboxStateType.DISABLED_SELECTED;
43-
const hasError = state === CheckboxStateType.ERROR;
54+
const hasError =
55+
state === CheckboxStateType.ERROR_SELECTED ||
56+
state === CheckboxStateType.ERROR_UNSELECTED ||
57+
// deprecated - This `error` state will be deprecated in the future
58+
state === CheckboxStateType.ERROR;
4459
return { isChecked, isDisabled, hasError };
4560
};
4661

0 commit comments

Comments
 (0)