-
Notifications
You must be signed in to change notification settings - Fork 185
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
feat(Form): добавляем новый компонент Form на замену FormLayout #4576
Closed
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2806e70
feat(Form): add new Form component
eugpoloz b2072b9
docs(Calendar): FormLayout => Form
eugpoloz a6f600a
docs(CalendarRange): FormLayout => Form
eugpoloz b968da6
docs(DateInput): FormLayout => Form
eugpoloz c9958d8
docs(DateRangeInput): FormLayout => Form
eugpoloz be33d04
docs(FormItem): FormLayout => Form
eugpoloz 066b21c
docs(FormLayoutGroup): FormLayout => Form
eugpoloz 1998c3c
docs(Input): FormLayout => Form
eugpoloz 85e5d28
docs(Popover): FormLayout => Form
eugpoloz a742621
docs(Radio): FormLayout => Form
eugpoloz 63244c7
docs(RadioGroup): FormLayout => Form
eugpoloz 36f1df8
test(RadioGroup): FormLayout => Form in e2e
eugpoloz 9130c08
docs(SubnavigationBar): FormLayout => Form
eugpoloz a0cc2d4
feat(FormLayout): deprecate FormLayout
eugpoloz 702fb9a
docs(Form): update README.md
eugpoloz 0ace106
test(Form): cover onSubmit w/ unit tests
eugpoloz 737717c
fix(Form,FormLayout): update version number
eugpoloz 2cdbfc6
fix(FormLayout): update deprecation notice
eugpoloz e6dbe3e
docs(Form): add storybook story
eugpoloz 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 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
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
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,7 @@ | ||
import * as React from 'react'; | ||
import { a11yBasicTest } from '../../testing/a11y'; | ||
import { Form } from './Form'; | ||
|
||
describe('Form', () => { | ||
a11yBasicTest((props) => <Form {...props} />); | ||
}); |
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,4 @@ | ||
.Form { | ||
position: relative; | ||
display: block; | ||
} |
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,47 @@ | ||
import React from 'react'; | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
import { withSinglePanel, withVKUILayout } from '../../storybook/VKUIDecorators'; | ||
import { CanvasFullLayout, DisableCartesianParam } from '../../storybook/constants'; | ||
import { Button } from '../Button/Button'; | ||
import { FormItem } from '../FormItem/FormItem'; | ||
import { Group } from '../Group/Group'; | ||
import { Input } from '../Input/Input'; | ||
import { Form, FormProps } from './Form'; | ||
|
||
const story: Meta<FormProps> = { | ||
title: 'Forms/Form', | ||
component: Form, | ||
parameters: { ...CanvasFullLayout, ...DisableCartesianParam }, | ||
}; | ||
|
||
export default story; | ||
|
||
export const Playground: StoryObj<FormProps> = { | ||
render: (props) => ( | ||
<Form {...props}> | ||
<FormItem top="Пароль"> | ||
<Input type="password" placeholder="Введите пароль" /> | ||
</FormItem> | ||
<FormItem> | ||
<Button type="submit" size="l"> | ||
Сохранить | ||
</Button> | ||
</FormItem> | ||
</Form> | ||
), | ||
args: { | ||
preventDefault: true, | ||
onSubmit: () => { | ||
console.log('Форма сохранена!'); | ||
}, | ||
}, | ||
decorators: [ | ||
(Component, context) => ( | ||
<Group> | ||
<Component {...context.args} /> | ||
</Group> | ||
), | ||
withSinglePanel, | ||
withVKUILayout, | ||
], | ||
}; |
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,46 @@ | ||
import * as React from 'react'; | ||
import { createEvent, fireEvent, render } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { baselineComponent } from '../../testing/utils'; | ||
import { Button } from '../Button/Button'; | ||
import { Form } from './Form'; | ||
|
||
describe('Form', () => { | ||
baselineComponent(Form); | ||
|
||
describe('checks e.preventDefault()', () => { | ||
const handlePreventDefault = (preventDefault = true) => { | ||
const { getByTestId } = render( | ||
<Form data-testid="form" preventDefault={preventDefault}> | ||
<Button type="submit">__submit__</Button> | ||
</Form>, | ||
); | ||
const form = getByTestId('form'); | ||
const submitForm = createEvent.submit(form); | ||
|
||
fireEvent(form, submitForm); | ||
expect(submitForm.defaultPrevented).toBe(preventDefault); | ||
}; | ||
|
||
it('if preventDefault={true} call e.preventDefault()', () => { | ||
return handlePreventDefault(true); | ||
}); | ||
|
||
it("if preventDefault={false} DON'T call e.preventDefault()", () => { | ||
return handlePreventDefault(false); | ||
}); | ||
}); | ||
|
||
it('calls passed onSubmit()', () => { | ||
const onSubmit = jest.fn(); | ||
|
||
const { getByText } = render( | ||
<Form onSubmit={onSubmit}> | ||
<Button type="submit">__submit__</Button> | ||
</Form>, | ||
); | ||
|
||
userEvent.click(getByText('__submit__')); | ||
expect(onSubmit).toBeCalledTimes(1); | ||
}); | ||
}); |
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,41 @@ | ||
import * as React from 'react'; | ||
import { classNames } from '@vkontakte/vkjs'; | ||
import { HasComponent, HasRootRef } from '../../types'; | ||
import styles from './Form.module.css'; | ||
|
||
export interface FormProps | ||
extends React.AllHTMLAttributes<HTMLElement>, | ||
HasRootRef<HTMLElement>, | ||
HasComponent { | ||
preventDefault?: boolean; | ||
} | ||
|
||
/** | ||
* @since v5.4.0 | ||
* @see https://vkcom.github.io/VKUI/#/Form | ||
*/ | ||
export const Form = ({ | ||
Component = 'form', | ||
onSubmit: onSubmitProp, | ||
preventDefault = true, | ||
getRootRef, | ||
className, | ||
children, | ||
...restProps | ||
}: FormProps) => { | ||
const onSubmit = (e: React.FormEvent<HTMLElement>) => { | ||
preventDefault && e.preventDefault(); | ||
onSubmitProp?.(e); | ||
}; | ||
|
||
return ( | ||
<Component | ||
{...restProps} | ||
className={classNames(styles['Form'], className)} | ||
onSubmit={onSubmit} | ||
ref={getRootRef} | ||
> | ||
{children} | ||
</Component> | ||
); | ||
}; |
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,43 @@ | ||
Компонент-надстройка над `form`. Принимает все валидные для этого элемента свойства. | ||
|
||
По умолчанию в `onSubmit` происходит `e.preventDefault()`, чтобы избежать перезагрузки страницы. Вы можете управлять этим поведением с помощью свойства `preventDefault`. | ||
|
||
Не забудьте добавить любую кнопку с `type="submit"`. Тогда данные вашей формы будут успешно отправляться как по клику на нее, так и по нажатию Enter. | ||
|
||
```jsx | ||
const Example = () => { | ||
const onSubmit = () => { | ||
console.log('Способ оплаты сохранен!'); | ||
}; | ||
|
||
return ( | ||
<View activePanel="panel"> | ||
<Panel id="panel"> | ||
<PanelHeader>Form</PanelHeader> | ||
<Group> | ||
<Form onSubmit={onSubmit}> | ||
<FormItem top="Откуда списать"> | ||
<Radio name="radio" value="1" description="Баланс 7 320 ₽" defaultChecked> | ||
VK Pay | ||
</Radio> | ||
<Radio name="radio" value="2"> | ||
Mastercard **** 1234 | ||
</Radio> | ||
<Radio name="radio" value="3" description="Заблокирована" disabled> | ||
Visa **** 4321 | ||
</Radio> | ||
</FormItem> | ||
<FormItem> | ||
<Button stretched size="l" type="submit"> | ||
Сохранить способ оплаты | ||
</Button> | ||
</FormItem> | ||
</Form> | ||
</Group> | ||
</Panel> | ||
</View> | ||
); | ||
}; | ||
|
||
<Example />; | ||
``` |
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
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Может нам и не нужен компонент
Form
? Ведь формы это уже что-то посложнее и от проекта к проекту отличаютсяС нашей стороны мы отдаём
FormLayoutGroup
, который помогает настроить визуальную часть, а всё остальное уже не задача ui китаПользователь сам своей стороне оборачивает в
<form>
или использует готовую библиотеку для работы с формами (например,react-final-form
)И вот задепрейкить
FormLayout
точно надо 👍