modules_DataGrid_utils_index.js

/**
 * @function getColumnStyle
 * @description Get the column style.
 *
 * @since 4.3.0
 *
 * @param {object}  styleProps The style properties.
 * @param {boolean} equalGrid  Whether the grid is equal or not.
 *
 * @return {object} The column style.
 */
export const getColumnStyle = (
	styleProps = {},
	equalGrid = false
) => {
	const defaultStyle = { flex: '1 0px' };

	if ( ! styleProps || equalGrid ) {
		return defaultStyle;
	}

	// Filter style props to only allowed properties.
	const allowedProperties = [ 'flex', 'flexGrow', 'flexShrink', 'flexBasis' ];
	const filteredStyleProps = Object.keys( styleProps )
		.filter( ( key ) => allowedProperties.includes( key ) )
		.reduce( ( obj, key ) => {
			obj[ key ] = styleProps[ key ];
			return obj;
		}, {} );

	return Object.assign( defaultStyle, filteredStyleProps );
};

/**
 * @function getModules
 * @description Get the modules from module names provided
 *
 * @since 4.3.0
 *
 * @param {Array} modules     The modules to filter.
 * @param {Array} moduleNames The module names to filter.
 *
 * @return {object} The modules.
 */
export const getModules = ( modules = [], moduleNames = [] ) => {
	const initialObj = moduleNames.reduce( ( carry, moduleName ) => {
		carry[ moduleName ] = false;
		return carry;
	}, {} );

	return modules.reduce( ( carry, module ) => {
		if ( moduleNames.includes( module.name ) ) {
			return {
				...carry,
				[ module.name ]: module,
			};
		}

		return carry;
	}, initialObj );
};

/**
 * @function getRequestDataFromState
 * @description Get request data from state.
 *
 * @since TBD
 *
 * @todo: set since version.
 *
 * @param {object} state   The state object.
 * @param {Array}  modules The modules for the data grid.
 *
 * @return {object} The request data.
 */
// @todo: implement function when building filters.
// export const getRequestDataFromState = ( state = {}, modules = [] ) => {
// 	const { Filters } = getModules( modules, [ 'Filters' ] );
//
// 	return {
// 		...state,
// 		...( Filters?.getRequestDataFromState( state ) || {} ),
// 	};
// };