Skip to content

Commit 06a12d0

Browse files
authored
Table cells are always inputs (#39)
* bump version * try to add nokogiri 1.18.1 for rails 7.1 * revert try to add nokogiri 1.18.1 for rails 7.1 * table cells are always inputs * update test * bump version * version and changelog
1 parent 4a480b4 commit 06a12d0

File tree

6 files changed

+33
-46
lines changed

6 files changed

+33
-46
lines changed

CHANGELOG.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
## [Unreleased](https://github.com/renuo/hotsheet/compare/v0.1.0..HEAD)
1+
<!-- ## [Unreleased](https://github.com/renuo/hotsheet/compare/v0.1.0..HEAD) -->
22

3-
- Configure compatible Ruby/Rails versions for testing
4-
- Improve flash messages layout
3+
## [0.1.1](https://github.com/renuo/hotsheet/releases/tag/v0.1.1) (2025-02-03)
4+
5+
- Improve configuration file usage and logic ([@simon-isler])
6+
- Configure compatible Ruby/Rails versions for testing ([@ignaciosy])
7+
- Improve flash messages layout ([@ignaciosy])
8+
- Form inputs are now always visible for usage simplicity ([@ignaciosy])
59

610
## [0.1.0](https://github.com/renuo/hotsheet/releases/tag/v0.1.0) (2024-11-05)
711

app/assets/javascripts/hotsheet/controllers/editable_attribute_controller.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,12 @@ export default class extends Controller {
55
broadcastUrl: String,
66
resourceName: String,
77
resourceId: Number,
8+
resourceInitialValue: String
89
}
910

10-
static targets = ["readonlyAttribute", "attributeForm", "attributeFormInput"]
11+
static targets = ["attributeFormInput"]
1112

12-
displayInputField() {
13-
// this.broadcastEditIntent()
14-
this.readonlyAttributeTarget.style.display = "none"
15-
this.attributeFormTarget.style.display = "block"
16-
this.attributeFormInputTarget.focus()
17-
}
18-
19-
broadcastEditIntent() {
13+
broadcastEditIntent() { // TODO: trigger on input focus
2014
const headers = {
2115
"Content-Type": "application/json",
2216
"X-CSRF-Token": document.querySelector("meta[name=csrf-token]").content,
@@ -35,14 +29,8 @@ export default class extends Controller {
3529
// Prevent standard submission triggered by Enter press
3630
event.preventDefault()
3731

38-
const previousValue = this.readonlyAttributeTarget.innerText.trim()
3932
const newValue = this.attributeFormInputTarget.value
40-
41-
if (previousValue && previousValue === newValue) {
42-
this.readonlyAttributeTarget.style.display = "block"
43-
this.attributeFormTarget.style.display = "none"
44-
return
45-
}
33+
if (this.resourceInitialValueValue === newValue) return;
4634

4735
// It's important to use requestSubmit() instead of simply submit() as the latter will circumvent the
4836
// Turbo mechanism, causing the PATCH request to be submitted as HTML instead of TURBO_STREAM

app/assets/stylesheets/hotsheet/application.css

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212
--main-padding-x: 2rem;
1313
--main-padding-y: 2rem;
1414

15+
--body-font: 400 16px system-ui, Roboto, Helvetica, Arial, sans-serif;
1516
--bg-color: lightgray;
1617
--success-color: green;
1718
--alert-color: yellow;
1819
--notice-color: blue;
1920
--error-color: red;
21+
22+
--td-padding: 0.5rem;
2023
}
2124

2225
body {
23-
font: 400 16px system-ui, Roboto, Helvetica, Arial, sans-serif;
26+
font: var(--body-font);
2427
margin: 0;
2528
}
2629

@@ -72,12 +75,12 @@ table {
7275
th,
7376
td {
7477
border: 1px solid var(--bg-color);
75-
padding: 0.5rem;
7678
text-align: left;
7779
}
7880

7981
th {
8082
background-color: var(--bg-color);
83+
padding: var(--td-padding);
8184
}
8285

8386
.readonly-attribute {
@@ -88,7 +91,9 @@ table {
8891
.editable-input {
8992
background-color: transparent;
9093
border: none;
91-
width: 100%;
94+
font: var(--body-font);
95+
padding: var(--td-padding);
96+
width: calc(100% - var(--td-padding) * 2);
9297
}
9398
}
9499

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
<%# locals: (attribute:, model:, record:) %>
22

33
<%= turbo_frame_tag "#{dom_id record}-#{attribute}" do %>
4-
<div data-controller="editable-attribute"
5-
data-editable-attribute-broadcast-url-value="<%= broadcast_edit_intent_path %>"
6-
data-editable-attribute-resource-name-value="<%= model.table_name %>"
7-
data-editable-attribute-resource-id-value="<%= record.id %>">
8-
<div class="readonly-attribute"
9-
data-editable-attribute-target="readonlyAttribute"
10-
data-action="click->editable-attribute#displayInputField"
11-
role="button">
12-
<%= record.public_send attribute %>
13-
</div>
14-
15-
<div data-editable-attribute-target="attributeForm" style="display:none">
16-
<%= form_for record, url: "#{root_path}#{model.table_name}/#{record.id}" do |f| %>
17-
<%= f.text_field attribute, class: "editable-input", data: {
18-
editable_attribute_target: "attributeFormInput",
19-
action: "keydown.enter->editable-attribute#submitForm blur->editable-attribute#submitForm"
20-
} %>
21-
<% end %>
22-
</div>
23-
</div>
4+
<%= form_for record, url: "#{root_path}#{model.table_name}/#{record.id}",
5+
html: { 'data-controller': "editable-attribute",
6+
'data-editable-attribute-broadcast-url-value': broadcast_edit_intent_path,
7+
'data-editable-attribute-resource-name-value': model.table_name,
8+
'data-editable-attribute-resource-id-value': record.id,
9+
'data-editable-attribute-resource-initial-value-value': record.public_send(attribute) } do |f| %>
10+
<%= f.text_field attribute, class: "editable-input", data: {
11+
editable_attribute_target: "attributeFormInput",
12+
action: "keydown.enter->editable-attribute#submitForm blur->editable-attribute#submitForm"
13+
} %>
14+
<% end %>
2415
<% end %>

lib/hotsheet/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Hotsheet
4-
VERSION = "0.1.0"
4+
VERSION = "0.1.1"
55
end

spec/system/editable_attributes_spec.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,20 @@
1717
visit "/hotsheet"
1818
click_link "Author"
1919

20-
find(".readonly-attribute", text: "Stephen").click
2120
fill_in "author_name", with: "King"
2221
end
2322

2423
it "updates attribute when pressing enter" do
2524
find_by_id("author_name").native.send_keys :return
2625

27-
expect(page).to have_content("King")
26+
expect(page).to have_field("author_name", with: "King")
2827
expect(author.reload.name).to eq("King")
2928
end
3029

3130
it "updates attribute when clicking outside the input" do
3231
find("th", text: "name").click
3332

34-
expect(page).to have_content("King")
33+
expect(page).to have_field("author_name", with: "King")
3534
expect(author.reload.name).to eq("King")
3635
end
3736
end

0 commit comments

Comments
 (0)