-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate default value (#3646)
- Loading branch information
1 parent
5d8c54d
commit 6c14617
Showing
14 changed files
with
250 additions
and
10 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
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 @@ | ||
export { useEnforceHighlightAutofill } from './use-enforce-highlight-autofill' |
67 changes: 67 additions & 0 deletions
67
packages/picasso-forms/src/RichTextEditor/hooks/use-enforce-highlight-autofill.test.tsx
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,67 @@ | ||
import React from 'react' | ||
import { render, fireEvent } from '@toptal/picasso/test-utils' | ||
|
||
import { useEnforceHighlightAutofill } from './use-enforce-highlight-autofill' | ||
import { useFormConfig } from '../../FormConfig' | ||
|
||
jest.mock('../../FormConfig', () => ({ | ||
useFormConfig: jest.fn(), | ||
})) | ||
|
||
const mockedUseFormConfig = useFormConfig as jest.MockedFunction< | ||
typeof useFormConfig | ||
> | ||
|
||
const TestComponent = () => { | ||
const { enforceHighlightAutofill, registerChangeOrFocus } = | ||
useEnforceHighlightAutofill() | ||
|
||
return ( | ||
<> | ||
<button onClick={registerChangeOrFocus}>trigger</button> | ||
<div data-testid='enforce-highlight-autofill-status'> | ||
{enforceHighlightAutofill ? 'true' : 'false'} | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
describe('useEnforceHighlightAutofill', () => { | ||
describe('when form does not highlight autofilled fields', () => { | ||
it('does not enforce highlighting', () => { | ||
mockedUseFormConfig.mockReturnValue({ highlightAutofill: false }) | ||
const { getByText, getByTestId } = render(<TestComponent />) | ||
const button = getByText('trigger') | ||
const autofillStatusContainer = getByTestId( | ||
'enforce-highlight-autofill-status' | ||
) | ||
|
||
fireEvent.click(button) | ||
|
||
// After first update | ||
expect(autofillStatusContainer).toHaveTextContent('false') | ||
}) | ||
}) | ||
|
||
describe('when form highlights autofilled fields', () => { | ||
it('highlighting is enforced after first update and not enforced after following updates', async () => { | ||
mockedUseFormConfig.mockReturnValue({ highlightAutofill: true }) | ||
const { getByText, getByTestId } = render(<TestComponent />) | ||
|
||
const button = getByText('trigger') | ||
const autofillStatusContainer = getByTestId( | ||
'enforce-highlight-autofill-status' | ||
) | ||
|
||
fireEvent.click(button) | ||
|
||
// After first update | ||
expect(autofillStatusContainer).toHaveTextContent('true') | ||
|
||
fireEvent.click(button) | ||
|
||
// After second update | ||
expect(autofillStatusContainer).toHaveTextContent('false') | ||
}) | ||
}) | ||
}) |
48 changes: 48 additions & 0 deletions
48
packages/picasso-forms/src/RichTextEditor/hooks/use-enforce-highlight-autofill.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 { useCallback, useMemo, useState } from 'react' | ||
|
||
import { useFormConfig } from '../../FormConfig' | ||
|
||
/** | ||
* This hook keeps the highlighted state for Rich Text Editor form fields after | ||
* first `onChange` callback which is triggered by editor itself when initial value is | ||
* provided. Starting from second `onChange` or first `onFocus` invocation (which are | ||
* triggered by user) the field is no longer highlighted. | ||
* | ||
* Editor triggers the very first onChange callback by itself only when initial value | ||
* is provided in order to propagate the value to wrapping form. | ||
*/ | ||
|
||
/** | ||
* After first two editor changes are tracked (the initial one and the one originated | ||
* from user), hook stops responding due to performance reasons. | ||
*/ | ||
const TRACKED_EVENTS_LIMIT = 2 | ||
|
||
export const useEnforceHighlightAutofill = () => { | ||
const { highlightAutofill } = useFormConfig() | ||
const [timesChangeOrFocusTriggered, setTimesChangeOrFocusTriggered] = | ||
useState(0) | ||
|
||
const enforceHighlightAutofill = useMemo(() => { | ||
if (!highlightAutofill) { | ||
return false | ||
} | ||
|
||
return timesChangeOrFocusTriggered < 2 | ||
}, [highlightAutofill, timesChangeOrFocusTriggered]) | ||
|
||
const registerChangeOrFocus = useCallback(() => { | ||
if (!highlightAutofill) { | ||
return | ||
} | ||
|
||
if (timesChangeOrFocusTriggered <= TRACKED_EVENTS_LIMIT) { | ||
setTimesChangeOrFocusTriggered(timesChangeOrFocusTriggered + 1) | ||
} | ||
}, [highlightAutofill, timesChangeOrFocusTriggered]) | ||
|
||
return { | ||
enforceHighlightAutofill, | ||
registerChangeOrFocus, | ||
} | ||
} |
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
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
24 changes: 24 additions & 0 deletions
24
packages/picasso-rich-text-editor/src/LexicalEditor/plugins/TriggerInitialOnChangePlugin.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,24 @@ | ||
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' | ||
import type { EditorState, LexicalEditor } from 'lexical' | ||
|
||
const TriggerInitialOnChangePlugin = ({ | ||
onChange, | ||
}: { | ||
onChange: ( | ||
editorState: EditorState, | ||
editor: LexicalEditor, | ||
tags: Set<string> | ||
) => void | ||
}) => { | ||
const [editor] = useLexicalComposerContext() | ||
|
||
editor.registerUpdateListener(({ editorState, prevEditorState, tags }) => { | ||
if (prevEditorState.isEmpty()) { | ||
onChange(editorState, editor, tags) | ||
} | ||
}) | ||
|
||
return null | ||
} | ||
|
||
export default TriggerInitialOnChangePlugin |
1 change: 1 addition & 0 deletions
1
packages/picasso-rich-text-editor/src/LexicalEditor/plugins/index.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 @@ | ||
export { default as TriggerInitialOnChangePlugin } from './TriggerInitialOnChangePlugin' |
18 changes: 18 additions & 0 deletions
18
packages/picasso-rich-text-editor/src/LexicalEditor/utils/getDomValue.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,18 @@ | ||
import type { ASTType } from '../../RichText' | ||
import { getDomValue } from './getDomValue' | ||
|
||
describe('getDomValue', () => { | ||
it('returns DOM structure from AST', () => { | ||
const inputValue: ASTType = { | ||
type: 'root', | ||
children: [{ type: 'text', value: 'Example of default text' }], | ||
} | ||
|
||
const result = getDomValue(inputValue) | ||
const serializer = new XMLSerializer() | ||
|
||
expect(serializer.serializeToString(result)).toBe( | ||
'<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body>Example of default text</body></html>' | ||
) | ||
}) | ||
}) |
10 changes: 10 additions & 0 deletions
10
packages/picasso-rich-text-editor/src/LexicalEditor/utils/getDomValue.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,10 @@ | ||
import type { HastNode } from 'hast-util-to-dom/lib' | ||
import toHtml from 'hast-util-to-html' | ||
|
||
import type { ASTType } from '../../RichText' | ||
|
||
export const getDomValue = (value: ASTType) => { | ||
const parser = new DOMParser() | ||
|
||
return parser.parseFromString(toHtml(value as HastNode), 'text/html') | ||
} |
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
29 changes: 29 additions & 0 deletions
29
packages/picasso-rich-text-editor/src/LexicalEditor/utils/setEditorValue.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,29 @@ | ||
import type { LexicalEditor as LexicalEditorType } from 'lexical' | ||
import { | ||
$createParagraphNode, | ||
$getRoot, | ||
$isDecoratorNode, | ||
$isElementNode, | ||
} from 'lexical' | ||
import { $generateNodesFromDOM } from '@lexical/html' | ||
|
||
import type { ASTType } from '../../RichText' | ||
import { getDomValue } from './getDomValue' | ||
|
||
export const setEditorValue = (editor: LexicalEditorType, value: ASTType) => { | ||
const domValue = getDomValue(value) | ||
|
||
const root = $getRoot() | ||
const lexicalValueNodes = $generateNodesFromDOM(editor, domValue) | ||
|
||
lexicalValueNodes.forEach(node => { | ||
if ($isElementNode(node) || $isDecoratorNode(node)) { | ||
root.append(node) | ||
} else { | ||
const paragraphNode = $createParagraphNode() | ||
|
||
paragraphNode.append(node) | ||
root.append(paragraphNode) | ||
} | ||
}) | ||
} |
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
Oops, something went wrong.