-
Notifications
You must be signed in to change notification settings - Fork 920
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
Add CrudField JS library, for interacting with fields inside a CRUD form #4312
Conversation
TODOs to move this forward:
CRUD::field([
'name' => 'extra_cost_value',
'type' => 'number',
'showWhen' => [ 'agreed', 'isChecked', 'AND', 'start_date', '>=', 'end_date', 'AND', 'end_date', '<=', date('Y-m-d') ],
'hideWhen' => [ 'agreed', 'isUnchecked', 'OR', 'start_date', '<', 'end_date', 'OR', 'end_date', '>', date('Y-m-d') ],
]); |
Can't wait for this feature release. Please do your magic soon. |
Thank you @margarizaldi ! @pxpm please take a look and give me your take on it. Let's move this forward. |
I think I know the answer but... as it is not related to "pro" and this is a basic feature can we hope a v4 backport? Thanks |
Nope... Backpack v4 will only receive security updates in the future, no extra features. See version support here. We must focus on making v5 the best we can. |
Final reason to make me leave BackPack adventure with 🫤 emoji ! |
Hey @rroblik sorry to hear that. Like @tabacitu said, we cannot focus on v5 roadmap if we kept pushing stuff into v4, but let me just add that there is nothing preventing you from using this in your v4 project, the code is here, under "Files", so you can just copy it to your project and use it. Please don't think I am arguing, I am 100% in favor of finding a better suite for my needs if the current one does not suffice them and I hope you find yours. Cheers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…ry-input return value from jquery input
add support for js api in checklist field
…events-in-text-and-textarea-fields
… into crud-form-javascript-library
…in-wrapper-search
Co-authored-by: António Almeida <[email protected]>
…avel-Backpack/CRUD into crud-form-javascript-library
…-wrapper-search don't use global * search operation, restrict the searching
…avel-Backpack/CRUD into crud-form-javascript-library
Co-authored-by: Pedro Martins <[email protected]>
* too, by exposing the main components (name, wrapper, input). | ||
*/ | ||
class CrudField { | ||
constructor(name) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just double-checked the selection logic and it still sounds good to me! Good job here @pxpm ! 👏👏👏
@promatik could you please also give it a look too? Maybe you spot red flags I missed, like you did with looking up through ancestors. Look at constructor()
but also at inputWrapper
and mainInput
please.
enable(value = true) { | ||
this.$input.attr('disabled', !value && 'disabled'); | ||
this.$input.trigger(`backpack:field.${value ? 'enable' : 'disable'}`); | ||
return this; | ||
} | ||
|
||
disable(value = true) { | ||
return this.enable(!value); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have disable()
and enable()
, but now I'm wondering... In most cases, isn't it readonly
that people need, not disabled
? What do you guys think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure it's more used than disabled, but definitely it's widely used and we should probably support it.
Update: I've decided NOT to use a convention, to automatically load JS files from a path. Yes, it's one extra step, but:
For those reasons, I've decided that, for now, we won't be automatically loading any JS files onpage. We can change our minds upon feedback, of course. It's easy to add later. But for now... no, let's not do that. Another update: We're SUPER FREAKING CLOSE to launching! 🎉🎉🎉 Everything's working properly, we have docs, we have... EVERYTHING. Running the final checks before we launch 🥳 |
A really good news ;) |
Of course. We don't make it any easier, but because of the simple architecture, because this is pure JS / jQuery that you write inside the closures or JS files... you can do AJAX calls, intercept AJAX calls, whatever you want 🎉 |
WHY
BEFORE - What was wrong? What was happening before this PR?
When you wanted to show/hide/toggle/change fields with JS, you had to write quite a bit of JS / jQuery code. For each field type... you'd have to think about the proper selector, try things out. Then when you manipulate it, you don't know where the info is stored (is it inside the main input/select/textarea? inside a hidden input? etc) and you don't know WHAT you can do to it (eg. checkbox and radio behave differently, because that's how the HTML spec works).
AFTER - What is happening after this PR?
Doing the most common things with JS in a form is a lot easier, because Backpack provides:
So.. no matter what field you're trying to select or to target, you can do
crud.field('price')
and you'll get back an object with the things you need most of times:name
,wrapper
,input
,value
onChange()
- also aliasedchange()
hide()
,show()
,enable()
,disable()
,require()
,unrequire()
,check()
,uncheck()
With this nifty little library, to show/hide one field depending on another, you'd now just need to:
Step 1. Create a script with whatever you want to do (eg. in
public/assets/js/admin/forms/product.js
):Step 2. Load that script inside the Create/Update form, by doing this in your
setupCreateOperation()
orsetupUpdateOperation()
:Optionally, we could eliminate the second step, using a naming convention. Backpack can check if a JS file exists that follows the naming convention - if yes, it could load it automatically. Not sure it's worth doing, though...
This small library has been specifically to cover all scenarios identified as "most common" in here: #4158 (comment) - and it does. You'll see in the examples below... they're all covered. Here's a video of me showing them off too:
Chromium.-.Add.product.__.Backpack.Admin.Panel.-.5.April.2022.mp4
HOW
How did you achieve that, in technical terms?
Created a small JS library for it. Just like we have
crud.table
on the ListOperation, now you can work withcrud.field()
andcrud.fields()
inside the Create and Update operations.Is it a breaking change?
Non-breaking. But the syntax and functionality might not be 100% final - that's up for debate.
How can we test the before & after?
Step 1. Use this branch:
Step 2. Inside a CrudController (for example in ProductCrudController in the demo), add the following to your
setupCreateOperation()
orsetupUpdateOperation()
to add the fields for the 10 most common examples: https://gist.github.com/tabacitu/248dd59da9b33debc26cb7496f205bb5#file-productcrudcontroller-phpStep 3. Create that
public/assets/js/product-form.js
file, and place the associated JS code for those 10 examples inside it: https://gist.github.com/tabacitu/248dd59da9b33debc26cb7496f205bb5#file-product-form-js