-
Notifications
You must be signed in to change notification settings - Fork 30
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
base: main
Are you sure you want to change the base?
Changes from all commits
ec5fb7d
46b1df6
bf37250
df1ea11
4dd00d2
0b5e162
40deec8
63e7331
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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` |
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { Stack } from '@ultraviolet/ui' | ||
|
|
||
| import { RichTextInputField } from '..' | ||
| import { Submit } from '../../../components/Submit' | ||
|
|
||
| import { Template } from './Template.stories' | ||
|
|
||
| import type { StoryFn } from '@storybook/react-vite' | ||
| import type { ComponentProps } from 'react' | ||
|
|
||
| export const Required: StoryFn< | ||
| ComponentProps<typeof RichTextInputField> | ||
| > = args => ( | ||
| <Stack gap={1}> | ||
| <RichTextInputField {...args} /> | ||
| <Submit>Submit</Submit> | ||
| </Stack> | ||
| ) | ||
|
|
||
| Required.args = { ...Template.args, required: true } | ||
| 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', | ||
| } |
| 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' |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,105 @@ | ||||||||||||||||
| 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.getByText('Submit')) | ||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. always use getByRole if possible. This is more precise, because you want to click on a button, not a text
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
| 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.getByTitle('ItalicIcon').closest('button') | ||||||||||||||||
| const bulletListButton = screen | ||||||||||||||||
| .getByTitle('ListBulletIcon') | ||||||||||||||||
| .closest('button') | ||||||||||||||||
|
Comment on lines
+78
to
+81
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
| 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.getByText('Submit')) | ||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
| await waitFor(() => { | ||||||||||||||||
| expect(onSubmit).toHaveBeenCalledOnce() | ||||||||||||||||
| expect(onSubmit.mock.calls[0][0]).toEqual({ | ||||||||||||||||
| test: '<ul><li><p><em>Styled item</em></p></li></ul>', | ||||||||||||||||
| }) | ||||||||||||||||
| }) | ||||||||||||||||
| }) | ||||||||||||||||
| }) | ||||||||||||||||
| 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! })} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this logic should be in the RichTextEditor component, not the field |
||
| /> | ||
| ) | ||
| } | ||
| 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 aria-describedby={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> | ||
| ) |
Uh oh!
There was an error while loading. Please reload this page.