Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Drag n Drop Reordering to ResponsiveTable Component #1662

Merged
merged 23 commits into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
688834b
install react-beautiful-dnd
tn3rb Sep 30, 2019
4e4a2b8
useReorderEntities - hook for setting custom order of entity list
tn3rb Sep 30, 2019
2f81d0f
pass through entity ID list and setter for sorting list order
tn3rb Sep 30, 2019
781aa99
cache menuItems in state and set after render if empty
tn3rb Oct 1, 2019
27c4145
move logic into ticketsListTableRow where it's actually used
tn3rb Oct 1, 2019
fefddae
extra comment regarding order value
tn3rb Oct 1, 2019
3729137
add useReorderTickets and use in EditorTicketEntitiesListView
tn3rb Oct 1, 2019
68026c6
add useReorderDates and use in EditorDateEntitiesListView
tn3rb Oct 1, 2019
eb8fd2a
bunch of css tweaks
tn3rb Oct 1, 2019
1c31273
implement react-beautiful-dnd
tn3rb Oct 1, 2019
1922c9c
fix dependencies
tn3rb Oct 2, 2019
a0aec80
fix jsdocs
tn3rb Oct 2, 2019
3a6785e
pass model name so that entities can be verified
tn3rb Oct 3, 2019
3fcfc5e
move logic from ResponsiveTable into table subcomponents
tn3rb Oct 3, 2019
4f50aa5
fix table data schema; rename component
tn3rb Oct 3, 2019
891ddf2
update components and table data schema after ResponsiveTable changes
tn3rb Oct 4, 2019
ca4e96b
fix render cycling
tn3rb Oct 4, 2019
5f5e35e
fix property name after change
tn3rb Oct 4, 2019
ff57db2
pass model name to useReorderEntities
tn3rb Oct 4, 2019
fc55a0c
use filtered entity ID lists to set keys on components
tn3rb Oct 4, 2019
57339db
tweak date ID position js_only
tn3rb Oct 4, 2019
73b3ec7
change license to GPLv3 and add ASLv2 to green licenses js_only
tn3rb Oct 4, 2019
e39c2e7
change license details in plugin main file js_only
tn3rb Oct 4, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/src/components/form/base/form-placeholder.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

.ee-form-placeholder-div {
border: 1px dashed var(--ee-color-grey-11);
margin: var(--ee-margin-default) var(--ee-margin-tiny);
margin: var(--ee-margin-default) 0;
}

.ee-editor-div > .ee-form-placeholder-div {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@
margin: var(--ee-margin-small) 0 0;
}

.ee-two-column-admin .px-3 .ee-form-row:not(li) {
.ee-two-column-admin .ee-form-row:not(li) {
margin: 0 0 var(--ee-margin-small);
}
}
Expand Down Expand Up @@ -617,7 +617,7 @@
/* Anything bigger than Smartphone */
@media screen and (min-width: 481px) {

.ee-two-column-admin .px-3 .ee-form-row:not(li) {
.ee-two-column-admin .ee-form-row:not(li) {
margin: 0 0 var(--ee-margin-small);
}

Expand Down Expand Up @@ -825,7 +825,7 @@
}

.ee-two-column-admin .ee-form-row,
.ee-two-column-admin .px-3 .ee-form-row:not(li) {
.ee-two-column-admin .ee-form-row:not(li) {
flex-flow: column nowrap;
justify-content: center;
min-height: unset;
Expand Down
3 changes: 3 additions & 0 deletions assets/src/components/ui/drop-down-menu/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
background: none !important;
border: 1px solid var(--ee-background-color) !important;
color: var(--ee-default-text-color) !important;
height: calc(var(--ee-icon-button-size) + 2px);
margin: 0;
padding: var(--ee-padding-tiny);
width: calc(var(--ee-icon-button-size) + 2px);
}

.ee-drop-down-menu.components-dropdown-menu .components-dropdown-menu__toggle:hover,
Expand Down
1 change: 1 addition & 0 deletions assets/src/components/ui/entity-list/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as EntityList } from './entity-list';
export { default as useReorderEntities } from './use-reorder-entities';
113 changes: 113 additions & 0 deletions assets/src/components/ui/entity-list/use-reorder-entities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* External imports
*/
import { sortBy } from 'lodash';
import { useCallback } from '@wordpress/element';
import { isModelEntityOfModel } from '@eventespresso/validators';

/**
* returns a custom hook
* for changing the order of the provided array of entities,
* given indexes for an entity's existing location in
* that array and the desired new location.
* Because the provided array of entities may NOT be
* the full set of entities, the full list must also
* be supplied and it will be reorder as well,
* with the entities from the subset array moved
* to the beginning of the full set.
*
* @function
* @param {string} modelName
* @param {Function} setSortBy
* @param {Function} setEntityIds
* @return {Function} custom hook
*/
const useReorderEntities = ( { modelName, setSortBy, setEntityIds } ) => {
/**
* @function
* @param {Array} entities a subset of filtered entities
* @param {Array} allEntities a list of ALL entities of the same type
* that may not have been present in the subset list that was sorted. It
* will be reorder as well.
* @param {number|string} oldIndex existing location of entity to be
* moved
* @param {number|string} newIndex target location for entity in subset
* array
*/
return useCallback( (
entities,
allEntities,
oldIndex,
newIndex,
) => {
oldIndex = parseInt( oldIndex, 10 );
newIndex = parseInt( newIndex, 10 );
if ( newIndex === oldIndex ) {
return;
}
if ( newIndex < 0 || oldIndex < 0 ) {
throw new Error(
'Can not reorder the entity list because' +
' indexes can not be negative!' +
"\n oldIndex: " + JSON.stringify( oldIndex ) +
"\n newIndex: " + JSON.stringify( newIndex )
);
}
if ( ! Array.isArray( entities ) ||
! Array.isArray( allEntities ) ) {
throw new Error(
'Can not reorder the entity list because one or more of the' +
' supplied entity lists were invalid!' +
"\n entities: " + JSON.stringify( entities ) +
"\n allEntities: " + JSON.stringify( allEntities )
);
}
// remove entity from existing location in filtered list
const [ removed ] = entities.splice( oldIndex, 1 );
// insert removed entity into new location in same list
entities.splice( newIndex, 0, removed );
// now loop thru entities in filtered list
entities.forEach( ( entity, index ) => {
if ( isModelEntityOfModel( entity, modelName ) ) {
// reset the order property for all entities in filtered list
entity.order = index + 1;
// grab index of reordered entities in list of all entities
const indexInAll = allEntities.indexOf( entity );
// remove reordered entities from list of all entities
allEntities.splice( indexInAll, 1 );
} else {
throw new Error(
'Can not reorder the entity list because' +
' an invalid entity was supplied!' +
"\n entity: " + JSON.stringify( entity )
);
}
} );
// reorder the list of all entities as well...
// reverse the reordered list of entities
const reversed = entities.reverse();
reversed.forEach( ( entity ) => {
// add each entity to the beginning of the allEntities array
allEntities.unshift( entity );
// so we previously removed these entities, but now we are added
// them back onto the array at the beginning and in reverse
// order. So we add #3 to the top, then #2, then #1,
// so that the final order of the array will be #1, #2, #3,
// followed by all of the other entities previously in the array
} );
// but now we need to reset the order properties for ALL entities
allEntities.forEach( ( entity, index ) => {
// add 1 so we don't end up with order: 0
entity.order = index + 1;
} );
allEntities = sortBy( allEntities, [ 'order' ] );
setEntityIds( allEntities.map( ( entity ) => entity.id ) );
setSortBy( 'by-order' );
}, [
modelName,
setSortBy,
setEntityIds,
] );
};

export default useReorderEntities;
4 changes: 2 additions & 2 deletions assets/src/components/ui/responsive-table/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import TableHeader from './table-header';
import TableBody from './table-body';
import TableFooter from './table-footer';
import TableRow from './table-row';
import TableHeadingCell from './table-heading-cell';
import TableHeaderCell from './table-header-cell';
import TableDataCell from './table-data-cell';

export const EspressoTable = {
Expand All @@ -12,7 +12,7 @@ export const EspressoTable = {
TableBody,
TableRow,
TableFooter,
TableHeadingCell,
TableHeaderCell,
TableDataCell,
};

Expand Down
Loading