-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sdk-react): Contentlets with empty HTML content needs to have min…
…-height to edit it in UVE (#30122) ### Proposed Changes https://github.com/user-attachments/assets/0873e554-def3-41fb-aafd-42510a2ab236 --------- Co-authored-by: Kevin Davila <[email protected]>
- Loading branch information
1 parent
030f9f8
commit b5e9fac
Showing
4 changed files
with
87 additions
and
4 deletions.
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
48 changes: 48 additions & 0 deletions
48
core-web/libs/sdk/react/src/lib/hooks/useCheckHaveContent.spec.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,48 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import React from 'react'; | ||
|
||
import { useCheckHaveContent } from './useCheckHaveContent'; | ||
|
||
describe('useCheckHaveContent', () => { | ||
it('should return true when there is content', () => { | ||
const div = document.createElement('div'); | ||
const child = document.createElement('div'); | ||
div.appendChild(child); | ||
|
||
jest.spyOn(child, 'getBoundingClientRect').mockImplementation(() => { | ||
return { | ||
bottom: 0, | ||
height: 100, | ||
left: 0, | ||
right: 0, | ||
top: 0, | ||
width: 0, | ||
x: 0, | ||
y: 0, | ||
toJSON: () => ({}) | ||
}; | ||
}); | ||
|
||
jest.spyOn(React, 'useRef').mockReturnValue({ | ||
current: div | ||
}); | ||
|
||
const { result } = renderHook(() => useCheckHaveContent()); | ||
|
||
expect(result.current.haveContent).toBe(true); | ||
}); | ||
|
||
it('should return false when there is no content', () => { | ||
const div = document.createElement('div'); | ||
const child = document.createElement('div'); | ||
div.appendChild(child); | ||
|
||
jest.spyOn(React, 'useRef').mockReturnValue({ | ||
current: div | ||
}); | ||
|
||
const { result } = renderHook(() => useCheckHaveContent()); | ||
|
||
expect(result.current.haveContent).toBe(false); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
core-web/libs/sdk/react/src/lib/hooks/useCheckHaveContent.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,30 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
|
||
export const useCheckHaveContent = () => { | ||
const contentletDivRef = useRef<HTMLDivElement | null>(null); | ||
const [haveContent, setHaveContent] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
if (!contentletDivRef.current) { | ||
return; | ||
} | ||
|
||
const childElement = contentletDivRef.current.firstElementChild; | ||
|
||
if (!childElement) { | ||
return; | ||
} | ||
|
||
const height = childElement.getBoundingClientRect().height; | ||
|
||
if (height > 0) { | ||
setHaveContent(true); | ||
|
||
return; | ||
} | ||
|
||
setHaveContent(false); | ||
}, [contentletDivRef]); | ||
|
||
return { contentletDivRef, haveContent }; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.