Skip to content

Commit 4ab2675

Browse files
author
Siyeop
committed
feature. Add readOnly props on CheckBoxGroup
1 parent 70569b6 commit 4ab2675

File tree

7 files changed

+57
-9
lines changed

7 files changed

+57
-9
lines changed

src/components/input/checkbox-group/TCheckboxGroup.interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import {CSSProperties} from 'react';
21
import {TValidatorProps} from '@/common/validator/TValidator.interface';
3-
import {TCheckboxValue} from '../checkbox/TCheckbox.interface';
2+
import {TCheckboxValue} from '@/components';
43
import {TBaseProps} from '@/common/base/TBase.interface';
54

65
export type TCheckboxGroupValue = TCheckboxValue[];
@@ -12,6 +11,7 @@ export interface TCheckboxGroupProps extends TBaseProps, TValidatorProps {
1211
value: TCheckboxGroupValue,
1312
items: TCheckboxGroupItem[],
1413
disabled?: boolean,
14+
readOnly?: boolean,
1515

1616
textKey?: string,
1717
valueKey?: string,

src/components/input/checkbox-group/TCheckboxGroup.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const TCheckboxGroup = forwardRef((props: TCheckboxGroupProps, ref: Ref<TCheckbo
2828

2929
if (props.className) { clazz.push(props.className); }
3030
if (props.disabled) { clazz.push('t-checkbox-group--disabled'); }
31+
if (props.readOnly) { clazz.push('t-checkbox-group--read-only'); }
3132
if (!validator.result) { clazz.push('t-checkbox-group--failure'); }
3233
if (validator.result && validator.message) { clazz.push('t-checkbox-group--success'); }
3334

@@ -105,7 +106,9 @@ const TCheckboxGroup = forwardRef((props: TCheckboxGroupProps, ref: Ref<TCheckbo
105106
negativeValue={null}
106107
value={props.value.some((v) => v === item[props.valueKey]) ? item[props.valueKey] : null}
107108
onChange={onChangeChildren}
108-
disabled={props.disabled || item.disabled}>
109+
disabled={props.disabled || item.disabled}
110+
readOnly={props.readOnly || item.readOnly}
111+
>
109112
{props.labelTemplate ? props.labelTemplate(item) : item[props.textKey]}
110113
</TCheckbox>
111114
))

src/components/input/checkbox/TCheckbox.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const TCheckbox = forwardRef((props: TCheckboxProps, ref: Ref<TCheckboxRef>) =>
3939
const clazz: string[] = [];
4040

4141
if (props.className) { clazz.push(props.className); }
42-
if (props.readOnly) { clazz.push('t-checkbox--readOnly'); }
42+
if (props.readOnly) { clazz.push('t-checkbox--read-only'); }
4343
if (props.disabled) { clazz.push('t-checkbox--disabled'); }
4444
if (!validator.result) { clazz.push('t-checkbox--failure'); }
4545
if (validator.result && validator.message) { clazz.push('t-checkbox--success'); }

src/styles/component/input/checkbox-group/TCheckboxGroup.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
@include typo-element-3;
1414
}
1515

16-
&.t-checkbox-group--disabled {
16+
&.t-checkbox-group--disabled,
17+
&.t-checkbox-group--read-only {
1718
pointer-events: none;
1819
}
1920

src/styles/component/input/checkbox/TCheckbox.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
}
3838
}
3939

40-
&.t-checkbox--readOnly .t-checkbox__container {
40+
&.t-checkbox--read-only .t-checkbox__container {
4141
pointer-events: none;
4242
}
4343

tests/unit/react/tks/component/input/checkbox-group/TCheckboxGroup.test.tsx

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,25 @@ describe('TCheckboxGroup', () => {
8080

8181
});
8282

83+
it('When readOnly prop is applied, root has t-checkbox-group--read-only class', () => {
84+
85+
// Arrange
86+
const testValue = [];
87+
const testItems = [
88+
{text: 'Apple', koreanText: '사과', value: 'apple', value2: 'a'},
89+
{text: 'Banana', koreanText: '바나나', value: 'banana', value2: 'b'},
90+
];
91+
92+
render(<TCheckboxGroup readOnly={true} onChange={mockFn} value={testValue} items={testItems}/>);
93+
94+
const root = screen.getByTestId('t-checkbox-group-root');
95+
96+
// Assert
97+
expect(root).toHaveClass('t-checkbox-group--read-only');
98+
99+
});
100+
101+
83102
it('When disabled prop is applied, root will be applied -1 to tabIndex', () => {
84103

85104
// Arrange
@@ -383,7 +402,7 @@ describe('TCheckboxGroup', () => {
383402

384403
});
385404

386-
it('When disabled attribute is applied to an item, that item should not be changed', () => {
405+
it('When disabled attribute is applied to an item, that item should not be changed', () => {
387406

388407
// Arrange
389408
const testValue = [];
@@ -402,6 +421,31 @@ describe('TCheckboxGroup', () => {
402421

403422
});
404423

424+
it('When readOnly attribute is applied to an item, that item should not be changed', () => {
425+
426+
// Arrange
427+
const testValue = [];
428+
const testItems = [
429+
{text: 'Apple', koreanText: '사과', value: 'apple', value2: 'a'},
430+
{text: 'Banana', koreanText: '바나나', value: 'banana', value2: 'b'},
431+
{text: 'ReadOnly', koreanText: '선택 불가 과일', value: 'readonly', value2: 'd2', readOnly: true},
432+
];
433+
434+
render(
435+
<TCheckboxGroup onChange={mockFn}
436+
value={testValue}
437+
items={testItems}
438+
/>,
439+
);
440+
441+
const checkboxButtons = screen.getAllByTestId('t-checkbox-root');
442+
443+
// Assert
444+
expect(checkboxButtons[2]).toHaveClass('t-checkbox--read-only');
445+
446+
});
447+
448+
405449
it('When textKey prop is applied, the text will have the key of that item', () => {
406450

407451
// Arrange

tests/unit/react/tks/component/input/checkbox/TCheckbox.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ describe('TCheckbox', () => {
5454

5555
});
5656

57-
it('When readOnly prop is applied, root has t-checkbox--readOnly class', () => {
57+
it('When readOnly prop is applied, root has t-checkbox--read-only class', () => {
5858

5959
// Arrange
6060
render(<TCheckbox readOnly>Test</TCheckbox>);
6161

6262
const root = screen.getByTestId('t-checkbox-root');
6363

6464
// Assert
65-
expect(root).toHaveClass('t-checkbox--readOnly');
65+
expect(root).toHaveClass('t-checkbox--read-only');
6666

6767
});
6868

0 commit comments

Comments
 (0)