-
Notifications
You must be signed in to change notification settings - Fork 31
feat(RichTextInput): create new component #6318
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
Open
JulienSaguez
wants to merge
12
commits into
main
Choose a base branch
from
feat/rich-text-editor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3b5193a
feat(RichTextEditor): create new component
JulienSaguez 635d96f
fix(RichTextEditor): fix threads and move component in compositions f…
JulienSaguez dd5dcc5
fix(RichTextEditor): fix tests
JulienSaguez 8093d0a
fix(RichTextEditor): fix anothers threads
JulienSaguez 21e1e8a
fix(RichTextEditor): add errorLabel prop
JulienSaguez 2aaf8cf
fix(RichTextEditor): update snapshots
JulienSaguez eda23f9
fix(RichTextEditor): fix threads part one
JulienSaguez 8aaeee0
fix(RichTextInput): fix threads part two and rename to richtextinput
JulienSaguez 73bf2f9
fix(RichTextInput): fix another threads
JulienSaguez bf66da2
fix(RichTextInput): update snapshot
JulienSaguez 675882b
fix(RichTextInput): fix last threads
JulienSaguez b1b3a8b
fix(RichTextInput): fix snapshot
JulienSaguez 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@ultraviolet/form": minor | ||
| "@ultraviolet/ui": minor | ||
| --- | ||
|
|
||
| `RichTextInput`: create component `RichTextInput` and `RichTextInputField` |
5 changes: 5 additions & 0 deletions
5
packages/form/src/compositions/RichTextInputField/__stories__/Playground.stories.tsx
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,5 @@ | ||
| import { Template } from './Template.stories' | ||
|
|
||
| export const Playground = Template.bind({}) | ||
|
|
||
| Playground.args = Template.args |
5 changes: 5 additions & 0 deletions
5
packages/form/src/compositions/RichTextInputField/__stories__/Required.stories.tsx
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,5 @@ | ||
| import { Template } from './Template.stories' | ||
|
|
||
| export const Required = Template.bind({}) | ||
|
|
||
| Required.args = { ...Template.args, required: true } |
21 changes: 21 additions & 0 deletions
21
packages/form/src/compositions/RichTextInputField/__stories__/Template.stories.tsx
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,21 @@ | ||
| import { Stack } from '@ultraviolet/ui' | ||
|
|
||
| import { RichTextInputField } from '..' | ||
| import { Submit } from '../../../components' | ||
|
|
||
| import type { StoryFn } from '@storybook/react-vite' | ||
| import type { ComponentProps } from 'react' | ||
|
|
||
| export const Template: StoryFn< | ||
| ComponentProps<typeof RichTextInputField> | ||
| > = args => ( | ||
| <Stack gap={1}> | ||
| <RichTextInputField {...args} /> | ||
| <Submit>Submit</Submit> | ||
| </Stack> | ||
| ) | ||
|
|
||
| Template.args = { | ||
| label: 'Label', | ||
| name: 'richTextInput', | ||
| } |
74 changes: 74 additions & 0 deletions
74
packages/form/src/compositions/RichTextInputField/__stories__/index.stories.tsx
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,74 @@ | ||
| import { Snippet, Stack, Text } from '@ultraviolet/ui' | ||
|
|
||
| import { RichTextInputField } from '..' | ||
| import { useForm } from '../../..' | ||
| import { Form } from '../../../components' | ||
| import { mockErrors } from '../../../mocks' | ||
|
|
||
| import type { Meta } from '@storybook/react-vite' | ||
|
|
||
| export default { | ||
| component: RichTextInputField, | ||
| decorators: [ | ||
| ChildStory => { | ||
| const methods = useForm() | ||
| const { | ||
| errors, | ||
| isDirty, | ||
| isSubmitting, | ||
| touchedFields, | ||
| submitCount, | ||
| dirtyFields, | ||
| isValid, | ||
| isLoading, | ||
| isSubmitted, | ||
| isValidating, | ||
| isSubmitSuccessful, | ||
| } = methods.formState | ||
|
|
||
| return ( | ||
| <Form errors={mockErrors} methods={methods} onSubmit={() => {}}> | ||
| <Stack gap={2}> | ||
| <ChildStory /> | ||
| <Stack gap={1}> | ||
| <Text as="p" variant="bodyStrong"> | ||
| Form input values: | ||
| </Text> | ||
| <Snippet initiallyExpanded prefix="lines"> | ||
| {JSON.stringify(methods.watch(), null, 1)} | ||
| </Snippet> | ||
| </Stack> | ||
| <Stack gap={1}> | ||
| <Text as="p" variant="bodyStrong"> | ||
| Form values: | ||
| </Text> | ||
| <Snippet prefix="lines"> | ||
| {JSON.stringify( | ||
| { | ||
| errors, | ||
| isDirty, | ||
| isSubmitting, | ||
| touchedFields, | ||
| submitCount, | ||
| dirtyFields, | ||
| isValid, | ||
| isLoading, | ||
| isSubmitted, | ||
| isValidating, | ||
| isSubmitSuccessful, | ||
| }, | ||
| null, | ||
| 1, | ||
| )} | ||
| </Snippet> | ||
| </Stack> | ||
| </Stack> | ||
| </Form> | ||
| ) | ||
| }, | ||
| ], | ||
| title: 'Form/Components/Compositions/RichTextInputField', | ||
| } as Meta | ||
|
|
||
| export { Playground } from './Playground.stories' | ||
| export { Required } from './Required.stories' |
383 changes: 383 additions & 0 deletions
383
...ages/form/src/compositions/RichTextInputField/__tests__/__snapshots__/index.test.tsx.snap
Large diffs are not rendered by default.
Oops, something went wrong.
103 changes: 103 additions & 0 deletions
103
packages/form/src/compositions/RichTextInputField/__tests__/index.test.tsx
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,103 @@ | ||
| import { renderHook, screen, waitFor } from '@testing-library/react' | ||
| import { userEvent } from '@testing-library/user-event' | ||
| import { mockFormErrors, renderWithForm, renderWithTheme } from '@utils/test' | ||
| import { useForm } from 'react-hook-form' | ||
| import { describe, expect, test, vi } from 'vitest' | ||
|
|
||
| import { RichTextInputField } from '..' | ||
| import { Submit } from '../../../components' | ||
| import { Form } from '../../../components/Form' | ||
|
|
||
| describe('richTextInputField', () => { | ||
| test('should render correctly', () => { | ||
| const { asFragment } = renderWithForm( | ||
| <RichTextInputField label="Test" name="test" />, | ||
| ) | ||
|
|
||
| expect(asFragment()).toMatchSnapshot() | ||
| }) | ||
|
|
||
| test('should render correctly generated', async () => { | ||
| const onSubmit = vi.fn() | ||
| const { result } = renderHook(() => | ||
| useForm<{ test: string }>({ defaultValues: { test: '' } }), | ||
| ) | ||
|
|
||
| const { asFragment } = renderWithTheme( | ||
| <Form | ||
| errors={mockFormErrors} | ||
| methods={result.current} | ||
| onSubmit={onSubmit} | ||
| > | ||
| <RichTextInputField label="Test" name="test" required /> | ||
| <Submit>Submit</Submit> | ||
| </Form>, | ||
| ) | ||
|
|
||
| await userEvent.click(screen.getByRole('button', { name: 'Submit' })) | ||
| await waitFor(() => { | ||
| expect(onSubmit).toHaveBeenCalledTimes(0) | ||
| }) | ||
|
|
||
| const doc = document.querySelector<HTMLDivElement>( | ||
| '[contenteditable="true"]', | ||
| ) | ||
| if (!doc) { | ||
| throw new Error('RichTextInput contenteditable not found') | ||
| } | ||
| await userEvent.click(doc) | ||
| await userEvent.type(doc, 'This is an example') | ||
| await userEvent.click(screen.getByRole('button', { name: 'Submit' })) | ||
|
|
||
| await waitFor(() => { | ||
| expect(onSubmit).toHaveBeenCalledOnce() | ||
| expect(onSubmit.mock.calls[0][0]).toEqual({ | ||
| test: '<p>This is an example</p>', | ||
| }) | ||
| }) | ||
| expect(asFragment()).toMatchSnapshot() | ||
| }) | ||
|
|
||
| test('should submit rich text with style and list', async () => { | ||
| const onSubmit = vi.fn() | ||
| const { result } = renderHook(() => | ||
| useForm<{ test: string }>({ defaultValues: { test: '' } }), | ||
| ) | ||
|
|
||
| renderWithTheme( | ||
| <Form | ||
| errors={mockFormErrors} | ||
| methods={result.current} | ||
| onSubmit={onSubmit} | ||
| > | ||
| <RichTextInputField label="Test" name="test" required /> | ||
| <Submit>Submit</Submit> | ||
| </Form>, | ||
| ) | ||
|
|
||
| const italicButton = screen.getByRole('button', { name: 'Italic' }) | ||
| const bulletListButton = screen.getByRole('button', { name: 'Bullet List' }) | ||
| expect(italicButton).not.toBeNull() | ||
| expect(bulletListButton).not.toBeNull() | ||
|
|
||
| const doc = document.querySelector<HTMLDivElement>( | ||
| '[contenteditable="true"]', | ||
| ) | ||
| if (!doc) { | ||
| throw new Error('RichTextInput contenteditable not found') | ||
| } | ||
| await userEvent.click(doc) | ||
| await userEvent.click(italicButton) | ||
| await userEvent.type(doc, 'Styled ') | ||
| await userEvent.click(bulletListButton) | ||
| await userEvent.type(doc, 'item') | ||
| await userEvent.click(screen.getByRole('button', { name: 'Submit' })) | ||
|
|
||
| await waitFor(() => { | ||
| expect(onSubmit).toHaveBeenCalledOnce() | ||
| expect(onSubmit.mock.calls[0][0]).toEqual({ | ||
| test: '<ul><li><p><em>Styled item</em></p></li></ul>', | ||
| }) | ||
| }) | ||
| }) | ||
| }) |
69 changes: 69 additions & 0 deletions
69
packages/form/src/compositions/RichTextInputField/index.tsx
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,69 @@ | ||
| 'use client' | ||
|
|
||
| import { RichTextInput } from '@ultraviolet/ui/compositions/RichTextInput' | ||
| import { useController } from 'react-hook-form' | ||
|
|
||
| import { useErrors } from '../../providers' | ||
|
|
||
| import type { BaseFieldProps } from '../../types' | ||
| import type { ComponentProps, FocusEvent } from 'react' | ||
| import type { FieldPath, FieldValues, Path, PathValue } from 'react-hook-form' | ||
|
|
||
| export type RichTextInputFieldProps< | ||
| TFieldValues extends FieldValues, | ||
| TFieldName extends FieldPath<TFieldValues>, | ||
| > = BaseFieldProps<TFieldValues, TFieldName> & | ||
| Omit<ComponentProps<typeof RichTextInput>, 'value' | 'onChange' | 'error'> | ||
|
|
||
| export const RichTextInputField = < | ||
| TFieldValues extends FieldValues, | ||
| TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>, | ||
| >({ | ||
| control, | ||
| errorLabel, | ||
| label, | ||
| onChange, | ||
| name, | ||
| onBlur, | ||
| required = false, | ||
| validate, | ||
| 'aria-label': ariaLabel, | ||
| ...props | ||
| }: RichTextInputFieldProps<TFieldValues, TFieldName>) => { | ||
| const { getError } = useErrors() | ||
|
|
||
| const { | ||
| field, | ||
| fieldState: { error }, | ||
| } = useController<TFieldValues, TFieldName>({ | ||
| control, | ||
| name, | ||
| rules: { | ||
| required, | ||
| validate, | ||
| }, | ||
| }) | ||
|
|
||
| return ( | ||
| <RichTextInput | ||
| {...props} | ||
| error={getError( | ||
| { | ||
| label: errorLabel ?? label ?? ariaLabel ?? name, | ||
| value: field.value, | ||
| }, | ||
| error, | ||
| )} | ||
| onBlur={(event: FocusEvent<HTMLElement>) => { | ||
| onBlur?.(event) | ||
| field.onBlur() | ||
| }} | ||
| onChange={value => { | ||
| field.onChange(value) | ||
| onChange?.(value as PathValue<TFieldValues, Path<TFieldValues>>) | ||
| }} | ||
| value={field.value} | ||
| {...(label ? { label } : { 'aria-label': ariaLabel! })} | ||
| /> | ||
| ) | ||
| } | ||
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,41 @@ | ||
| 'use client' | ||
|
|
||
| import { Row } from '../../components/Row' | ||
| import { Text } from '../../components/Text' | ||
|
|
||
| import type { ReactNode } from 'react' | ||
|
|
||
| export const Notice = ({ | ||
| error, | ||
| success, | ||
| helper, | ||
| disabled, | ||
| sentiment, | ||
| id, | ||
| }: { | ||
| error?: string | boolean | ||
| success?: string | boolean | ||
| helper?: ReactNode | ||
| disabled?: boolean | ||
| sentiment?: 'danger' | 'success' | 'neutral' | ||
| id?: string | ||
| }) => ( | ||
| <div id={id} role="status"> | ||
| <Row gap="1" templateColumns="minmax(0, 1fr) min-content"> | ||
| {error || success || typeof helper === 'string' ? ( | ||
| <Text | ||
| as="p" | ||
| disabled={disabled} | ||
| prominence={error || success ? 'default' : 'weak'} | ||
| sentiment={sentiment} | ||
| variant="caption" | ||
| > | ||
| {error || success || helper} | ||
| </Text> | ||
| ) : null} | ||
| {!(error || success) && typeof helper !== 'string' && helper | ||
| ? helper | ||
| : null} | ||
| </Row> | ||
| </div> | ||
| ) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.