pouya-eghbali / meteor-autoform

Meteor AutoForm fork, maintained, with tons of performance issues fixed
MIT License
9 stars 1 forks source link

Make list items sortable #4

Open jankapunkt opened 4 years ago

jankapunkt commented 4 years ago

I currently use a custom extension to make list entries sortable. However, I think it should not be that big issue to provide some [ :arrow_up: ] and [ :arrow_down: ] buttons to make list items sortable by incrementing / decrementing indices. This solution could also be implemented without any third party lib.

pouya-eghbali commented 4 years ago

I've added a AutoForm.setFieldValue(fieldName, value, formId) method. I'll investigate if it's possible to getFieldValue, swap elements, and then setFieldValue to achieve this. I also need this functionality, I've modified how arrays work in autoform, idk if it's better to use setFieldValue or to add some magic to the arrayTracker.

pouya-eghbali commented 4 years ago

Ok, getFieldValue doesn't work for Arrays, and setFieldValue doesn't work for Arrays and Objects. I'll fix these issues ASAP because I depend on them.

pouya-eghbali commented 4 years ago

https://github.com/pouya-eghbali/meteor-autoform/commit/c914c2be29e541bdb70fba6518cd74179edf1afc should fix getFieldValue

pouya-eghbali commented 4 years ago

https://github.com/pouya-eghbali/meteor-autoform/commit/6969d6d895c8874e5660bcd804f498b273243afa should fix setFieldValue

This works as long as there isn't any weird fields in the array! In my tests this works for swapping arrays of objects with selects and simple text inputs, but doesn't work with material time and date inputs. Needs more work.

pouya-eghbali commented 4 years ago

https://github.com/pouya-eghbali/meteor-autoform/commit/208d2dd35655cd4bf0b18cfb5032e66172e3afc8 Fixes everything, setFieldValue works perfectly, and array items are now swappable/sortable.

pouya-eghbali commented 4 years ago

@jankapunkt items can be swapped like this now:

const [item1, item2] = AutoForm.getFieldValue(fieldName, formId)
AutoForm.setFieldValue(fieldName, [item2, item1], formId)

This will make it a lot easier to provide [ :arrow_up: ] and [ :arrow_down: ] buttons. More fancy stuff are also possible now:

const items = AutoForm.getFieldValue(fieldName, formId)
const sortedItems = items.sort(mySortFunction)
AutoForm.setFieldValue(fieldName, sortedItems, formId)
jankapunkt commented 4 years ago

Do you think it makes sense then to let extensions implement this? I think it is now flexible enough to let developers imement their own version of up / down / sort. Wdyt?

jankapunkt commented 4 years ago

Sorry forget my previous comment, extensions can override it anyway so out-of-the-box up/down would still make most sense IMO.

pouya-eghbali commented 4 years ago

up/down in templates? or as an API method that extensions can use to implement the functionality?

jankapunkt commented 4 years ago

Hi. I think it makes sense to provide up down buttons out-of-the box in the default array template, don't you think? The API methods would still be required for this and then extensions could either ignore it or use it to implement e.g. drag and drop etc.

I can work towards a PR if you wan.

pouya-eghbali commented 4 years ago

In the default bootstrap and plain templates?

pouya-eghbali commented 4 years ago

@jankapunkt I redid the setFieldValue method, it had some issues. I think it works better now, but it's still not perfect.