generated from vtex-apps/admin-example
-
Notifications
You must be signed in to change notification settings - Fork 16
feat(user-widget): fix bugs in search by term; creates infinite scroll #201
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
Merged
renan-vtex
merged 2 commits into
vtex-apps:master
from
tiago-freire:feat/user-widget-organizations-infinite-scroll
Oct 9, 2025
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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,109 @@ | ||
| import React, { Fragment, useEffect, useState } from 'react' | ||
| import { useCssHandles } from 'vtex.css-handles' | ||
| import { ButtonWithIcon, IconCaretDown } from 'vtex.styleguide' | ||
|
|
||
| import { useScroll } from '../../hooks/useScroll' | ||
|
|
||
| type UserWidgetTableProps = Readonly<{ | ||
| list?: Array<{ | ||
| orgId: string | ||
| costId: string | ||
| organizationName: string | ||
| costCenterName: string | ||
| }> | ||
| radioValue: string | ||
| setRadioValue: React.Dispatch<React.SetStateAction<string>> | ||
| }> | ||
|
|
||
| const CSS_HANDLES = [ | ||
| 'userWidgetModalTotal', | ||
| 'userWidgetModalTableContainer', | ||
| 'userWidgetModalTable', | ||
| 'userWidgetModalTableRow', | ||
| 'userWidgetModalTableRowChecked', | ||
| 'userWidgetModalTableRadio', | ||
| 'userWidgetModalTableCell', | ||
| ] as const | ||
|
|
||
| const DEFAULT_OFFSET = 15 | ||
|
|
||
| export function UserWidgetTable(props: UserWidgetTableProps) { | ||
| const handles = useCssHandles(CSS_HANDLES) | ||
| const [offset, setOffset] = useState(DEFAULT_OFFSET) | ||
| const { list = [], radioValue, setRadioValue } = props | ||
| const slicedList = list.slice(0, offset) | ||
| const currentOffset = offset < list.length ? offset : list.length | ||
| const onScrollEnd = () => setOffset((prev: number) => prev + DEFAULT_OFFSET) | ||
| const scrollRef = useScroll<HTMLDivElement>({ onScrollEnd }) | ||
|
|
||
| useEffect(() => { | ||
| scrollRef.current?.scrollTo(0, 0) | ||
| setOffset(DEFAULT_OFFSET) | ||
| }, [list.length]) | ||
|
|
||
| return ( | ||
| <> | ||
| <div | ||
| ref={scrollRef} | ||
| className={`${handles.userWidgetModalTableContainer} mb4`} | ||
| > | ||
| <table className={handles.userWidgetModalTable}> | ||
| <tbody> | ||
| {slicedList.map((organization, index) => { | ||
| const id = [organization.orgId, organization.costId, index].join() | ||
|
|
||
| return ( | ||
| <Fragment key={id}> | ||
| <tr | ||
| className={`${handles.userWidgetModalTableRow} ${ | ||
| id === radioValue | ||
| ? handles.userWidgetModalTableRowChecked | ||
| : '' | ||
| }`} | ||
| onClick={() => setRadioValue(id)} | ||
| > | ||
| <td className={handles.userWidgetModalTableCell}> | ||
| <input | ||
| id={id} | ||
| value={id} | ||
| checked={id === radioValue} | ||
| onChange={(e: any) => setRadioValue(e.target.value)} | ||
| type="radio" | ||
| className={handles.userWidgetModalTableRadio} | ||
| /> | ||
| </td> | ||
| <td className={handles.userWidgetModalTableCell}> | ||
| <label htmlFor={id}> | ||
| {organization.organizationName} | ||
| </label> | ||
| </td> | ||
| <td className={handles.userWidgetModalTableCell}> | ||
| <label htmlFor={id}>{organization.costCenterName}</label> | ||
| </td> | ||
| </tr> | ||
| {index === slicedList.length - 1 && | ||
| list.length > currentOffset && ( | ||
| <tr> | ||
| <td colSpan={3} align="center"> | ||
| <ButtonWithIcon | ||
| onClick={onScrollEnd} | ||
| icon={<IconCaretDown />} | ||
| variation="tertiary" | ||
| /> | ||
| </td> | ||
| </tr> | ||
| )} | ||
| </Fragment> | ||
| ) | ||
| })} | ||
| </tbody> | ||
| </table> | ||
| </div> | ||
| {list.length > DEFAULT_OFFSET && ( | ||
| <div className="flex justify-end mb4"> | ||
| {currentOffset}/{list.length} | ||
| </div> | ||
| )} | ||
| </> | ||
| ) | ||
| } |
This file contains hidden or 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 hidden or 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,37 @@ | ||
| import { useCallback, useEffect, useRef, useState } from 'react' | ||
|
|
||
| export function useDebounceValue<T>(value: T, delay: number) { | ||
| const [debounced, setDebounced] = useState(value) | ||
|
|
||
| useEffect(() => { | ||
| const timer = setTimeout(() => setDebounced(value), delay) | ||
|
|
||
| return () => clearTimeout(timer) | ||
| }, [value, delay]) | ||
|
|
||
| return debounced | ||
| } | ||
|
|
||
| export const useDebounceFunction = <T extends unknown[]>( | ||
| fn: (...args: T) => void, | ||
| delay: number | ||
| ) => { | ||
| const timerRef = useRef<number>() | ||
| const fnRef = useRef(fn) | ||
|
|
||
| useEffect(() => { | ||
| fnRef.current = fn | ||
| }, [fn]) | ||
|
|
||
| const fnDebounced = useCallback( | ||
| (...args: T) => { | ||
| window.clearTimeout(timerRef.current) | ||
| timerRef.current = window.setTimeout(() => fnRef.current(...args), delay) | ||
| }, | ||
| [delay] | ||
| ) | ||
|
|
||
| useEffect(() => () => window.clearTimeout(timerRef.current), []) | ||
|
|
||
| return fnDebounced | ||
| } |
This file contains hidden or 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,43 @@ | ||
| import { useCallback, useEffect, useRef } from 'react' | ||
|
|
||
| import { useDebounceFunction } from './useDebounce' | ||
|
|
||
| type UseScrollArgs = Readonly<{ onScrollEnd: () => void }> | ||
|
|
||
| const SCROLL_DELAY = 150 | ||
| const SCROLL_END_TOLERANCE = 10 | ||
|
|
||
| export function useScroll<T extends HTMLElement>({ | ||
| onScrollEnd, | ||
| }: UseScrollArgs) { | ||
| const ref = useRef<T>(null) | ||
| const onScrollEndDebounced = useDebounceFunction(onScrollEnd, SCROLL_DELAY) | ||
|
|
||
| const handleScroll = useCallback(() => { | ||
| if (!ref.current) return | ||
|
|
||
| const scrollTop: number = Math.ceil(ref.current.scrollTop) | ||
| const scrollHeight: number = Math.round(ref.current.scrollHeight) | ||
| const clientHeight: number = Math.round(ref.current.clientHeight) | ||
|
|
||
| const isScrollEnd = | ||
| scrollTop + SCROLL_END_TOLERANCE >= scrollHeight - clientHeight | ||
|
|
||
| if (isScrollEnd) { | ||
| onScrollEndDebounced() | ||
| } | ||
| }, [onScrollEndDebounced]) | ||
|
|
||
| useEffect(() => { | ||
| if (!ref.current) return | ||
|
|
||
| ref.current.addEventListener('scroll', handleScroll) | ||
| }, [handleScroll]) | ||
|
|
||
| useEffect( | ||
| () => () => ref.current?.removeEventListener('scroll', handleScroll), | ||
| [] | ||
| ) | ||
|
|
||
| return ref | ||
| } |
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.
For performance improvement, we removed these derived states to simply derive the query result (see below in this file).