geido commented on code in PR #33517: URL: https://github.com/apache/superset/pull/33517#discussion_r2145535617
########## superset-frontend/plugins/plugin-chart-ag-grid-table/src/AgGridTable/components/CustomHeader.tsx: ########## @@ -0,0 +1,346 @@ +/* eslint-disable jsx-a11y/no-static-element-interactions */ +/* eslint-disable theme-colors/no-literal-colors */ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { useRef, useState } from 'react'; +import { styled } from '@superset-ui/core'; +import { + ArrowDownOutlined, + ArrowUpOutlined, + PlusOutlined, +} from '@ant-design/icons'; +import { IHeaderParams, Column, ColDef } from 'ag-grid-community'; +import CustomPopover from './CustomPopover'; +import { CustomColDef } from '..'; + +const ThreeDots = ({ size = 14, color = 'black' }) => ( + <svg + width={size} + height={size} + viewBox="0 0 16 16" + fill={color} + xmlns="http://www.w3.org/2000/svg" + > + <circle cx="8" cy="3" r="1.2" /> + <circle cx="8" cy="8" r="1.2" /> + <circle cx="8" cy="13" r="1.2" /> + </svg> +); +const FilterIcon = ( + <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor"> + <rect x="3" y="6" width="18" height="2" rx="1" /> + <rect x="6" y="11" width="12" height="2" rx="1" /> + <rect x="9" y="16" width="6" height="2" rx="1" /> + </svg> +); + +// Styled Components +const Container = styled.div` + display: flex; + width: 100%; + + .three-dots-menu { + align-self: center; + margin-left: 5px; + cursor: pointer; + padding: 2px; + border-radius: 4px; + } +`; + +const HeaderContainer = styled.div` + width: 100%; + display: flex; + align-items: center; + cursor: pointer; + padding: 0 8px; + overflow: hidden; +`; + +const HeaderLabel = styled.span` + font-weight: 500; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + display: block; + max-width: 100%; +`; + +const SortIconWrapper = styled.div` + display: flex; + align-items: center; + margin-left: 0.5rem; +`; + +const FilterIconWrapper = styled.div` + align-self: flex-end; + margin-left: auto; + cursor: pointer; +`; + +const MenuContainer = styled.div` + min-width: 180px; + padding: 4px 0; + + .menu-item { + padding: 8px 16px; + cursor: pointer; + display: flex; + align-items: center; + gap: 8px; + + &:hover { + background-color: rgba(32, 167, 201, 0.2); + } + } + + .menu-divider { + height: 1px; + background-color: #e8e8e8; + margin: 4px 0; + } +`; + +const ToggleButton = styled.div` + display: flex; + align-items: center; + cursor: pointer; + padding: 2px; + margin-left: 4px; + transition: transform 0.2s; + + &:hover { + background: rgba(0, 0, 0, 0.04); + border-radius: 4px; + } +`; + +// Export the interfaces +export interface SortState { + colId: string; + sort: 'asc' | 'desc' | null; +} + +export interface CustomContext { + initialSortState: SortState[]; + onColumnHeaderClicked: (args: { column: SortState }) => void; +} + +export interface CustomHeaderParams extends IHeaderParams { + context: CustomContext; + column: Column; +} + +interface UserProvidedColDef extends ColDef { + isMain?: boolean; + timeComparisonKey?: string; +} + +const getSortIcon = ( + sortState: SortState[], + colId: string | null, +): React.ReactNode => { + if (!sortState?.length || !colId) return null; + const currentSort = sortState[0]; + if (currentSort.colId === colId) { + if (currentSort.sort === 'asc') return <ArrowUpOutlined />; + if (currentSort.sort === 'desc') return <ArrowDownOutlined />; + } + return null; +}; + +const CustomHeader: React.FC<CustomHeaderParams> = ({ + displayName, + enableSorting, + setSort, + context, + column, + api, +}) => { + const { initialSortState, onColumnHeaderClicked } = context; + const colId = column?.getColId(); + const isPercentMetric = (column?.getColDef() as CustomColDef)?.customMeta + ?.isPercentMetric; + const [isPopoverVisible, setPopoverVisible] = useState(false); + const filterContainerRef = useRef<HTMLDivElement>(null); + const [isMenuVisible, setIsMenuVisible] = useState(false); + const currentSort = initialSortState?.[0]; + const [areComparisonColumnsVisible, setAreComparisonColumnsVisible] = + useState(true); + + const userColumn = column.getUserProvidedColDef() as UserProvidedColDef; + const isMain = userColumn?.isMain; + const timeComparisonKey = userColumn?.timeComparisonKey || ''; + const sortKey = isMain ? colId.replace('Main', '').trim() : colId; + const isTimeComparison = !isMain && timeComparisonKey; + + const ClearSort = () => { + onColumnHeaderClicked({ column: { colId: sortKey, sort: null } }); + setSort(null, false); + }; + + const handleSortAsc = () => { + onColumnHeaderClicked({ column: { colId: sortKey, sort: 'asc' } }); + setSort('asc', false); + }; + + const handleSortDesc = () => { + onColumnHeaderClicked({ column: { colId: sortKey, sort: 'desc' } }); + setSort('desc', false); + }; + + const handleSort = () => { + if (isTimeComparison) return; + + if (!enableSorting) return; + + const current = initialSortState?.[0]; + + if (!current || current.colId !== colId) { + handleSortAsc(); + } else if (current.sort === 'asc') { + handleSortDesc(); + } else { + ClearSort(); + } + }; + + const handleFilterClick = async (event: React.MouseEvent) => { + event.stopPropagation(); + setPopoverVisible(!isPopoverVisible); + + const filterInstance = await api.getColumnFilterInstance<any>(column); + const filterEl = filterInstance?.eGui; + + if (!filterEl || !filterContainerRef.current) return; + + filterContainerRef.current.innerHTML = ''; + filterContainerRef.current.appendChild(filterEl); + }; + + const handleMenuClick = (event: React.MouseEvent) => { + event.stopPropagation(); + setIsMenuVisible(!isMenuVisible); + }; + + const handleToggleComparison = (event: React.MouseEvent) => { + event.stopPropagation(); + + const allColumns = api.getColumnDefs(); + const timeComparisonColumns = allColumns?.filter( + col => + (col as UserProvidedColDef).timeComparisonKey === timeComparisonKey && + !(col as UserProvidedColDef).isMain, + ); + const timeComparsionColIds = timeComparisonColumns?.map( + item => (item as UserProvidedColDef).field || '', + ) as string[]; + api.setColumnsVisible(timeComparsionColIds, !areComparisonColumnsVisible); + + api.sizeColumnsToFit(); + + setAreComparisonColumnsVisible(!areComparisonColumnsVisible); + }; + + const shouldShowAsc = + !currentSort || + (currentSort?.colId === colId && currentSort?.sort === 'desc') || + currentSort?.colId !== colId; + const shouldShowDesc = + !currentSort || + (currentSort?.colId === colId && currentSort?.sort === 'asc') || + currentSort?.colId !== colId; + + const menuContent = ( + <MenuContainer> + {shouldShowAsc && !isTimeComparison && ( + <div onClick={handleSortAsc} className="menu-item"> + <ArrowUpOutlined /> Sort Ascending + </div> + )} + {shouldShowDesc && !isTimeComparison && ( + <div onClick={handleSortDesc} className="menu-item"> + <ArrowDownOutlined /> Sort Descending + </div> + )} + {currentSort && currentSort?.colId === colId && ( + <div onClick={ClearSort} className="menu-item"> + <span style={{ fontSize: 16 }}>↻</span> Clear Sort + </div> + )} + </MenuContainer> + ); + + return ( + <Container> + <HeaderContainer onClick={handleSort} className="custom-header"> + <HeaderLabel>{displayName}</HeaderLabel> + <SortIconWrapper> + {getSortIcon(initialSortState, colId)} + </SortIconWrapper> + {isMain && timeComparisonKey && ( + <ToggleButton + onClick={handleToggleComparison} + title={ + areComparisonColumnsVisible + ? 'Hide comparison columns' Review Comment: Should these be localized / translatable? ########## superset-frontend/plugins/plugin-chart-ag-grid-table/src/AgGridTable/components/CustomHeader.tsx: ########## @@ -0,0 +1,346 @@ +/* eslint-disable jsx-a11y/no-static-element-interactions */ +/* eslint-disable theme-colors/no-literal-colors */ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { useRef, useState } from 'react'; +import { styled } from '@superset-ui/core'; +import { + ArrowDownOutlined, + ArrowUpOutlined, + PlusOutlined, +} from '@ant-design/icons'; +import { IHeaderParams, Column, ColDef } from 'ag-grid-community'; +import CustomPopover from './CustomPopover'; +import { CustomColDef } from '..'; + +const ThreeDots = ({ size = 14, color = 'black' }) => ( + <svg + width={size} + height={size} + viewBox="0 0 16 16" + fill={color} + xmlns="http://www.w3.org/2000/svg" + > + <circle cx="8" cy="3" r="1.2" /> + <circle cx="8" cy="8" r="1.2" /> + <circle cx="8" cy="13" r="1.2" /> + </svg> +); +const FilterIcon = ( + <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor"> + <rect x="3" y="6" width="18" height="2" rx="1" /> + <rect x="6" y="11" width="12" height="2" rx="1" /> + <rect x="9" y="16" width="6" height="2" rx="1" /> + </svg> +); + +// Styled Components +const Container = styled.div` + display: flex; + width: 100%; + + .three-dots-menu { + align-self: center; + margin-left: 5px; + cursor: pointer; + padding: 2px; + border-radius: 4px; + } +`; + +const HeaderContainer = styled.div` + width: 100%; + display: flex; + align-items: center; + cursor: pointer; + padding: 0 8px; + overflow: hidden; +`; + +const HeaderLabel = styled.span` + font-weight: 500; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + display: block; + max-width: 100%; +`; + +const SortIconWrapper = styled.div` + display: flex; + align-items: center; + margin-left: 0.5rem; +`; + +const FilterIconWrapper = styled.div` + align-self: flex-end; + margin-left: auto; + cursor: pointer; +`; + +const MenuContainer = styled.div` + min-width: 180px; + padding: 4px 0; + + .menu-item { + padding: 8px 16px; + cursor: pointer; + display: flex; + align-items: center; + gap: 8px; + + &:hover { + background-color: rgba(32, 167, 201, 0.2); + } + } + + .menu-divider { + height: 1px; + background-color: #e8e8e8; + margin: 4px 0; + } +`; + +const ToggleButton = styled.div` + display: flex; + align-items: center; + cursor: pointer; + padding: 2px; + margin-left: 4px; + transition: transform 0.2s; + + &:hover { + background: rgba(0, 0, 0, 0.04); + border-radius: 4px; + } +`; + +// Export the interfaces +export interface SortState { + colId: string; + sort: 'asc' | 'desc' | null; +} + +export interface CustomContext { + initialSortState: SortState[]; + onColumnHeaderClicked: (args: { column: SortState }) => void; +} + +export interface CustomHeaderParams extends IHeaderParams { + context: CustomContext; + column: Column; +} + +interface UserProvidedColDef extends ColDef { + isMain?: boolean; + timeComparisonKey?: string; +} + +const getSortIcon = ( + sortState: SortState[], + colId: string | null, +): React.ReactNode => { + if (!sortState?.length || !colId) return null; + const currentSort = sortState[0]; + if (currentSort.colId === colId) { + if (currentSort.sort === 'asc') return <ArrowUpOutlined />; + if (currentSort.sort === 'desc') return <ArrowDownOutlined />; + } + return null; +}; + +const CustomHeader: React.FC<CustomHeaderParams> = ({ + displayName, + enableSorting, + setSort, + context, + column, + api, +}) => { + const { initialSortState, onColumnHeaderClicked } = context; + const colId = column?.getColId(); + const isPercentMetric = (column?.getColDef() as CustomColDef)?.customMeta + ?.isPercentMetric; + const [isPopoverVisible, setPopoverVisible] = useState(false); + const filterContainerRef = useRef<HTMLDivElement>(null); + const [isMenuVisible, setIsMenuVisible] = useState(false); + const currentSort = initialSortState?.[0]; + const [areComparisonColumnsVisible, setAreComparisonColumnsVisible] = + useState(true); + + const userColumn = column.getUserProvidedColDef() as UserProvidedColDef; + const isMain = userColumn?.isMain; + const timeComparisonKey = userColumn?.timeComparisonKey || ''; + const sortKey = isMain ? colId.replace('Main', '').trim() : colId; + const isTimeComparison = !isMain && timeComparisonKey; + + const ClearSort = () => { + onColumnHeaderClicked({ column: { colId: sortKey, sort: null } }); + setSort(null, false); + }; + + const handleSortAsc = () => { + onColumnHeaderClicked({ column: { colId: sortKey, sort: 'asc' } }); + setSort('asc', false); + }; + + const handleSortDesc = () => { + onColumnHeaderClicked({ column: { colId: sortKey, sort: 'desc' } }); + setSort('desc', false); + }; + + const handleSort = () => { + if (isTimeComparison) return; + + if (!enableSorting) return; + + const current = initialSortState?.[0]; + + if (!current || current.colId !== colId) { + handleSortAsc(); + } else if (current.sort === 'asc') { + handleSortDesc(); + } else { + ClearSort(); Review Comment: ```suggestion clearSort(); ``` ########## superset-frontend/plugins/plugin-chart-ag-grid-table/src/AgGridTable/components/Pagination.tsx: ########## @@ -0,0 +1,204 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/* eslint-disable theme-colors/no-literal-colors */ +import { styled } from '@superset-ui/core'; +import { + VerticalLeftOutlined, + VerticalRightOutlined, + LeftOutlined, + RightOutlined, +} from '@ant-design/icons'; + +const PaginationContainer = styled.div` + border: 1px solid #dcdddd; + border-bottom-left-radius: 10px; + border-bottom-right-radius: 10px; + display: flex; + align-items: center; + justify-content: flex-end; + padding: 8px 16px; + border-top: 1px solid ${({ theme }) => theme.colors.grayscale.light2}; + font-size: 14px; + color: ${({ theme }) => theme.colors.grayscale.dark1}; + transform: translateY(-5px); + background: white; +`; + +const SelectWrapper = styled.div` + position: relative; + display: inline-block; + min-width: 70px; +`; + +const StyledSelect = styled.select<{ numberLength: number }>` + width: auto; + margin: 0 8px; + padding: 2px 24px 2px 8px; + border: 1px solid ${({ theme }) => theme.colors.grayscale.light2}; + border-radius: 4px; + background: white; + appearance: none; + cursor: pointer; + + /* Custom arrow styling */ + background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23000000'%3e%3cpath d='M7 10l5 5 5-5z'/%3e%3c/svg%3e"); + background-repeat: no-repeat; + background-position: right + ${props => (props.numberLength <= 2 ? '8px' : '4px')} center; + background-size: 24px; + + &:hover { + border-color: ${({ theme }) => theme.colors.grayscale.dark1}; + } +`; + +const PageInfo = styled.span` + margin: 0 24px; + span { + font-weight: 500; + } +`; + +const PageCount = styled.span` + span { + font-weight: 500; + } +`; +const ButtonGroup = styled.div` + display: flex; + gap: 12px; +`; + +interface PageButtonProps { + disabled?: boolean; +} + +const PageButton = styled.div<PageButtonProps>` + cursor: ${({ disabled }) => (disabled ? 'not-allowed' : 'pointer')}; + display: flex; + align-items: center; + justify-content: center; + + svg { + height: 12px; + width: 12px; + fill: ${({ theme, disabled }) => + disabled ? theme.colors.grayscale.light1 : theme.colors.grayscale.dark2}; + } +`; + +interface PaginationProps { + currentPage: number; + pageSize: number; + totalRows: number; + pageSizeOptions: number[]; + onServerPaginationChange: (pageNumber: number, pageSize: number) => void; + onServerPageSizeChange: (pageSize: number) => void; +} + +const Pagination: React.FC<PaginationProps> = ({ + currentPage = 0, + pageSize = 10, + totalRows = 0, + pageSizeOptions = [10, 20, 50, 100, 200], + onServerPaginationChange = () => {}, + onServerPageSizeChange = () => {}, +}) => { + const totalPages = Math.ceil(totalRows / pageSize); + const startRow = currentPage * pageSize + 1; + const endRow = Math.min((currentPage + 1) * pageSize, totalRows); + + const handleNextPage = (disabled: boolean) => () => { + if (disabled) return; + onServerPaginationChange(currentPage + 1, pageSize); + }; + + const handlePrevPage = (disabled: boolean) => () => { + if (disabled) return; + onServerPaginationChange(currentPage - 1, pageSize); + }; + + const handleNavigateToFirstPage = (disabled: boolean) => () => { + if (disabled) return; + onServerPaginationChange(0, pageSize); + }; + + const handleNavigateToLastPage = (disabled: boolean) => () => { + if (disabled) return; + onServerPaginationChange(totalPages - 1, pageSize); + }; + + return ( + <PaginationContainer> + <span>Page Size:</span> Review Comment: Localize? ########## superset-frontend/plugins/plugin-chart-ag-grid-table/src/AgGridTable/components/Pagination.tsx: ########## @@ -0,0 +1,204 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/* eslint-disable theme-colors/no-literal-colors */ +import { styled } from '@superset-ui/core'; +import { + VerticalLeftOutlined, + VerticalRightOutlined, + LeftOutlined, + RightOutlined, +} from '@ant-design/icons'; + +const PaginationContainer = styled.div` + border: 1px solid #dcdddd; + border-bottom-left-radius: 10px; + border-bottom-right-radius: 10px; + display: flex; + align-items: center; + justify-content: flex-end; + padding: 8px 16px; + border-top: 1px solid ${({ theme }) => theme.colors.grayscale.light2}; + font-size: 14px; + color: ${({ theme }) => theme.colors.grayscale.dark1}; + transform: translateY(-5px); + background: white; +`; + +const SelectWrapper = styled.div` + position: relative; + display: inline-block; + min-width: 70px; +`; + +const StyledSelect = styled.select<{ numberLength: number }>` + width: auto; + margin: 0 8px; + padding: 2px 24px 2px 8px; + border: 1px solid ${({ theme }) => theme.colors.grayscale.light2}; + border-radius: 4px; + background: white; + appearance: none; + cursor: pointer; + + /* Custom arrow styling */ + background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23000000'%3e%3cpath d='M7 10l5 5 5-5z'/%3e%3c/svg%3e"); + background-repeat: no-repeat; + background-position: right + ${props => (props.numberLength <= 2 ? '8px' : '4px')} center; + background-size: 24px; + + &:hover { + border-color: ${({ theme }) => theme.colors.grayscale.dark1}; + } +`; + +const PageInfo = styled.span` + margin: 0 24px; + span { + font-weight: 500; + } +`; + +const PageCount = styled.span` + span { + font-weight: 500; + } +`; +const ButtonGroup = styled.div` + display: flex; + gap: 12px; +`; + +interface PageButtonProps { + disabled?: boolean; +} + +const PageButton = styled.div<PageButtonProps>` + cursor: ${({ disabled }) => (disabled ? 'not-allowed' : 'pointer')}; + display: flex; + align-items: center; + justify-content: center; + + svg { + height: 12px; + width: 12px; + fill: ${({ theme, disabled }) => + disabled ? theme.colors.grayscale.light1 : theme.colors.grayscale.dark2}; + } +`; + +interface PaginationProps { + currentPage: number; + pageSize: number; + totalRows: number; + pageSizeOptions: number[]; + onServerPaginationChange: (pageNumber: number, pageSize: number) => void; + onServerPageSizeChange: (pageSize: number) => void; +} + +const Pagination: React.FC<PaginationProps> = ({ + currentPage = 0, + pageSize = 10, + totalRows = 0, + pageSizeOptions = [10, 20, 50, 100, 200], + onServerPaginationChange = () => {}, + onServerPageSizeChange = () => {}, +}) => { + const totalPages = Math.ceil(totalRows / pageSize); + const startRow = currentPage * pageSize + 1; + const endRow = Math.min((currentPage + 1) * pageSize, totalRows); + + const handleNextPage = (disabled: boolean) => () => { + if (disabled) return; + onServerPaginationChange(currentPage + 1, pageSize); + }; + + const handlePrevPage = (disabled: boolean) => () => { + if (disabled) return; + onServerPaginationChange(currentPage - 1, pageSize); + }; + + const handleNavigateToFirstPage = (disabled: boolean) => () => { + if (disabled) return; + onServerPaginationChange(0, pageSize); + }; + + const handleNavigateToLastPage = (disabled: boolean) => () => { + if (disabled) return; + onServerPaginationChange(totalPages - 1, pageSize); + }; + + return ( + <PaginationContainer> + <span>Page Size:</span> + <SelectWrapper> + <StyledSelect + numberLength={pageSize.toString().length} + onChange={e => { + onServerPageSizeChange(Number(e.target.value)); + }} + value={pageSize} + > + {pageSizeOptions.map(size => ( + <option key={size} value={size}> + {size} + </option> + ))} + </StyledSelect> + </SelectWrapper> + + <PageInfo> + <span>{startRow}</span> to <span>{endRow}</span> of{' '} Review Comment: Should we localize these and other strings? ########## superset-frontend/plugins/plugin-chart-ag-grid-table/src/AgGridTable/components/CustomHeader.tsx: ########## @@ -0,0 +1,346 @@ +/* eslint-disable jsx-a11y/no-static-element-interactions */ +/* eslint-disable theme-colors/no-literal-colors */ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { useRef, useState } from 'react'; +import { styled } from '@superset-ui/core'; +import { + ArrowDownOutlined, + ArrowUpOutlined, + PlusOutlined, +} from '@ant-design/icons'; +import { IHeaderParams, Column, ColDef } from 'ag-grid-community'; +import CustomPopover from './CustomPopover'; +import { CustomColDef } from '..'; + +const ThreeDots = ({ size = 14, color = 'black' }) => ( + <svg + width={size} + height={size} + viewBox="0 0 16 16" + fill={color} + xmlns="http://www.w3.org/2000/svg" + > + <circle cx="8" cy="3" r="1.2" /> + <circle cx="8" cy="8" r="1.2" /> + <circle cx="8" cy="13" r="1.2" /> + </svg> +); +const FilterIcon = ( + <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor"> + <rect x="3" y="6" width="18" height="2" rx="1" /> + <rect x="6" y="11" width="12" height="2" rx="1" /> + <rect x="9" y="16" width="6" height="2" rx="1" /> + </svg> +); + +// Styled Components +const Container = styled.div` + display: flex; + width: 100%; + + .three-dots-menu { + align-self: center; + margin-left: 5px; + cursor: pointer; + padding: 2px; + border-radius: 4px; + } +`; + +const HeaderContainer = styled.div` + width: 100%; + display: flex; + align-items: center; + cursor: pointer; + padding: 0 8px; + overflow: hidden; +`; + +const HeaderLabel = styled.span` + font-weight: 500; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + display: block; + max-width: 100%; +`; + +const SortIconWrapper = styled.div` + display: flex; + align-items: center; + margin-left: 0.5rem; +`; + +const FilterIconWrapper = styled.div` + align-self: flex-end; + margin-left: auto; + cursor: pointer; +`; + +const MenuContainer = styled.div` + min-width: 180px; + padding: 4px 0; + + .menu-item { + padding: 8px 16px; + cursor: pointer; + display: flex; + align-items: center; + gap: 8px; + + &:hover { + background-color: rgba(32, 167, 201, 0.2); + } + } + + .menu-divider { + height: 1px; + background-color: #e8e8e8; + margin: 4px 0; + } +`; + +const ToggleButton = styled.div` + display: flex; + align-items: center; + cursor: pointer; + padding: 2px; + margin-left: 4px; + transition: transform 0.2s; + + &:hover { + background: rgba(0, 0, 0, 0.04); + border-radius: 4px; + } +`; + +// Export the interfaces +export interface SortState { + colId: string; + sort: 'asc' | 'desc' | null; +} + +export interface CustomContext { + initialSortState: SortState[]; + onColumnHeaderClicked: (args: { column: SortState }) => void; +} + +export interface CustomHeaderParams extends IHeaderParams { + context: CustomContext; + column: Column; +} + +interface UserProvidedColDef extends ColDef { + isMain?: boolean; + timeComparisonKey?: string; +} + +const getSortIcon = ( + sortState: SortState[], + colId: string | null, +): React.ReactNode => { + if (!sortState?.length || !colId) return null; + const currentSort = sortState[0]; + if (currentSort.colId === colId) { + if (currentSort.sort === 'asc') return <ArrowUpOutlined />; + if (currentSort.sort === 'desc') return <ArrowDownOutlined />; + } + return null; +}; + +const CustomHeader: React.FC<CustomHeaderParams> = ({ + displayName, + enableSorting, + setSort, + context, + column, + api, +}) => { + const { initialSortState, onColumnHeaderClicked } = context; + const colId = column?.getColId(); + const isPercentMetric = (column?.getColDef() as CustomColDef)?.customMeta + ?.isPercentMetric; + const [isPopoverVisible, setPopoverVisible] = useState(false); + const filterContainerRef = useRef<HTMLDivElement>(null); + const [isMenuVisible, setIsMenuVisible] = useState(false); + const currentSort = initialSortState?.[0]; + const [areComparisonColumnsVisible, setAreComparisonColumnsVisible] = + useState(true); + + const userColumn = column.getUserProvidedColDef() as UserProvidedColDef; + const isMain = userColumn?.isMain; + const timeComparisonKey = userColumn?.timeComparisonKey || ''; + const sortKey = isMain ? colId.replace('Main', '').trim() : colId; + const isTimeComparison = !isMain && timeComparisonKey; + + const ClearSort = () => { Review Comment: ```suggestion const clearSort = () => { ``` ########## superset-frontend/plugins/plugin-chart-ag-grid-table/src/AgGridTable/components/CustomHeader.tsx: ########## @@ -0,0 +1,346 @@ +/* eslint-disable jsx-a11y/no-static-element-interactions */ +/* eslint-disable theme-colors/no-literal-colors */ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { useRef, useState } from 'react'; +import { styled } from '@superset-ui/core'; +import { + ArrowDownOutlined, + ArrowUpOutlined, + PlusOutlined, +} from '@ant-design/icons'; +import { IHeaderParams, Column, ColDef } from 'ag-grid-community'; +import CustomPopover from './CustomPopover'; +import { CustomColDef } from '..'; + +const ThreeDots = ({ size = 14, color = 'black' }) => ( + <svg + width={size} + height={size} + viewBox="0 0 16 16" + fill={color} + xmlns="http://www.w3.org/2000/svg" + > + <circle cx="8" cy="3" r="1.2" /> + <circle cx="8" cy="8" r="1.2" /> + <circle cx="8" cy="13" r="1.2" /> + </svg> +); +const FilterIcon = ( + <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor"> + <rect x="3" y="6" width="18" height="2" rx="1" /> + <rect x="6" y="11" width="12" height="2" rx="1" /> + <rect x="9" y="16" width="6" height="2" rx="1" /> + </svg> +); + +// Styled Components +const Container = styled.div` + display: flex; + width: 100%; + + .three-dots-menu { + align-self: center; + margin-left: 5px; + cursor: pointer; + padding: 2px; + border-radius: 4px; + } +`; + +const HeaderContainer = styled.div` + width: 100%; + display: flex; + align-items: center; + cursor: pointer; + padding: 0 8px; + overflow: hidden; +`; + +const HeaderLabel = styled.span` + font-weight: 500; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + display: block; + max-width: 100%; +`; + +const SortIconWrapper = styled.div` + display: flex; + align-items: center; + margin-left: 0.5rem; +`; + +const FilterIconWrapper = styled.div` + align-self: flex-end; + margin-left: auto; + cursor: pointer; +`; + +const MenuContainer = styled.div` + min-width: 180px; + padding: 4px 0; + + .menu-item { + padding: 8px 16px; + cursor: pointer; + display: flex; + align-items: center; + gap: 8px; + + &:hover { + background-color: rgba(32, 167, 201, 0.2); + } + } + + .menu-divider { + height: 1px; + background-color: #e8e8e8; + margin: 4px 0; + } +`; + +const ToggleButton = styled.div` + display: flex; + align-items: center; + cursor: pointer; + padding: 2px; + margin-left: 4px; + transition: transform 0.2s; + + &:hover { + background: rgba(0, 0, 0, 0.04); + border-radius: 4px; + } +`; + +// Export the interfaces +export interface SortState { + colId: string; + sort: 'asc' | 'desc' | null; +} + +export interface CustomContext { + initialSortState: SortState[]; + onColumnHeaderClicked: (args: { column: SortState }) => void; +} + +export interface CustomHeaderParams extends IHeaderParams { + context: CustomContext; + column: Column; +} + +interface UserProvidedColDef extends ColDef { + isMain?: boolean; + timeComparisonKey?: string; +} + +const getSortIcon = ( + sortState: SortState[], + colId: string | null, +): React.ReactNode => { + if (!sortState?.length || !colId) return null; + const currentSort = sortState[0]; + if (currentSort.colId === colId) { + if (currentSort.sort === 'asc') return <ArrowUpOutlined />; + if (currentSort.sort === 'desc') return <ArrowDownOutlined />; + } + return null; +}; + +const CustomHeader: React.FC<CustomHeaderParams> = ({ + displayName, + enableSorting, + setSort, + context, + column, + api, +}) => { + const { initialSortState, onColumnHeaderClicked } = context; + const colId = column?.getColId(); + const isPercentMetric = (column?.getColDef() as CustomColDef)?.customMeta + ?.isPercentMetric; + const [isPopoverVisible, setPopoverVisible] = useState(false); + const filterContainerRef = useRef<HTMLDivElement>(null); + const [isMenuVisible, setIsMenuVisible] = useState(false); + const currentSort = initialSortState?.[0]; + const [areComparisonColumnsVisible, setAreComparisonColumnsVisible] = + useState(true); + + const userColumn = column.getUserProvidedColDef() as UserProvidedColDef; + const isMain = userColumn?.isMain; + const timeComparisonKey = userColumn?.timeComparisonKey || ''; + const sortKey = isMain ? colId.replace('Main', '').trim() : colId; + const isTimeComparison = !isMain && timeComparisonKey; + + const ClearSort = () => { + onColumnHeaderClicked({ column: { colId: sortKey, sort: null } }); + setSort(null, false); + }; + + const handleSortAsc = () => { + onColumnHeaderClicked({ column: { colId: sortKey, sort: 'asc' } }); + setSort('asc', false); + }; + + const handleSortDesc = () => { + onColumnHeaderClicked({ column: { colId: sortKey, sort: 'desc' } }); + setSort('desc', false); + }; + + const handleSort = () => { + if (isTimeComparison) return; + + if (!enableSorting) return; + + const current = initialSortState?.[0]; + + if (!current || current.colId !== colId) { + handleSortAsc(); + } else if (current.sort === 'asc') { + handleSortDesc(); + } else { + ClearSort(); + } + }; + + const handleFilterClick = async (event: React.MouseEvent) => { + event.stopPropagation(); + setPopoverVisible(!isPopoverVisible); + + const filterInstance = await api.getColumnFilterInstance<any>(column); + const filterEl = filterInstance?.eGui; + + if (!filterEl || !filterContainerRef.current) return; + + filterContainerRef.current.innerHTML = ''; + filterContainerRef.current.appendChild(filterEl); + }; + + const handleMenuClick = (event: React.MouseEvent) => { + event.stopPropagation(); + setIsMenuVisible(!isMenuVisible); + }; + + const handleToggleComparison = (event: React.MouseEvent) => { + event.stopPropagation(); + + const allColumns = api.getColumnDefs(); + const timeComparisonColumns = allColumns?.filter( + col => + (col as UserProvidedColDef).timeComparisonKey === timeComparisonKey && + !(col as UserProvidedColDef).isMain, + ); + const timeComparsionColIds = timeComparisonColumns?.map( + item => (item as UserProvidedColDef).field || '', + ) as string[]; + api.setColumnsVisible(timeComparsionColIds, !areComparisonColumnsVisible); + + api.sizeColumnsToFit(); + + setAreComparisonColumnsVisible(!areComparisonColumnsVisible); + }; + + const shouldShowAsc = + !currentSort || + (currentSort?.colId === colId && currentSort?.sort === 'desc') || + currentSort?.colId !== colId; + const shouldShowDesc = + !currentSort || + (currentSort?.colId === colId && currentSort?.sort === 'asc') || + currentSort?.colId !== colId; + + const menuContent = ( + <MenuContainer> + {shouldShowAsc && !isTimeComparison && ( + <div onClick={handleSortAsc} className="menu-item"> + <ArrowUpOutlined /> Sort Ascending + </div> + )} + {shouldShowDesc && !isTimeComparison && ( + <div onClick={handleSortDesc} className="menu-item"> + <ArrowDownOutlined /> Sort Descending + </div> + )} + {currentSort && currentSort?.colId === colId && ( + <div onClick={ClearSort} className="menu-item"> Review Comment: ```suggestion <div onClick={clearSort} className="menu-item"> ``` ########## superset-frontend/plugins/plugin-chart-ag-grid-table/src/AgGridTable/components/CustomPopover.tsx: ########## @@ -0,0 +1,112 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/* eslint-disable theme-colors/no-literal-colors */ +import { useEffect, useRef, useState, cloneElement } from 'react'; +import { styled } from '@superset-ui/core'; + +const PopoverWrapper = styled.div` + position: relative; + display: inline-block; +`; + +const PopoverContainer = styled.div` + position: fixed; + background: #f8f8f8; Review Comment: I see we are not using the theme anywhere, is that intended? ########## superset-frontend/plugins/plugin-chart-ag-grid-table/src/AgGridTable/components/Pagination.tsx: ########## @@ -0,0 +1,204 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/* eslint-disable theme-colors/no-literal-colors */ +import { styled } from '@superset-ui/core'; +import { + VerticalLeftOutlined, + VerticalRightOutlined, + LeftOutlined, + RightOutlined, +} from '@ant-design/icons'; + +const PaginationContainer = styled.div` + border: 1px solid #dcdddd; + border-bottom-left-radius: 10px; + border-bottom-right-radius: 10px; + display: flex; + align-items: center; + justify-content: flex-end; + padding: 8px 16px; + border-top: 1px solid ${({ theme }) => theme.colors.grayscale.light2}; Review Comment: Nit: We can promote the theme at the top of the styled declaration ########## superset-frontend/plugins/plugin-chart-ag-grid-table/src/AgGridTable/components/Pagination.tsx: ########## @@ -0,0 +1,204 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/* eslint-disable theme-colors/no-literal-colors */ +import { styled } from '@superset-ui/core'; +import { + VerticalLeftOutlined, + VerticalRightOutlined, + LeftOutlined, + RightOutlined, +} from '@ant-design/icons'; + +const PaginationContainer = styled.div` + border: 1px solid #dcdddd; + border-bottom-left-radius: 10px; + border-bottom-right-radius: 10px; + display: flex; + align-items: center; + justify-content: flex-end; + padding: 8px 16px; + border-top: 1px solid ${({ theme }) => theme.colors.grayscale.light2}; + font-size: 14px; Review Comment: Let's use the theme for font sizes, colors, paddings / margings, etc. ########## superset-frontend/plugins/plugin-chart-ag-grid-table/src/AgGridTable/components/CustomPopover.tsx: ########## @@ -0,0 +1,112 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/* eslint-disable theme-colors/no-literal-colors */ Review Comment: Do we need this? Theming is just around the corner so we should try to use colors from the theme I guess -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org