-
Notifications
You must be signed in to change notification settings - Fork 99
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(withTableSettings): isSelected -> selected, isRequired -> required #1478
Merged
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3b1785c
chore: remove TableColumnSetupItem from export
GermanVor 4aec3d3
feat: new tableColumnSetup with old ableColumnSetupProps
GermanVor d677d5a
feat: add sticky support for withTableSettings
GermanVor 458d812
chore: new tableColumnSetup with old ableColumnSetupProps
GermanVor 3a68f6c
chore: absolute import path
GermanVor 7685c37
chore: fix test
GermanVor 5c2df09
chore: itemsById instead items.find
GermanVor 3e0caca
chore: unknown
GermanVor 260152e
chore: obsolete words
GermanVor 2c18968
chore: remove fors
GermanVor ace54b3
chore: add sticky for old TableColumnSetup
GermanVor a97ab2d
chore: remove isDragDisabled from Item
GermanVor be995eb
chore: remove warnings
GermanVor d309b2d
chore: remove prepareDndItems
GermanVor 79d33de
chore: prepareStickyState
GermanVor 1c17566
Merge branch 'main' into withTableSettings-TableColumnSetupItem
GermanVor 856c4c0
Merge branch 'main' into withTableSettings-TableColumnSetupItem
GermanVor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/components/Table/hoc/withTableSettings/TableColumnSetup/TableColumnSetup.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ import React from 'react'; | |
import {Gear, Grip, Lock} from '@gravity-ui/icons'; | ||
import {DragDropContext, Draggable, Droppable} from 'react-beautiful-dnd'; | ||
import type { | ||
DraggableChildrenFn, | ||
DraggableProvided, | ||
DraggableStateSnapshot, | ||
OnDragEndResponder, | ||
|
@@ -19,21 +20,17 @@ import type { | |
TreeSelectRenderContainer, | ||
TreeSelectRenderItem, | ||
} from '../../../../TreeSelect/types'; | ||
import type {ListItemViewProps} from '../../../../useList'; | ||
import type {ListItemCommonProps, ListItemViewProps} from '../../../../useList'; | ||
import {ListContainerView, ListItemView} from '../../../../useList'; | ||
import {block} from '../../../../utils/cn'; | ||
import type {TableColumnSetupItem, TableSetting} from '../withTableSettings'; | ||
import type {TableColumnConfig} from '../../../Table'; | ||
import type {TableSetting} from '../withTableSettings'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a circular dependency here, let's avoid such a case. |
||
|
||
import i18n from './i18n'; | ||
|
||
import './TableColumnSetup.scss'; | ||
|
||
function identity<T>(value: T): T { | ||
return value; | ||
} | ||
|
||
const b = block('table-column-setup'); | ||
const tableColumnSetupCn = b(null); | ||
const b = block('inner-table-column-setup'); | ||
const controlsCn = b('controls'); | ||
|
||
const reorderArray = <T extends unknown>(list: T[], startIndex: number, endIndex: number): T[] => { | ||
|
@@ -44,17 +41,35 @@ const reorderArray = <T extends unknown>(list: T[], startIndex: number, endIndex | |
return result; | ||
}; | ||
|
||
const prepareDndItems = (tableColumnItems: TableColumnSetupItem[]) => { | ||
return tableColumnItems.map<Item>((tableColumnItem) => { | ||
const hasSelectionIcon = tableColumnItem.isRequired === false; | ||
const prepareStickyState = ( | ||
itemsById: Record<string, TableColumnSetupItem>, | ||
visibleFlattenIds: string[], | ||
) => { | ||
let lastStickyStartIdx = 0; | ||
for (; lastStickyStartIdx !== visibleFlattenIds.length; lastStickyStartIdx++) { | ||
const visibleFlattenId = visibleFlattenIds[lastStickyStartIdx]; | ||
const item = itemsById[visibleFlattenId]; | ||
|
||
if (item?.sticky !== 'left' && item?.sticky !== 'start') { | ||
break; | ||
} | ||
} | ||
|
||
return { | ||
...tableColumnItem, | ||
startSlot: tableColumnItem.isRequired ? <Icon data={Lock} /> : undefined, | ||
hasSelectionIcon, | ||
selected: hasSelectionIcon ? tableColumnItem.isSelected : undefined, | ||
}; | ||
}); | ||
let firstStickyEndIdx = visibleFlattenIds.length; | ||
for (; firstStickyEndIdx !== 0; firstStickyEndIdx--) { | ||
const visibleFlattenId = visibleFlattenIds[firstStickyEndIdx - 1]; | ||
const item = itemsById[visibleFlattenId]; | ||
|
||
if (item?.sticky !== 'right' && item?.sticky !== 'end') { | ||
break; | ||
} | ||
} | ||
|
||
return { | ||
stickyStartItemIdList: visibleFlattenIds.slice(0, lastStickyStartIdx), | ||
sortableItemIdList: visibleFlattenIds.slice(lastStickyStartIdx, firstStickyEndIdx), | ||
stickyEndItemIdList: visibleFlattenIds.slice(firstStickyEndIdx), | ||
}; | ||
}; | ||
|
||
const prepareValue = (tableColumnItems: TableColumnSetupItem[]) => { | ||
|
@@ -70,10 +85,13 @@ const prepareValue = (tableColumnItems: TableColumnSetupItem[]) => { | |
}; | ||
|
||
interface RenderContainerProps { | ||
provided: DraggableProvided; | ||
snapshot: DraggableStateSnapshot; | ||
isDragDisabled?: boolean; | ||
provided?: DraggableProvided; | ||
snapshot?: DraggableStateSnapshot; | ||
} | ||
|
||
const RENDER_DRAG_DISABLED_CONTAINER_PROPS: RenderContainerProps = {isDragDisabled: true}; | ||
|
||
interface SwitcherProps { | ||
onKeyDown: React.KeyboardEventHandler<HTMLElement>; | ||
onClick: React.MouseEventHandler<HTMLElement>; | ||
|
@@ -86,50 +104,68 @@ interface UseDndRenderContainerParams { | |
const useDndRenderContainer = ({onDragEnd, renderControls}: UseDndRenderContainerParams) => { | ||
const uniqId = useUniqId(); | ||
|
||
const dndRenderContainer: TreeSelectRenderContainer<Item> = ({ | ||
const dndRenderContainer: TreeSelectRenderContainer<TableColumnSetupItem> = ({ | ||
renderItem, | ||
visibleFlattenIds, | ||
items: _items, | ||
itemsById, | ||
containerRef, | ||
id, | ||
className, | ||
}) => { | ||
const visibleFlattenItemList = visibleFlattenIds.map((visibleFlattenId, idx) => | ||
renderItem(visibleFlattenId, idx), | ||
const renderDndActiveItem: DraggableChildrenFn = (provided, snapshot, rubric) => { | ||
const renderContainerProps: RenderContainerProps = { | ||
provided, | ||
snapshot, | ||
}; | ||
|
||
return renderItem( | ||
visibleFlattenIds[rubric.source.index], | ||
rubric.source.index, | ||
renderContainerProps, | ||
); | ||
}; | ||
|
||
const {stickyStartItemIdList, sortableItemIdList, stickyEndItemIdList} = prepareStickyState( | ||
itemsById, | ||
visibleFlattenIds, | ||
); | ||
|
||
const stickyStartItemList = stickyStartItemIdList.map((visibleFlattenId, idx) => { | ||
return renderItem(visibleFlattenId, idx, RENDER_DRAG_DISABLED_CONTAINER_PROPS); | ||
}); | ||
|
||
const sortableItemList = sortableItemIdList.map((visibleFlattenId, idx) => { | ||
return renderItem(visibleFlattenId, idx + stickyStartItemIdList.length); | ||
}); | ||
|
||
const stickyEndItemList = stickyEndItemIdList.map((visibleFlattenId, idx) => { | ||
return renderItem( | ||
visibleFlattenId, | ||
stickyStartItemList.length + sortableItemList.length + idx, | ||
RENDER_DRAG_DISABLED_CONTAINER_PROPS, | ||
); | ||
}); | ||
|
||
return ( | ||
<React.Fragment> | ||
<ListContainerView ref={containerRef} id={id} className={className}> | ||
{stickyStartItemList} | ||
<DragDropContext onDragEnd={onDragEnd}> | ||
<Droppable | ||
droppableId={uniqId} | ||
renderClone={(provided, snapshot, rubric) => { | ||
const renderContainerProps: RenderContainerProps = { | ||
provided, | ||
snapshot, | ||
}; | ||
|
||
return renderItem( | ||
visibleFlattenIds[rubric.source.index], | ||
rubric.source.index, | ||
renderContainerProps, | ||
); | ||
}} | ||
> | ||
<Droppable droppableId={uniqId} renderClone={renderDndActiveItem}> | ||
{(droppableProvided) => { | ||
return ( | ||
<div | ||
{...droppableProvided.droppableProps} | ||
ref={droppableProvided.innerRef} | ||
> | ||
{visibleFlattenItemList} | ||
{sortableItemList} | ||
{droppableProvided.placeholder} | ||
</div> | ||
); | ||
}} | ||
</Droppable> | ||
</DragDropContext> | ||
{stickyEndItemList} | ||
</ListContainerView> | ||
<div className={controlsCn}>{renderControls()}</div> | ||
</React.Fragment> | ||
|
@@ -140,23 +176,30 @@ const useDndRenderContainer = ({onDragEnd, renderControls}: UseDndRenderContaine | |
}; | ||
|
||
const useDndRenderItem = (sortable: boolean | undefined) => { | ||
const renderDndItem: TreeSelectRenderItem<Item, RenderContainerProps> = ({ | ||
data, | ||
const renderDndItem: TreeSelectRenderItem<TableColumnSetupItem, RenderContainerProps> = ({ | ||
data: item, | ||
props, | ||
index, | ||
renderContainerProps, | ||
}) => { | ||
const isDragDisabled = sortable === false; | ||
const isDragDisabled = sortable === false || renderContainerProps?.isDragDisabled === true; | ||
const endSlot = isDragDisabled ? undefined : <Icon data={Grip} size={16} />; | ||
const hasSelectionIcon = !item.isRequired; | ||
const startSlot = item.isRequired ? <Icon data={Lock} /> : undefined; | ||
const selected = item.isRequired ? false : props.selected; | ||
|
||
const endSlot = | ||
data.endSlot ?? (isDragDisabled ? undefined : <Icon data={Grip} size={16} />); | ||
|
||
const commonProps = { | ||
const commonProps: ListItemViewProps = { | ||
...props, | ||
...data, | ||
selected, | ||
startSlot, | ||
hasSelectionIcon, | ||
endSlot, | ||
}; | ||
|
||
if (isDragDisabled) { | ||
return <ListItemView {...commonProps} key={commonProps.id} />; | ||
} | ||
|
||
const renderItem = (provided: DraggableProvided, snapshot: DraggableStateSnapshot) => ( | ||
<ListItemView | ||
{...commonProps} | ||
|
@@ -167,15 +210,15 @@ const useDndRenderItem = (sortable: boolean | undefined) => { | |
/> | ||
); | ||
|
||
if (renderContainerProps) { | ||
if (renderContainerProps?.provided && renderContainerProps.snapshot) { | ||
return renderItem(renderContainerProps.provided, renderContainerProps.snapshot); | ||
} | ||
|
||
return ( | ||
<Draggable | ||
draggableId={data.id} | ||
draggableId={props.id} | ||
index={index} | ||
key={`item-key-${data.id}`} | ||
key={`item-key-${props.id}`} | ||
isDragDisabled={isDragDisabled} | ||
> | ||
{renderItem} | ||
|
@@ -186,11 +229,17 @@ const useDndRenderItem = (sortable: boolean | undefined) => { | |
return renderDndItem; | ||
}; | ||
|
||
type Item = TableColumnSetupItem & | ||
ListItemViewProps & { | ||
id: string; | ||
isDragDisabled?: boolean; | ||
export type TableColumnSetupItem = TableSetting & { | ||
title: React.ReactNode; | ||
isRequired?: boolean; | ||
sticky?: TableColumnConfig<unknown>['sticky']; | ||
}; | ||
|
||
const mapItemDataToProps = (item: TableColumnSetupItem): ListItemCommonProps => { | ||
return { | ||
title: item.title, | ||
}; | ||
}; | ||
|
||
export type RenderControls = (params: { | ||
DefaultApplyButton: React.ComponentType; | ||
|
@@ -214,6 +263,8 @@ export interface TableColumnSetupProps { | |
* @deprecated | ||
*/ | ||
renderControls?: RenderControls; | ||
|
||
className?: string; | ||
} | ||
|
||
export const TableColumnSetup = (props: TableColumnSetupProps) => { | ||
|
@@ -225,6 +276,7 @@ export const TableColumnSetup = (props: TableColumnSetupProps) => { | |
onUpdate: propsOnUpdate, | ||
sortable, | ||
renderControls, | ||
className, | ||
} = props; | ||
|
||
const [open, setOpen] = React.useState(false); | ||
|
@@ -295,20 +347,17 @@ export const TableColumnSetup = (props: TableColumnSetupProps) => { | |
}); | ||
}; | ||
|
||
const [value, dndItems] = React.useMemo( | ||
() => [prepareValue(items), prepareDndItems(items)] as const, | ||
[items], | ||
); | ||
const value = React.useMemo(() => prepareValue(items), [items]); | ||
|
||
return ( | ||
<TreeSelect | ||
className={tableColumnSetupCn} | ||
mapItemDataToProps={identity} | ||
className={b(null, className)} | ||
mapItemDataToProps={mapItemDataToProps} | ||
GermanVor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
multiple | ||
size="l" | ||
open={open} | ||
value={value} | ||
items={dndItems} | ||
items={items} | ||
onUpdate={onUpdate} | ||
popupWidth={popupWidth} | ||
onOpenChange={onOpenChange} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@use '../variables'; | ||
|
||
$block: '.#{variables.$ns}table-column-setup'; | ||
|
||
#{$block} { | ||
&__status { | ||
margin-inline-start: 5px; | ||
color: var(--g-color-text-secondary); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have no idea why it is broken now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is because you test component
const TableWithSettings = withTableSettings<SomeItem>(Table);
. And keysets are taken from https://github.com/gravity-ui/uikit/tree/839d503f378e23eef87887866d20973bcd174fb4/src/components/Table/hoc/withTableSettings/i18nThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will it be fixed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In separate PR, I do not know what happened with this place