final-form / final-form-arrays

Array Mutators for 🏁 Final Form
https://final-form.org/arrays
MIT License
70 stars 40 forks source link

Sort mutator #55

Closed il421 closed 2 years ago

il421 commented 4 years ago

Very often I need a sort mutator, for arrays, after any actions. For example, update a value, and sort after. I have added the method in my local project. And bind with arrayMutators. Works fine! May be I could contribute. I feel the method could be usefull

const sort: Mutator<any> = (
  [name, compareFn]: [string, (a: any, b: any) => number],
  state: MutableState<any>,
  { setIn }: Tools<any>
) => {
  const sortedArray = state.formState.values[name].sort(compareFn)
  setIn(state, 'formState.values' + name, sortedArray)
}

export default sort;
huan086 commented 4 years ago

This is not sufficient. The field state needs to be moved as well