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(DataGrid): add checkbox support #5380

Merged
merged 18 commits into from
Jun 25, 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
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ $row-heights: (
align-items: center;
}

.#{variables.$block-class}__inline-edit--outer-cell-checkbox-focus
.cds--checkbox-label::before {
outline: $spacing-01 solid $focus;
outline-offset: 1px;
}

.#{variables.$block-class}__inline-edit--outer-cell-checkbox {
display: flex;
height: -webkit-fill-available;
justify-content: center;
padding-inline: $spacing-05;
}

.#{variables.$block-class}__static--outer-cell {
display: flex;
height: -webkit-fill-available;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ InlineEditButton.propTypes = {
nonEditCell: PropTypes.bool,
placeholder: PropTypes.string,
renderIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
type: PropTypes.oneOf(['text', 'number', 'selection', 'date']),
type: PropTypes.oneOf(['text', 'number', 'selection', 'date', 'checkbox']),
value: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
Dropdown,
DatePicker,
DatePickerInput,
Checkbox,
} from '@carbon/react';
import { Edit, CaretSort, ChevronDown, Calendar } from '@carbon/react/icons';
import { InlineEditButton } from '../InlineEditButton';
Expand Down Expand Up @@ -55,6 +56,7 @@ export const InlineEditCell = ({
const { inputProps } = config || {};

const textInputRef = useRef();
const checkboxRef = useRef();
const numberInputRef = useRef();
const dropdownRef = useRef();
const datePickerRef = useRef();
Expand Down Expand Up @@ -240,9 +242,29 @@ export const InlineEditCell = ({
const handleKeyDown = (event) => {
const { key } = event;
switch (key) {
case 'ArrowRight':
case 'ArrowLeft':
case 'ArrowUp':
case 'ArrowDown':
if (inEditMode && event.target.type === 'checkbox') {
const newCellId = getNewCellId(key);
saveCellData(cellValue);
setInitialValue(cellValue);
dispatch({ type: 'EXIT_EDIT_MODE', payload: newCellId });
setInEditMode(false);
sendFocusBackToGrid();
}
break;
// Save cell contents to data
case 'Tab':
case 'Enter': {
if (type === 'checkbox') {
// Since checkbox doesn't need to click into it to enter `inEditMode` we don't need to check for it
const newCellId = getNewCellId(key);
dispatch({ type: 'EXIT_EDIT_MODE', payload: newCellId });
setInEditMode(false);
sendFocusBackToGrid();
}
if (inEditMode) {
// Dropdown saves are handled in the Dropdown's/DatePicker's onChange prop
if (type === 'selection' || type === 'date') {
Expand Down Expand Up @@ -463,6 +485,29 @@ export const InlineEditCell = ({
);
};

const renderCheckBoxCell = () => {
return (
<Checkbox
labelText={cellLabel || 'Checkbox'}
{...inputProps}
className={cx(`${blockClass}__inline-edit--outer-cell-checkbox`, {
[`${blockClass}__inline-edit--outer-cell-checkbox-focus`]:
activeCellId === cellId,
})}
id={cellId}
hideLabel
checked={cellValue}
onChange={(event, { checked }) => {
setCellValue(checked);
if (inputProps.onChange) {
inputProps.onChange(checked);
}
}}
ref={checkboxRef}
/>
);
};

const renderTextInput = () => {
const { validator } = config || {};
const isInvalid = validator?.(cellValue);
Expand Down Expand Up @@ -524,8 +569,11 @@ export const InlineEditCell = ({
[`${blockClass}__static--outer-cell`]: !disabledCell,
})}
>
{!nonEditCell && !disabledCell && renderRegularCell()}
{(!inEditMode || disabledCell) && (
{!nonEditCell &&
!disabledCell &&
type !== 'checkbox' &&
renderRegularCell()}
{(!inEditMode || disabledCell) && type !== 'checkbox' && (
<InlineEditButton
isActiveCell={cellId === activeCellId}
renderIcon={setRenderIcon()}
Expand All @@ -539,6 +587,7 @@ export const InlineEditCell = ({
type={type}
/>
)}
{type === 'checkbox' && renderCheckBoxCell()}
{!nonEditCell && inEditMode && cellId === activeCellId && (
<>
{type === 'text' && renderTextInput()}
Expand Down Expand Up @@ -566,7 +615,7 @@ InlineEditCell.propTypes = {
nonEditCell: PropTypes.bool,
placeholder: PropTypes.string,
tabIndex: PropTypes.number,
type: PropTypes.oneOf(['text', 'number', 'selection', 'date']),
type: PropTypes.oneOf(['text', 'number', 'selection', 'date', 'checkbox']),
value: PropTypes.oneOfType([
PropTypes.string,
PropTypes.node,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ export const handleGridKeyPress = ({

// Stop grid key listener when in edit mode
const isEditing =
(document.activeElement.id === activeCellId &&
(focusedCell.getAttribute('data-inline-type') !== 'checkbox' &&
document.activeElement.id === activeCellId &&
document.activeElement.id === editId) ||
dropdownIsActive() ||
datePickerIsActive();
Expand All @@ -106,6 +107,9 @@ export const handleGridKeyPress = ({
}
const isDisabledCell =
focusedCell.getAttribute('data-disabled') === 'false' ? false : true;
const isEditableCell = !event.target.classList.contains(
`${blockClass}__inline-edit-button--non-edit`
);
const sharedUpdateParams = {
oldId: activeCellId,
instance,
Expand Down Expand Up @@ -212,7 +216,7 @@ export const handleGridKeyPress = ({
case 'F2':
case 'Enter': {
// Disabled cells are not allowed to go into edit mode
if (isDisabledCell) {
if (isDisabledCell || !isEditableCell) {
return;
}
// Only go into edit mode if there is no editId, meaning that we're not already in edit mode
Expand All @@ -225,6 +229,13 @@ export const handleGridKeyPress = ({
dropdownTrigger?.click();
}, 1);
}
if (focusedType === 'checkbox') {
setTimeout(() => {
const checkboxTrigger = focusedCell.querySelector('input');
checkboxTrigger?.click();
checkboxTrigger?.focus();
}, 1);
}
if (focusedType === 'date') {
setTimeout(() => {
const dateInputTrigger = focusedCell.querySelector('input');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const useInlineEdit = (hooks: Hooks) => {
{!staticCell &&
inlineEditType === 'selection' &&
renderInlineEditComponent(inlineEditType)}
{inlineEditType === 'checkbox' &&
renderInlineEditComponent(inlineEditType)}
{!staticCell &&
inlineEditType === 'date' &&
renderInlineEditComponent(inlineEditType)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ export const getInlineEditColumns = () => {
}, // These props are passed to the Carbon component used for inline editing
},
},
{
Header: 'Key',
accessor: 'key',
inlineEdit: {
type: 'checkbox',
inputProps: {
onChange: (newDateObj, cell) => {
console.log(newDateObj, cell);
},
},
},
},
{
Header: 'Active since',
accessor: 'activeSince',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ const newPerson = (index, config) => {
: initialChartTypeIndex === 1
? inlineEditSelectItems[1]
: inlineEditSelectItems[2],
key: false,
activeSince:
activeChance > 0.66
? activeSinceDate
Expand Down
Loading