mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
This PR introduces FormKit, a component-based form library designed to simplify form creation and management. This library provides a single `Form` component, various field components, controls, validation mechanisms, and customization options. Additionally, it includes helpers to facilitate testing and writing specifications for forms.
1. **Form Component**:
- The main component that encapsulates form logic and structure.
- Yields various utilities like `Field`, `Submit`, `Alert`, etc.
**Example Usage**:
```gjs
import Form from "discourse/form";
<template>
<Form as |form|>
<form.Field
@name="username"
@title="Username"
@validation="required"
as |field|
>
<field.Input />
</form.Field>
<form.Field @name="age" @title="Age" as |field|>
<field.Input @type="number" />
</form.Field>
<form.Submit />
</Form>
</template>
```
2. **Validation**:
- Built-in validation rules such as `required`, `number`, `length`, and `url`.
- Custom validation callbacks for more complex validation logic.
**Example Usage**:
```javascript
validateUsername(name, value, data, { addError }) {
if (data.bar / 2 === value) {
addError(name, "That's not how maths work.");
}
}
```
```hbs
<form.Field @name="username" @validate={{this.validateUsername}} />
```
3. **Customization**:
- Plugin outlets for extending form functionality.
- Styling capabilities through propagated attributes.
- Custom controls with properties provided by `form` and `field`.
**Example Usage**:
```hbs
<Form class="my-form" as |form|>
<form.Field class="my-field" as |field|>
<MyCustomControl id={{field.id}} @onChange={{field.set}} />
</form.Field>
</Form>
```
4. **Helpers for Testing**:
- Test assertions for form and field validation.
**Example usage**:
```javascript
assert.form().hasErrors("the form shows errors");
assert.form().field("foo").hasValue("bar", "user has set the value");
```
- Helper for interacting with he form
**Example usage**:
```javascript
await formKit().field("foo").fillIn("bar");
```
5. **Page Object for System Specs**:
- Page objects for interacting with forms in system specs.
- Methods for submitting forms, checking alerts, and interacting with fields.
**Example Usage**:
```ruby
form = PageObjects::Components::FormKit.new(".my-form")
form.submit
expect(form).to have_an_alert("message")
```
**Field Interactions**:
```ruby
field = form.field("foo")
expect(field).to have_value("bar")
field.fill_in("bar")
```
6. **Collections handling**:
- A specific component to handle array of objects
**Example Usage**:
```gjs
<Form @data={{hash foo=(array (hash bar=1) (hash bar=2))}} as |form|>
<form.Collection @name="foo" as |collection|>
<collection.Field @name="bar" @title="Bar" as |field|>
<field.Input />
</collection.Field>
</form.Collection>
</Form>
```
70 lines
688 B
SCSS
70 lines
688 B
SCSS
.form-kit__col {
|
|
flex: 0 0 auto;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
@include breakpoint("large") {
|
|
.--col-1,
|
|
.--col-2,
|
|
.--col-3,
|
|
.--col-4,
|
|
.--col-5,
|
|
.--col-6,
|
|
.--col-7,
|
|
.--col-8,
|
|
.--col-9,
|
|
.--col-10,
|
|
.--col-11,
|
|
.--col-12 {
|
|
width: 100% !important;
|
|
}
|
|
}
|
|
|
|
.--col-1 {
|
|
width: 8.33333333%;
|
|
}
|
|
|
|
.--col-2 {
|
|
width: 16.66666667%;
|
|
}
|
|
|
|
.--col-3 {
|
|
width: 25%;
|
|
}
|
|
|
|
.--col-4 {
|
|
width: 33.33333333%;
|
|
}
|
|
|
|
.--col-5 {
|
|
width: 41.66666667%;
|
|
}
|
|
|
|
.--col-6 {
|
|
width: 50%;
|
|
}
|
|
|
|
.--col-7 {
|
|
width: 58.33333333%;
|
|
}
|
|
|
|
.--col-8 {
|
|
width: 66.66666667%;
|
|
}
|
|
|
|
.--col-9 {
|
|
width: 75%;
|
|
}
|
|
|
|
.--col-10 {
|
|
width: 83.33333333%;
|
|
}
|
|
|
|
.--col-11 {
|
|
width: 91.66666667%;
|
|
}
|
|
|
|
.--col-12 {
|
|
width: 100%;
|
|
}
|