Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #7313, TriStateCheckbox: Warning: value prop on input should not be null. Consider using an empty string to clear the component or undefined for uncontrolled components. #7315

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions components/lib/tristatecheckbox/TriStateCheckbox.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { useState, useEffect } from 'react';
import { PrimeReactContext, ariaLabel } from '../api/Api';
import { useHandleStyle } from '../componentbase/ComponentBase';
import { useMergeProps, useMountEffect } from '../hooks/Hooks';
Expand All @@ -14,6 +15,7 @@ export const TriStateCheckbox = React.memo(
const context = React.useContext(PrimeReactContext);
const props = TriStateCheckboxBase.getProps(inProps, context);

const [checkBoxValue, setCheckBoxValue] = useState('');
const elementRef = React.useRef(null);

const { ptm, cx, isUnstyled } = TriStateCheckboxBase.setMetaData({
Expand All @@ -22,19 +24,27 @@ export const TriStateCheckbox = React.memo(

useHandleStyle(TriStateCheckboxBase.css.styles, isUnstyled, { name: 'tristatecheckbox' });

useEffect(() => {
if ([true, false, ''].includes(props.value)) {
setCheckBoxValue(props.value);
} else {
setCheckBoxValue('');
}
}, [props.value]);

const onChange = (event) => {
if (props.disabled || props.readOnly) {
return;
}

let newValue;

if (props.value === null || props.value === undefined) {
if (checkBoxValue === '') {
newValue = true;
} else if (props.value === true) {
} else if (checkBoxValue === true) {
newValue = false;
} else if (props.value === false) {
newValue = null;
} else if (checkBoxValue === false) {
newValue = '';
}

if (props.onChange) {
Expand Down Expand Up @@ -101,16 +111,16 @@ export const TriStateCheckbox = React.memo(

let icon;

if (props.value === false) {
if (checkBoxValue === false) {
icon = props.uncheckIcon || <TimesIcon {...uncheckIconProps} />;
} else if (props.value === true) {
} else if (checkBoxValue === true) {
icon = props.checkIcon || <CheckIcon {...checkIconProps} />;
}

const checkIcon = IconUtils.getJSXIcon(icon, { ...checkIconProps }, { props });

const ariaValueLabel = props.value ? ariaLabel('trueLabel') : props.value === false ? ariaLabel('falseLabel') : ariaLabel('nullLabel');
const ariaChecked = props.value ? 'true' : 'false';
const ariaValueLabel = checkBoxValue ? ariaLabel('trueLabel') : checkBoxValue === false ? ariaLabel('falseLabel') : ariaLabel('nullLabel');
const ariaChecked = checkBoxValue ? 'true' : 'false';

const boxProps = mergeProps(
{
Expand Down Expand Up @@ -152,8 +162,8 @@ export const TriStateCheckbox = React.memo(
'aria-invalid': props.invalid,
disabled: props.disabled,
readOnly: props.readOnly,
value: props.value,
checked: props.value,
value: checkBoxValue,
checked: checkBoxValue,
onChange: onChange
},
ptm('input')
Expand Down
4 changes: 2 additions & 2 deletions components/lib/tristatecheckbox/TriStateCheckboxBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { classNames } from '../utils/Utils';
const classes = {
root: ({ props, context }) =>
classNames('p-tristatecheckbox p-checkbox p-component', {
'p-highlight': props.value !== null,
'p-highlight': props.value !== '' && props.value !== null,
'p-disabled': props.disabled,
'p-invalid': props.invalid,
'p-variant-filled': props.variant ? props.variant === 'filled' : context && context.inputStyle === 'filled'
Expand All @@ -31,7 +31,7 @@ export const TriStateCheckboxBase = ComponentBase.extend({
tooltip: null,
tooltipOptions: null,
uncheckIcon: null,
value: null,
value: '',
children: undefined
},
css: {
Expand Down
Loading