-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tab-selection): add ability to auto select the tab based on data …
…availability
- Loading branch information
1 parent
bdce6c3
commit a47ad09
Showing
3 changed files
with
90 additions
and
1 deletion.
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
51 changes: 51 additions & 0 deletions
51
packages/pipelines/src/components/Output/hooks/__tests__/useFirstLoadedInput.test.ts
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,51 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { useFirstLoadedInput } from '../useFirstLoadedInput'; | ||
|
||
describe('useFirstLoadedInput', () => { | ||
test('should handle the invalid value', () => { | ||
const { result } = renderHook(() => useFirstLoadedInput([])); | ||
expect(result.current).toBeUndefined(); | ||
}); | ||
|
||
test('should return the default value if the input value is empty', () => { | ||
const { result } = renderHook(() => useFirstLoadedInput([{ name: 'tab-1', value: [] }])); | ||
expect(result.current).toBe('tab-1'); | ||
}); | ||
test('should return the default value if the input value is undefined', () => { | ||
const { result } = renderHook(() => | ||
useFirstLoadedInput([{ name: 'tab-1', value: undefined as any }]), | ||
); | ||
expect(result.current).toBe('tab-1'); | ||
}); | ||
|
||
test('should identify the first input with some data in it', () => { | ||
const { result } = renderHook(() => | ||
useFirstLoadedInput([ | ||
{ name: 'tab-1', value: [] }, | ||
{ name: 'tab-2', value: ['data'] }, | ||
]), | ||
); | ||
|
||
expect(result.current).toBe('tab-2'); | ||
}); | ||
|
||
test('should work with object values', () => { | ||
const { result } = renderHook(() => | ||
useFirstLoadedInput([ | ||
{ name: 'tab-1', value: [] }, | ||
{ name: 'tab-2', value: { key: 'data' } }, | ||
]), | ||
); | ||
expect(result.current).toBe('tab-2'); | ||
}); | ||
|
||
test('should work with string values', () => { | ||
const { result } = renderHook(() => | ||
useFirstLoadedInput([ | ||
{ name: 'tab-1', value: [] }, | ||
{ name: 'tab-2', value: 'data streaming' }, | ||
]), | ||
); | ||
expect(result.current).toBe('tab-2'); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
packages/pipelines/src/components/Output/hooks/useFirstLoadedInput.ts
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,25 @@ | ||
import React from 'react'; | ||
|
||
export const useFirstLoadedInput = <T extends Object>(inputs: { name: T; value: any }[]): T => { | ||
const [firstLoadedInput, setFirstLoadedInput] = React.useState<T>(); | ||
|
||
React.useEffect(() => { | ||
for (let i = 0; i < inputs.length; i++) { | ||
const inputValue = inputs[i].value; | ||
if (typeof inputValue === 'object') { | ||
if (Array.isArray(inputValue) && inputValue.length > 0) { | ||
setFirstLoadedInput(inputs[i].name); | ||
break; | ||
} else if (!Array.isArray(inputValue) && Object.keys(inputValue).length > 0) { | ||
setFirstLoadedInput(inputs[i].name); | ||
break; | ||
} | ||
} else if (inputValue !== undefined && inputValue !== '') { | ||
setFirstLoadedInput(inputs[i].name); | ||
break; | ||
} | ||
} | ||
}, [inputs]); | ||
|
||
return firstLoadedInput ?? inputs?.[0]?.name; | ||
}; |