mirror of
https://github.com/kingomarnajjar/flex-template-web.git
synced 2026-07-29 05:00:43 +10:00
Move redux form value functions to util/data
This commit is contained in:
parent
8d444b310a
commit
39bd5866b9
3 changed files with 76 additions and 25 deletions
|
|
@ -2,34 +2,13 @@ import React, { Component } from 'react';
|
|||
import { array, arrayOf, func, number, string } from 'prop-types';
|
||||
import classNames from 'classnames';
|
||||
import { injectIntl, intlShape } from 'react-intl';
|
||||
import { toPairs } from 'lodash';
|
||||
|
||||
import { arrayToFormValues, formValuesToArray } from '../../util/data';
|
||||
import SelectMultipleFilterForm from './SelectMultipleFilterForm';
|
||||
import css from './SelectMultipleFilter.css';
|
||||
|
||||
const KEY_CODE_ESCAPE = 27;
|
||||
|
||||
/**
|
||||
* Convert an array of option keys into
|
||||
* an object that can be passed to a redux
|
||||
* form as initial values.
|
||||
*/
|
||||
const keysToValues = keys => {
|
||||
return keys.reduce((map, key) => {
|
||||
map[key] = true;
|
||||
return map;
|
||||
}, {});
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert an object containing values received
|
||||
* from a redux form into an array of values.
|
||||
*/
|
||||
const valuesToKeys = values => {
|
||||
const entries = toPairs(values);
|
||||
return entries.filter(entry => entry[1] === true).map(entry => entry[0]);
|
||||
};
|
||||
|
||||
class SelectMultipleFilter extends Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
|
|
@ -49,7 +28,7 @@ class SelectMultipleFilter extends Component {
|
|||
|
||||
handleSubmit(values) {
|
||||
const { name, onSelect, urlParam } = this.props;
|
||||
const selectedKeys = valuesToKeys(values[name]);
|
||||
const selectedKeys = formValuesToArray(values[name]);
|
||||
this.setState({ isOpen: false });
|
||||
onSelect(urlParam, selectedKeys);
|
||||
}
|
||||
|
|
@ -129,7 +108,7 @@ class SelectMultipleFilter extends Component {
|
|||
|
||||
// turn a list of values into a map that can be passed to
|
||||
// a redux form
|
||||
const initialValuesObj = keysToValues(initialValues);
|
||||
const initialValuesObj = arrayToFormValues(initialValues);
|
||||
|
||||
// pass the initial values with the name key so that
|
||||
// they can be passed to the correct field
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { isArray, reduce } from 'lodash';
|
||||
import { isArray, reduce, toPairs } from 'lodash';
|
||||
|
||||
/**
|
||||
* Combine the given relationships objects
|
||||
|
|
@ -281,3 +281,39 @@ export const overrideArrays = (objValue, srcValue, key, object, source, stack) =
|
|||
return srcValue;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Converts an array of strings into an object where the array items
|
||||
* are keys and values in all fields are true. This kind of object
|
||||
* can be passed as initialValues parameter to a ReduxForm that contains
|
||||
* checkbox inputs.
|
||||
*
|
||||
* @param {Array} array An array of strings
|
||||
*
|
||||
* @return {Object} An object containing the array items as keys and all
|
||||
* the values set to true
|
||||
*
|
||||
* A complementary function to formValuesToArray.
|
||||
*
|
||||
*/
|
||||
export const arrayToFormValues = array => {
|
||||
return array.reduce((map, key) => {
|
||||
map[key] = true;
|
||||
return map;
|
||||
}, {});
|
||||
};
|
||||
|
||||
/**
|
||||
* Converts a values object received form a Redux Form containing
|
||||
* checkboxes into an array that contains only the values.
|
||||
*
|
||||
* @param {Object} formValues A values object received from a Redux Form
|
||||
*
|
||||
* @return {Array} An array containing the keys of the formValues parameter
|
||||
*
|
||||
* A complementary function to arrayToFormValues.
|
||||
*/
|
||||
export const formValuesToArray = formValues => {
|
||||
const entries = toPairs(formValues);
|
||||
return entries.filter(entry => entry[1] === true).map(entry => entry[0]);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import {
|
|||
combinedResourceObjects,
|
||||
updatedEntities,
|
||||
denormalisedEntities,
|
||||
arrayToFormValues,
|
||||
formValuesToArray,
|
||||
} from './data';
|
||||
|
||||
const { UUID } = sdkTypes;
|
||||
|
|
@ -326,4 +328,38 @@ describe('data utils', () => {
|
|||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('arrayToFormValues()', () => {
|
||||
it('converts an empty array', () => {
|
||||
expect(arrayToFormValues([])).toEqual({});
|
||||
});
|
||||
it('converts multiple values', () => {
|
||||
const array = ['one', 'two', 'three'];
|
||||
const formValues = { one: true, two: true, three: true };
|
||||
expect(arrayToFormValues(array)).toEqual(formValues);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formValuesToArray()', () => {
|
||||
it('converts an empty object', () => {
|
||||
expect(formValuesToArray({})).toEqual([]);
|
||||
});
|
||||
it('converts multiple values', () => {
|
||||
const formValues = { one: true, two: true, three: true };
|
||||
const array = ['one', 'two', 'three'];
|
||||
expect(formValuesToArray(formValues)).toEqual(array);
|
||||
});
|
||||
it('it skips non-true values form input object', () => {
|
||||
const formValues = {
|
||||
one: true,
|
||||
two: null,
|
||||
three: 'true',
|
||||
four: false,
|
||||
five: '',
|
||||
six: true,
|
||||
};
|
||||
const array = ['one', 'six'];
|
||||
expect(formValuesToArray(formValues)).toEqual(array);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue