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

fix: Endless loop when table value dependent on 'select' event #2069 #2082

Merged
merged 8 commits into from
Aug 17, 2023
1 change: 1 addition & 0 deletions ui/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = {
modulePaths: [],
moduleNameMapper: {
"^react-native$": "react-native-web",
'^d3$': '<rootDir>/node_modules/d3/dist/d3.min.js',
"^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
},
moduleFileExtensions: [
Expand Down
21 changes: 21 additions & 0 deletions ui/src/table.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,27 @@ describe('Table.tsx', () => {
expect(emitMock).toHaveBeenCalledTimes(1)
})

it('Fires event - single selection with initial value', async () => {
const
props = { ...tableProps, single: true, events: ['select'], value: 'rowname2' },
{ getAllByRole } = render(<XTable model={{ ...props }} />),
radioButtons = getAllByRole('radio')
fireEvent.click(radioButtons[0])
expect(emitMock).toHaveBeenCalledWith(tableProps.name, 'select', ['rowname1'])
expect(emitMock).toHaveBeenCalledTimes(1)
})

it('Fires event - multiple selection - select single with initial values', async () => {
const
values = ['rowname2', 'rowname3'],
props = { ...tableProps, multiple: true, events: ['select'], values },
{ getAllByRole } = render(<XTable model={{ ...props }} />),
checkboxes = getAllByRole('checkbox')
fireEvent.click(checkboxes[1])
expect(emitMock).toHaveBeenCalledWith(tableProps.name, 'select', ['rowname1', ...values])
expect(emitMock).toHaveBeenCalledTimes(1)
})

it('Clicks a column - link set on second col', () => {
tableProps = {
...tableProps,
Expand Down
7 changes: 6 additions & 1 deletion ui/src/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ export const
[groupByKey, setGroupByKey] = React.useState('*'),
contentRef = React.useRef<Fluent.IScrollablePane | null>(null),
tableRef = React.useRef<{ resetSortIcons: () => void } | null>(null),
skipNextEventEmit = React.useRef<B>(false),
groupByOptions: Fluent.IDropdownOption[] = React.useMemo(() =>
groupable ? [{ key: '*', text: '(No Grouping)' }, ...m.columns.map(col => ({ key: col.name, text: col.label }))] : [], [m.columns, groupable]
),
Expand Down Expand Up @@ -913,7 +914,7 @@ export const
onSelectionChanged: () => {
const selectedItemKeys = selection.getSelection().map(item => item.key as S)
wave.args[m.name] = selectedItemKeys
if (m.events?.includes('select')) wave.emit(m.name, 'select', selectedItemKeys)
if (!skipNextEventEmit.current && m.events?.includes('select')) wave.emit(m.name, 'select', selectedItemKeys)
}
}), [m.name, m.events]),
computeHeight = () => {
Expand Down Expand Up @@ -955,11 +956,15 @@ export const
React.useEffect(() => {
wave.args[m.name] = []
if (isSingle && m.value) {
skipNextEventEmit.current = true
selection.setKeySelected(m.value, true, false)
skipNextEventEmit.current = false
wave.args[m.name] = [m.value]
}
else if (isMultiple && m.values) {
skipNextEventEmit.current = true
m.values.forEach(v => selection.setKeySelected(v, true, false))
skipNextEventEmit.current = false
wave.args[m.name] = m.values
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down