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

feat: add multi-select option to column editVariant parameter #357

Merged
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 @@ -134,7 +134,7 @@ export const columnOptions: ColumnOption[] = [
linkText: 'MRT Editing Docs',
source: 'MRT',
required: false,
type: "'select' | 'text'",
type: "'select' | 'text' | 'multi-select'",
},
{
columnOption: 'enableClickToCopy',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
import { type FocusEvent, type KeyboardEvent, useState } from 'react';
import { Select, TextInput, type TextInputProps } from '@mantine/core';
import {
MultiSelect,
Select,
TextInput,
type TextInputProps,
type SelectProps,
type MultiSelectProps
} from '@mantine/core';
import {
type HTMLPropsRef,
type MRT_Cell,
type MRT_CellValue,
type MRT_RowData,
type MRT_TableInstance,
} from '../../types';
import { parseFromValuesOrFunc } from '../../utils/utils';

interface Props<TData extends MRT_RowData, TValue = MRT_CellValue>

interface PropsTextInput<TData extends MRT_RowData, TValue = MRT_CellValue>
extends TextInputProps {
cell: MRT_Cell<TData, TValue>;
table: MRT_TableInstance<TData>;
}

interface PropsSelect<TData extends MRT_RowData, TValue = MRT_CellValue>
extends SelectProps {
cell: MRT_Cell<TData, TValue>;
table: MRT_TableInstance<TData>;
}

interface PropsMultiSelect<TData extends MRT_RowData, TValue = MRT_CellValue>
extends MultiSelectProps {
cell: MRT_Cell<TData, TValue>;
table: MRT_TableInstance<TData>;
}

type MRT_TextInputProps = TextInputProps & HTMLPropsRef<HTMLInputElement>
type MRT_SelectProps = SelectProps & HTMLPropsRef<HTMLInputElement>
type MRT_MultiSelectProps = MultiSelectProps & HTMLPropsRef<HTMLInputElement>

export const MRT_EditCellTextInput = <TData extends MRT_RowData>({
cell,
table,
...rest
}: Props<TData>) => {
}: PropsTextInput<TData> | PropsSelect<TData> | PropsMultiSelect<TData>) => {
const {
getState,
options: {
Expand All @@ -39,6 +64,7 @@ export const MRT_EditCellTextInput = <TData extends MRT_RowData>({
const isCreating = creatingRow?.id === row.id;
const isEditing = editingRow?.id === row.id;
const isSelectEdit = columnDef.editVariant === 'select';
const isMultiSelectEdit = columnDef.editVariant === 'multi-select';

const [value, setValue] = useState(() => cell.getValue<any>());

Expand All @@ -47,7 +73,7 @@ export const MRT_EditCellTextInput = <TData extends MRT_RowData>({
...parseFromValuesOrFunc(mantineEditTextInputProps, arg),
...parseFromValuesOrFunc(columnDef.mantineEditTextInputProps, arg),
...rest,
};
} as MRT_TextInputProps;

const selectProps = {
...parseFromValuesOrFunc(mantineEditSelectProps, arg),
Expand Down Expand Up @@ -105,15 +131,42 @@ export const MRT_EditCellTextInput = <TData extends MRT_RowData>({

if (isSelectEdit) {
return (
// @ts-ignore
<Select
{...commonProps}
searchable
value={value as any}
{...(selectProps as MRT_SelectProps)}
onBlur={handleBlur}
onChange={(value) => {
(selectProps as MRT_SelectProps).onChange?.(value as any);
setValue(value);
}}
onClick={(e) => {
e.stopPropagation();
selectProps?.onClick?.(e);
}}
ref={(node) => {
if (node) {
editInputRefs.current[cell.id] = node;
if (selectProps.ref) {
selectProps.ref.current = node;
}
}
}}
/>
);
}

if (isMultiSelectEdit) {
return (
<MultiSelect
{...commonProps}
searchable
value={value}
{...selectProps}
{...(selectProps as MRT_MultiSelectProps)}
onBlur={handleBlur}
onChange={(value) => {
selectProps.onChange?.(value as any);
(selectProps as MRT_MultiSelectProps).onChange?.(value as any);
setValue(value);
}}
onClick={(e) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/mantine-react-table/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ export type MRT_ColumnDef<TData extends MRT_RowData, TValue = unknown> = Omit<
LiteralUnion<string & MRT_FilterOption>
> | null;
columns?: MRT_ColumnDef<TData>[];
editVariant?: 'select' | 'text';
editVariant?: 'select' | 'text' | 'multi-select';
enableClickToCopy?: ((cell: MRT_Cell<TData>) => boolean) | boolean;
enableColumnActions?: boolean;
enableColumnDragging?: boolean;
Expand Down
52 changes: 52 additions & 0 deletions packages/mantine-react-table/stories/features/Editing.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type Person = {
lastName: string;
phoneNumber: string;
state: string;
visitedStates: string[];
};

const data: Person[] = [...Array(100)].map(() => ({
Expand All @@ -87,6 +88,7 @@ const data: Person[] = [...Array(100)].map(() => ({
lastName: faker.person.lastName(),
phoneNumber: faker.phone.number(),
state: faker.location.state(),
visitedStates: faker.helpers.multiple(faker.location.state),
}));

export const EditingEnabledEditModeModalDefault = () => {
Expand Down Expand Up @@ -365,6 +367,56 @@ export const CustomEditModal = () => {
);
};

export const EditMultiSelectVariant = () => {
const [tableData, setTableData] = useState(data);

const handleSaveRow: MRT_TableOptions<Person>['onEditingRowSave'] = ({
exitEditingMode,
row,
values,
}) => {
tableData[+row.index] = values;
setTableData([...tableData]);
exitEditingMode();
};

return (
<MantineReactTable
columns={[
{
accessorKey: 'firstName',
header: 'First Name',
},
{
accessorKey: 'lastName',
header: 'Last Name',
},
{
accessorKey: 'address',
header: 'Address',
},
{
accessorKey: 'visitedStates',
editVariant: 'multi-select',
header: 'Visited States',
mantineEditSelectProps: {
data: usStates as any,
},
},
{
accessorKey: 'phoneNumber',
header: 'Phone Number',
},
]}
data={tableData}
editDisplayMode="row"
enableEditing
enableRowActions
onEditingRowSave={handleSaveRow}
/>
);
};

export const EditSelectVariant = () => {
const [tableData, setTableData] = useState(data);

Expand Down