Skip to content

Commit

Permalink
Merge branch 'v2' into 371-when-the-rows-are-pinned-scrolling-up-or-d…
Browse files Browse the repository at this point in the history
…own-overlaps-with-the-pinned-rows
  • Loading branch information
alessandrojcm committed Jul 11, 2024
2 parents fa46675 + cf06e45 commit f038e1b
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 9 deletions.
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
56 changes: 56 additions & 0 deletions packages/mantine-react-table/stories/features/Editing.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
MRT_EditActionButtons,
type MRT_TableOptions,
MantineReactTable,
MRT_ColumnDef,
} from '../../src';
import { faker } from '@faker-js/faker';
import { type Meta } from '@storybook/react';
Expand Down Expand Up @@ -79,6 +80,7 @@ type Person = {
lastName: string;
phoneNumber: string;
state: string;
visitedStates: string[];
};

const data: Person[] = [...Array(100)].map(() => ({
Expand All @@ -87,6 +89,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 @@ -364,6 +367,59 @@ export const CustomEditModal = () => {
/>
);
};
const multiSelectColumns: MRT_ColumnDef<Person>[] = [
{
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,
},
Cell: ({ cell }) => {
return (cell.getValue() as string[]).join(', ');
},
},
{
accessorKey: 'phoneNumber',
header: 'Phone Number',
},
];
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={multiSelectColumns}
data={tableData}
editDisplayMode="row"
enableEditing
enableRowActions
onEditingRowSave={handleSaveRow}
/>
);
};

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

0 comments on commit f038e1b

Please sign in to comment.