Skip to content

Commit

Permalink
Table cells are always inputs (#39)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ignaciosy authored Feb 3, 2025
1 parent 4a480b4 commit 06a12d0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 46 deletions.
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
## [Unreleased](https://github.com/renuo/hotsheet/compare/v0.1.0..HEAD)
<!-- ## [Unreleased](https://github.com/renuo/hotsheet/compare/v0.1.0..HEAD) -->

- Configure compatible Ruby/Rails versions for testing
- Improve flash messages layout
## [0.1.1](https://github.com/renuo/hotsheet/releases/tag/v0.1.1) (2025-02-03)

- Improve configuration file usage and logic ([@simon-isler])
- Configure compatible Ruby/Rails versions for testing ([@ignaciosy])
- Improve flash messages layout ([@ignaciosy])
- Form inputs are now always visible for usage simplicity ([@ignaciosy])

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@ export default class extends Controller {
broadcastUrl: String,
resourceName: String,
resourceId: Number,
resourceInitialValue: String
}

static targets = ["readonlyAttribute", "attributeForm", "attributeFormInput"]
static targets = ["attributeFormInput"]

displayInputField() {
// this.broadcastEditIntent()
this.readonlyAttributeTarget.style.display = "none"
this.attributeFormTarget.style.display = "block"
this.attributeFormInputTarget.focus()
}

broadcastEditIntent() {
broadcastEditIntent() { // TODO: trigger on input focus
const headers = {
"Content-Type": "application/json",
"X-CSRF-Token": document.querySelector("meta[name=csrf-token]").content,
Expand All @@ -35,14 +29,8 @@ export default class extends Controller {
// Prevent standard submission triggered by Enter press
event.preventDefault()

const previousValue = this.readonlyAttributeTarget.innerText.trim()
const newValue = this.attributeFormInputTarget.value

if (previousValue && previousValue === newValue) {
this.readonlyAttributeTarget.style.display = "block"
this.attributeFormTarget.style.display = "none"
return
}
if (this.resourceInitialValueValue === newValue) return;

// It's important to use requestSubmit() instead of simply submit() as the latter will circumvent the
// Turbo mechanism, causing the PATCH request to be submitted as HTML instead of TURBO_STREAM
Expand Down
11 changes: 8 additions & 3 deletions app/assets/stylesheets/hotsheet/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
--main-padding-x: 2rem;
--main-padding-y: 2rem;

--body-font: 400 16px system-ui, Roboto, Helvetica, Arial, sans-serif;
--bg-color: lightgray;
--success-color: green;
--alert-color: yellow;
--notice-color: blue;
--error-color: red;

--td-padding: 0.5rem;
}

body {
font: 400 16px system-ui, Roboto, Helvetica, Arial, sans-serif;
font: var(--body-font);
margin: 0;
}

Expand Down Expand Up @@ -72,12 +75,12 @@ table {
th,
td {
border: 1px solid var(--bg-color);
padding: 0.5rem;
text-align: left;
}

th {
background-color: var(--bg-color);
padding: var(--td-padding);
}

.readonly-attribute {
Expand All @@ -88,7 +91,9 @@ table {
.editable-input {
background-color: transparent;
border: none;
width: 100%;
font: var(--body-font);
padding: var(--td-padding);
width: calc(100% - var(--td-padding) * 2);
}
}

Expand Down
31 changes: 11 additions & 20 deletions app/views/hotsheet/pages/_editable_attribute.html.erb
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
<%# locals: (attribute:, model:, record:) %>

<%= turbo_frame_tag "#{dom_id record}-#{attribute}" do %>
<div data-controller="editable-attribute"
data-editable-attribute-broadcast-url-value="<%= broadcast_edit_intent_path %>"
data-editable-attribute-resource-name-value="<%= model.table_name %>"
data-editable-attribute-resource-id-value="<%= record.id %>">
<div class="readonly-attribute"
data-editable-attribute-target="readonlyAttribute"
data-action="click->editable-attribute#displayInputField"
role="button">
<%= record.public_send attribute %>
</div>

<div data-editable-attribute-target="attributeForm" style="display:none">
<%= form_for record, url: "#{root_path}#{model.table_name}/#{record.id}" do |f| %>
<%= f.text_field attribute, class: "editable-input", data: {
editable_attribute_target: "attributeFormInput",
action: "keydown.enter->editable-attribute#submitForm blur->editable-attribute#submitForm"
} %>
<% end %>
</div>
</div>
<%= form_for record, url: "#{root_path}#{model.table_name}/#{record.id}",
html: { 'data-controller': "editable-attribute",
'data-editable-attribute-broadcast-url-value': broadcast_edit_intent_path,
'data-editable-attribute-resource-name-value': model.table_name,
'data-editable-attribute-resource-id-value': record.id,
'data-editable-attribute-resource-initial-value-value': record.public_send(attribute) } do |f| %>
<%= f.text_field attribute, class: "editable-input", data: {
editable_attribute_target: "attributeFormInput",
action: "keydown.enter->editable-attribute#submitForm blur->editable-attribute#submitForm"
} %>
<% end %>
<% end %>
2 changes: 1 addition & 1 deletion lib/hotsheet/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Hotsheet
VERSION = "0.1.0"
VERSION = "0.1.1"
end
5 changes: 2 additions & 3 deletions spec/system/editable_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,20 @@
visit "/hotsheet"
click_link "Author"

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

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

expect(page).to have_content("King")
expect(page).to have_field("author_name", with: "King")
expect(author.reload.name).to eq("King")
end

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

expect(page).to have_content("King")
expect(page).to have_field("author_name", with: "King")
expect(author.reload.name).to eq("King")
end
end
Expand Down

0 comments on commit 06a12d0

Please sign in to comment.