Skip to content

Commit

Permalink
fix(Checkbox): remove mutating ref
Browse files Browse the repository at this point in the history
- see #6920

---

> Mutating a value returned from a function whose return value should not be mutated

Скрываем мутацию от компилятора
  • Loading branch information
SevereCloud committed Jul 9, 2024
1 parent e728d24 commit 7f938e9
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/vkui/src/components/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import { Text } from '../Typography/Text/Text';
import { VisuallyHidden } from '../VisuallyHidden/VisuallyHidden';
import styles from './Checkbox.module.css';

function setIndeterminate(el: HTMLInputElement, indeterminate: boolean) {
el.indeterminate = indeterminate;
}

const sizeYClassNames = {
none: styles['Checkbox--sizeY-none'],
['compact']: styles['Checkbox--sizeY-compact'],
Expand Down Expand Up @@ -68,10 +72,10 @@ export const Checkbox = ({
const { sizeY: adaptiveSizeY } = useAdaptivityConditionalRender();

React.useEffect(() => {
const indeterminateValue = indeterminate === undefined ? defaultIndeterminate : indeterminate;
const indeterminateValue = indeterminate ?? Boolean(defaultIndeterminate);

if (inputRef.current) {
inputRef.current.indeterminate = Boolean(indeterminateValue);
setIndeterminate(inputRef.current, indeterminateValue);
}
}, [defaultIndeterminate, indeterminate, inputRef]);

Expand All @@ -80,17 +84,16 @@ export const Checkbox = ({
if (
defaultIndeterminate !== undefined &&
indeterminate === undefined &&
restProps.checked === undefined &&
inputRef.current
restProps.checked === undefined

Check warning on line 87 in packages/vkui/src/components/Checkbox/Checkbox.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/Checkbox/Checkbox.tsx#L87

Added line #L87 was not covered by tests
) {
inputRef.current.indeterminate = false;
event.currentTarget.indeterminate = false;

Check warning on line 89 in packages/vkui/src/components/Checkbox/Checkbox.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/Checkbox/Checkbox.tsx#L89

Added line #L89 was not covered by tests
}
if (indeterminate !== undefined && inputRef.current) {
inputRef.current.indeterminate = indeterminate;
if (indeterminate !== undefined) {
event.currentTarget.indeterminate = indeterminate;

Check warning on line 92 in packages/vkui/src/components/Checkbox/Checkbox.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/Checkbox/Checkbox.tsx#L92

Added line #L92 was not covered by tests
}
onChange && onChange(event);
},
[defaultIndeterminate, indeterminate, restProps.checked, onChange, inputRef],
[defaultIndeterminate, indeterminate, restProps.checked, onChange],
);

if (process.env.NODE_ENV === 'development') {
Expand Down

0 comments on commit 7f938e9

Please sign in to comment.