Skip to content

Commit

Permalink
Skip underscore keys and support objects in bulk editing values
Browse files Browse the repository at this point in the history
  • Loading branch information
louwie17 committed Dec 3, 2024
1 parent c27bd65 commit 366883a
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions packages/dataviews/src/components/dataform/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,34 @@ import { MIXED_VALUE } from '../../constants';

/**
* Loops through the list of data items and returns an object with the intersecting ( same ) key and values.
* Skips keys that start with an underscore.
*
* @param data list of items.
*/
function getIntersectingValues< Item extends object >( data: Item[] ): Item {
const intersectingValues = {} as Item;
const keys = Object.keys( data[ 0 ] ) as Array< keyof Item >;
for ( const key of keys ) {
// Skip keys that start with underscore.
if ( key.toString().startsWith( '_' ) ) {
continue;
}
const [ firstRecord, ...remainingRecords ] = data;
const intersects = remainingRecords.every( ( item ) => {
return item[ key ] === firstRecord[ key ];
} );
if ( intersects ) {
intersectingValues[ key ] = firstRecord[ key ];

if ( typeof firstRecord[ key ] === 'object' ) {
// Traverse through nested objects.
intersectingValues[ key ] = getIntersectingValues(
data.map( ( item ) => item[ key ] as object )
) as Item[ keyof Item ];
} else {
intersectingValues[ key ] = MIXED_VALUE as Item[ keyof Item ];
const intersects = remainingRecords.every( ( item ) => {
return item[ key ] === firstRecord[ key ];
} );
if ( intersects ) {
intersectingValues[ key ] = firstRecord[ key ];
} else {
intersectingValues[ key ] = MIXED_VALUE as Item[ keyof Item ];
}
}
}
return intersectingValues;
Expand Down

0 comments on commit 366883a

Please sign in to comment.