Skip to content

Commit 338222f

Browse files
committed
Document component argument convention
Positional/named keywords are for component config, **kwargs are for HTML attributes. Added to CLAUDE.md and README Customization section.
1 parent 082cbf2 commit 338222f

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

CLAUDE.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Superform Development Guide
2+
3+
## Component Argument Convention
4+
5+
HTML components follow a strict convention for arguments:
6+
7+
- **Positional/named keyword arguments** are for component configuration (non-HTML concerns)
8+
- **`**kwargs`** are for HTML attributes that pass through to the rendered element
9+
10+
```ruby
11+
# field is positional, value: is config, **attributes are HTML
12+
class Radio < Field
13+
def initialize(field, value:, **attributes)
14+
super(field, **attributes)
15+
@value = value
16+
end
17+
end
18+
19+
# options: and multiple: are config, **attributes are HTML
20+
class Select < Field
21+
def initialize(field, options:, multiple: false, **attributes)
22+
super(field, **attributes)
23+
@options = options
24+
@multiple = multiple
25+
end
26+
end
27+
```
28+
29+
The Field methods mirror this: config args are explicit, HTML attributes flow through as `**kwargs`.
30+
31+
```ruby
32+
def radio(value, **attributes)
33+
Components::Radio.new(field, value:, **attributes)
34+
end
35+
36+
def select(*options, multiple: false, **attributes, &)
37+
Components::Select.new(field, options:, multiple:, **attributes, &)
38+
end
39+
```
40+
41+
## Key Architecture
42+
43+
- **Field** is a data binding object (model, key, value, DOM name) — not HTML
44+
- **Components** are HTML elements — their kwargs should be HTML attributes
45+
- **Form** is a Phlex component that contains fields and renders the `<form>` tag
46+
- Customization is through subclassing, not runtime options

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ This gives you complete control over markup, styling, and component composition
165165

166166
Superforms are built out of [Phlex components](https://www.phlex.fun/html/components/). The method names correspond with the HTML tag, its arguments are attributes, and the blocks are the contents of the tag.
167167

168+
When building custom components, follow this convention: **positional or named keyword arguments** are for component configuration (non-HTML concerns like `value:`, `options:`, `multiple:`), and **`**kwargs`** are for HTML attributes that pass through to the rendered element. This keeps the boundary between component logic and HTML clean.
169+
168170
```ruby
169171
# ./app/components/form.rb
170172
class Components::Form < Superform::Rails::Form

0 commit comments

Comments
 (0)