2023-02-10 03:36:27 +08:00
|
|
|
.d-toggle-switch {
|
|
|
|
--toggle-switch-width: 45px;
|
|
|
|
--toggle-switch-height: 24px;
|
|
|
|
|
|
|
|
&:focus {
|
|
|
|
.d-toggle-switch__checkbox-slider {
|
|
|
|
outline: 2px solid var(--tertiary);
|
2023-09-12 02:26:41 +08:00
|
|
|
outline-offset: 2px;
|
2023-02-10 03:36:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
.d-toggle-switch__checkbox-slider {
|
|
|
|
background-color: var(--primary-high);
|
|
|
|
}
|
|
|
|
|
2023-08-05 06:20:56 +08:00
|
|
|
.d-toggle-switch__checkbox[aria-checked="true"]:not([disabled])
|
2023-02-10 03:36:27 +08:00
|
|
|
+ .d-toggle-switch__checkbox-slider {
|
|
|
|
background-color: var(--tertiary-hover);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
2023-09-12 02:26:41 +08:00
|
|
|
gap: 0.5rem;
|
2023-02-10 03:36:27 +08:00
|
|
|
|
|
|
|
&__label {
|
|
|
|
position: relative;
|
|
|
|
display: inline-block;
|
|
|
|
cursor: pointer;
|
DEV: form-kit
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>
```
2024-07-17 17:59:35 +08:00
|
|
|
margin: 0;
|
2023-02-10 03:36:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
&__checkbox {
|
|
|
|
position: absolute;
|
2023-09-12 02:26:41 +08:00
|
|
|
border: 0;
|
|
|
|
padding: 0;
|
|
|
|
background: transparent;
|
|
|
|
|
|
|
|
&:focus {
|
|
|
|
+ .d-toggle-switch__checkbox-slider {
|
|
|
|
outline: 2px solid var(--tertiary);
|
|
|
|
outline-offset: 2px;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Outline should show only when tabbing, not clicking
|
|
|
|
&:not(:focus-visible) {
|
|
|
|
+ .d-toggle-switch__checkbox-slider {
|
|
|
|
outline: none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-10 03:36:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
&__checkbox[aria-checked="true"] + .d-toggle-switch__checkbox-slider {
|
|
|
|
background-color: var(--tertiary);
|
|
|
|
}
|
|
|
|
|
|
|
|
&__checkbox[aria-checked="true"] + .d-toggle-switch__checkbox-slider::before {
|
|
|
|
left: calc(var(--toggle-switch-width) - 22px);
|
|
|
|
}
|
|
|
|
|
2023-08-05 06:20:56 +08:00
|
|
|
&__checkbox[disabled] + .d-toggle-switch__checkbox-slider {
|
|
|
|
opacity: 0.5;
|
|
|
|
cursor: not-allowed;
|
|
|
|
|
|
|
|
&::before {
|
|
|
|
cursor: not-allowed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 03:36:27 +08:00
|
|
|
&__checkbox-slider {
|
|
|
|
display: inline-block;
|
|
|
|
cursor: pointer;
|
|
|
|
background: var(--primary-low-mid);
|
2023-09-06 04:08:44 +08:00
|
|
|
border-radius: var(--toggle-switch-height);
|
2023-02-10 03:36:27 +08:00
|
|
|
width: var(--toggle-switch-width);
|
|
|
|
height: var(--toggle-switch-height);
|
|
|
|
position: relative;
|
|
|
|
vertical-align: middle;
|
|
|
|
transition: background 0.25s;
|
|
|
|
|
|
|
|
.d-icon {
|
|
|
|
font-size: var(--font-down-1);
|
|
|
|
color: var(--secondary);
|
|
|
|
left: 7px;
|
2023-11-21 14:48:11 +08:00
|
|
|
top: 6px;
|
2023-02-10 03:36:27 +08:00
|
|
|
position: absolute;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
&__checkbox-slider::before,
|
|
|
|
&__checkbox-slider::after {
|
|
|
|
content: "";
|
|
|
|
display: block;
|
|
|
|
position: absolute;
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
&__checkbox-slider::before {
|
|
|
|
background: var(--secondary);
|
|
|
|
border-radius: 50%;
|
2023-09-06 04:08:44 +08:00
|
|
|
width: calc(var(--toggle-switch-height) * 0.75);
|
|
|
|
height: calc(var(--toggle-switch-height) * 0.75);
|
|
|
|
top: calc(var(--toggle-switch-height) * 0.125);
|
|
|
|
left: calc(var(--toggle-switch-height) * 0.125);
|
2023-02-10 03:36:27 +08:00
|
|
|
transition: left 0.25s;
|
2023-09-12 02:26:41 +08:00
|
|
|
|
|
|
|
@media (prefers-reduced-motion: reduce) {
|
|
|
|
transition-duration: 0ms;
|
|
|
|
}
|
2023-02-10 03:36:27 +08:00
|
|
|
}
|
|
|
|
}
|