-
-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow minimum/maximum configuration #6
Comments
Hum that sounds cool but I have few questions. How to find the initial number? How to configure if you want to hide or disable the buttons? |
Ooo I like this.
😄 |
Just saw this issue, as I also needed to set a limit value. Here's my extension; feel free to take or leave it: import NestedForm from 'stimulus-rails-nested-form'
export default class extends NestedForm {
static targets = ['addButton', 'clickAfterAdd']
static values = {
limit: Number
}
connect () {
super.connect()
this.setVisibilityOfAddButton()
}
add (e) {
e.preventDefault()
const currentCount = this.nestedFormCount()
if (this.hasLimitValue && currentCount > this.limitValue) return
super.add(e)
this.setVisibilityOfAddButton()
if (!localStorage.isBeingControlledByCapybara && this.hasClickAfterAddTargets) {
this.clickAfterAddTargets[this.clickAfterAddTargets.length - 1].click()
}
this.element.dispatchEvent(new CustomEvent('itemsChanged', { bubbles: true }))
}
remove (e) {
// Since the file removal isn't committed until saved, I guess this is overkill
// if (!confirm("This will delete the image. Are you sure?")) return
super.remove(e)
this.setVisibilityOfAddButton()
this.element.dispatchEvent(new CustomEvent('itemsChanged', { bubbles: true }))
}
// private
setVisibilityOfAddButton () {
if (!this.hasAddButtonTarget || !this.hasLimitValue) return
const currentCount = this.nestedFormCount()
this.addButtonTarget.classList.toggle('hidden', currentCount > this.limitValue)
}
nestedFormCount () {
return this.nestedForms()
.filter(el => el.style.display !== 'none').length
}
nestedForms () {
return Array.from(this.element.querySelectorAll(this.wrapperSelectorValue))
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cocoon would allow developers to define a minimum and maximum number of nested forms, would be nice to do the same here without extending the controller.
i.e. minimum = 1, maximum = 4
If only one nested form is being displayed then the delete button is hidden or disabled
If 4 are being displayed then the add button is hidden or disabled
The text was updated successfully, but these errors were encountered: