Skip to content

Commit e9f4a19

Browse files
authored
Merge pull request #13 from Query-Builder/show-branches
feat: add ability to show branches via css and it's classes
2 parents 42417e6 + 008571d commit e9f4a19

File tree

7 files changed

+143
-43
lines changed

7 files changed

+143
-43
lines changed

dev/App.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const App: Component = () => {
2323
const [packageVersion] = createResource(fetchPackageVersion);
2424

2525
const [disableQB, setDisableQB] = createSignal(false);
26+
const [showBranches, setShowBranches] = createSignal(true);
2627
const [showShiftActions, setShowShiftActions] = createSignal(true);
2728
const [allowDragAndDrop, setAllowDragAndDrop] = createSignal(true);
2829
const [addSingleRuleToGroup, setAddSingleRuleToGroup] = createSignal(true);
@@ -103,6 +104,15 @@ const App: Component = () => {
103104
/>
104105
Disable Query Builder
105106
</label>
107+
<label class="label">
108+
<input
109+
name="toggle-branches"
110+
type="checkbox"
111+
checked={showBranches()}
112+
onChange={() => setShowBranches(!showBranches())}
113+
/>
114+
Show Branches
115+
</label>
106116
<label class="label">
107117
<input
108118
name="toggle-shift-actions"
@@ -156,6 +166,7 @@ const App: Component = () => {
156166
disabled={disableQB()}
157167
addSingleRuleToGroup={addSingleRuleToGroup()}
158168
showNotToggle={showNotToggle()}
169+
showBranches={showBranches()}
159170
/>
160171
</section>
161172
</section>

dev/styles.css

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,9 @@ html[data-theme='dark'] .github-logo {
8282
}
8383

8484
.options {
85-
display: flex;
86-
flex-direction: row;
87-
align-items: center;
88-
column-gap: 10px;
85+
gap: 10px;
86+
display: grid;
87+
grid-template-columns: repeat(3, 33%);
8988
}
9089

9190
.options label {
@@ -238,7 +237,7 @@ select {
238237
align-items: center;
239238
column-gap: 10px;
240239
height: 40px;
241-
margin-bottom: 15px;
240+
margin-bottom: 20px;
242241
}
243242

244243
.query-builder select[name='operators'] {
@@ -247,9 +246,11 @@ select {
247246

248247
.query-builder .shift-actions {
249248
height: 40px;
249+
margin-top: -8px;
250250
}
251251

252252
.query-builder .shift-actions button {
253+
height: 25px;
253254
background-color: transparent;
254255
padding: 0;
255256
}
@@ -258,16 +259,12 @@ html[data-theme='light'] .shift-actions button {
258259
color: black;
259260
}
260261

261-
html[data-theme='dark'] .query-builder .rule-group {
262-
border-color: rgb(188 194 206);
263-
}
264-
265-
html[data-theme='dark'] .query-builder .rule {
266-
border-color: rgb(188 194 206 / 10%);
262+
.query-builder .rule-group {
263+
border-color: var(--pico-border-color);
267264
}
268265

269-
html[data-theme='light'] .query-builder .rule {
270-
border-color: rgb(0 0 0 / 10%);
266+
.query-builder .rule {
267+
border-color: var(--pico-muted-border-color);
271268
}
272269

273270
.query-builder .rule-group {
@@ -283,3 +280,19 @@ html[data-theme='light'] .query-builder .rule {
283280
}
284281

285282
/* Query Builder Styles ends here... */
283+
284+
.query-builder.query-builder-branches .rule::before {
285+
border-color: var(--pico-primary);
286+
}
287+
288+
.query-builder.query-builder-branches .rule::after {
289+
border-color: var(--pico-primary);
290+
}
291+
292+
.query-builder.query-builder-branches .rule-group::before {
293+
border-color: var(--pico-primary);
294+
}
295+
296+
.query-builder.query-builder-branches .rule-group::after {
297+
border-color: var(--pico-primary);
298+
}

src/components/QueryBuilderBase.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import {
22
DragDropProvider,
33
DragDropSensors,
44
type DragEvent,
5+
DragOverlay,
56
closestCorners,
7+
useDragDropContext,
68
} from '@thisbeyond/solid-dnd';
79

810
import { useQueryBuilderContext } from 'src/context/QueryBuilderContext';
@@ -11,9 +13,19 @@ import { ConstraintDrag } from './ConstraintDrag';
1113
import { RuleGroup } from './RuleGroup';
1214
import { useQueryBuilderDNDContext } from 'src/context';
1315

16+
const DragOverlayWrapper = () => {
17+
const [{ active }] = useDragDropContext()!;
18+
return (
19+
<DragOverlay>
20+
<div class="drag-overlay">{active.draggable?.data.rule?.field}</div>
21+
</DragOverlay>
22+
);
23+
};
24+
1425
export const QueryBuilderBase = () => {
15-
const { store: query, dispatch } = useQueryBuilderContext();
26+
const { store: query, dispatch, config } = useQueryBuilderContext();
1627
const { dndConfig, setDropPosition } = useQueryBuilderDNDContext();
28+
1729
let queryBuilderRef: HTMLDivElement;
1830

1931
const onDragEndEventHandler = (event: DragEvent) => {
@@ -39,7 +51,12 @@ export const QueryBuilderBase = () => {
3951
};
4052

4153
return (
42-
<div class="query-builder" ref={queryBuilderRef!}>
54+
<div
55+
class={['query-builder', config().showBranches ? 'query-builder-branches' : '']
56+
.filter(Boolean)
57+
.join(' ')}
58+
ref={queryBuilderRef!}
59+
>
4360
<DragDropProvider collisionDetector={closestCorners} onDragEnd={onDragEndEventHandler}>
4461
<DragDropSensors />
4562
<ConstraintDrag queryBuilderRef={queryBuilderRef!} />
@@ -50,6 +67,7 @@ export const QueryBuilderBase = () => {
5067
shiftUpDisabled={true}
5168
shiftDownDisabled={true}
5269
/>
70+
<DragOverlayWrapper />
5371
</DragDropProvider>
5472
</div>
5573
);

src/components/Rule.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Show, For, createEffect, createSignal, onCleanup, onMount } from 'solid
22
import {
33
createDraggable,
44
createDroppable,
5-
DragOverlay,
65
transformStyle,
76
useDragDropContext,
87
} from '@thisbeyond/solid-dnd';
@@ -271,9 +270,6 @@ export const Rule = (props: RuleProps) => {
271270
>
272271
Delete
273272
</button>
274-
<DragOverlay>
275-
<div class="drag-overlay">{currentFieldData()?.name}</div>
276-
</DragOverlay>
277273
</div>
278274
);
279275
};

src/context/QueryBuilderContext.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Config = WithRequired<
2121
| 'showShiftActions'
2222
| 'allowDragAndDrop'
2323
| 'addSingleRuleToGroup'
24+
| 'showBranches'
2425
>;
2526

2627
type QueryBuilderContext = {
@@ -45,6 +46,7 @@ type QueryBuilderProviderProps = Pick<
4546
| 'showShiftActions'
4647
| 'allowDragAndDrop'
4748
| 'addSingleRuleToGroup'
49+
| 'showBranches'
4850
> & {
4951
children: JSX.Element;
5052
};
@@ -72,6 +74,7 @@ export const QueryBuilderProvider = (props: QueryBuilderProviderProps) => {
7274
showShiftActions: false,
7375
allowDragAndDrop: false,
7476
addSingleRuleToGroup: false,
77+
showBranches: false,
7578
},
7679
props,
7780
);
@@ -93,6 +96,7 @@ export const QueryBuilderProvider = (props: QueryBuilderProviderProps) => {
9396
showShiftActions: mergedProps.showShiftActions,
9497
allowDragAndDrop: mergedProps.allowDragAndDrop,
9598
addSingleRuleToGroup: mergedProps.addSingleRuleToGroup,
99+
showBranches: mergedProps.showBranches,
96100
};
97101
};
98102

src/styles.css

Lines changed: 80 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
flex-direction: row;
2424
column-gap: 5px;
2525
align-items: center;
26-
margin-bottom: 10px;
26+
margin-bottom: 20px;
2727
}
2828

2929
.rule-group__body {
@@ -56,40 +56,96 @@
5656
border-radius: 5px;
5757
}
5858

59-
.rule.rule-drop-top {
60-
border: 1px dashed blue;
59+
.query-builder .rule.rule-drop-top {
60+
border-top: 3px solid red;
6161
}
6262

63-
.rule.rule-drop-bottom {
64-
border: 1px dashed blue;
63+
.query-builder .rule.rule-drop-bottom {
64+
border-bottom: 3px solid red;
6565
}
6666

67-
.rule.rule-drop-top::before {
67+
.drag-overlay {
68+
border: 1px solid black;
69+
width: max-content;
70+
padding: 10px 20px;
71+
border-radius: 5px;
72+
background-color: #e1e2e2;
73+
opacity: 0.8;
74+
z-index: 999;
75+
}
76+
77+
/** branches */
78+
79+
.query-builder.query-builder-branches .rule,
80+
.query-builder.query-builder-branches .rule-group {
81+
position: relative;
82+
}
83+
84+
.query-builder.query-builder-branches .rule-group[data-level='0']::before,
85+
.query-builder.query-builder-branches .rule-group[data-level='0']::after {
86+
display: none;
87+
}
88+
89+
.query-builder.query-builder-branches .rule::before {
6890
content: '';
6991
position: absolute;
70-
border: 1px dashed red;
71-
top: 0%;
72-
left: 0%;
73-
width: 100%;
74-
transform: translate(0, -5px);
92+
border: 1px solid black;
93+
top: -20px;
94+
left: -20px;
95+
width: 15px;
96+
border-width: 0 0 1px 1px;
97+
height: calc(50% + 20px);
98+
border-radius: 0;
7599
}
76100

77-
.rule.rule-drop-bottom::after {
101+
.query-builder.query-builder-branches .rule::after {
78102
content: '';
79103
position: absolute;
80-
border: 1px dashed red;
81-
bottom: 0%;
82-
left: 0%;
83-
width: 100%;
84-
transform: translate(0, 5px);
104+
border: 1px solid black;
105+
top: 50%;
106+
left: -20px;
107+
width: 15px;
108+
border-width: 0 0 0 1px;
109+
height: 54%;
110+
border-radius: 0;
85111
}
86112

87-
.drag-overlay {
113+
.query-builder.query-builder-branches .rule-group::before {
114+
content: '';
115+
position: absolute;
88116
border: 1px solid black;
89-
width: max-content;
90-
padding: 3px;
91-
border-radius: 5px;
92-
background-color: #e1e2e2;
93-
opacity: 0.5;
94-
z-index: 2;
117+
top: -20px;
118+
left: -20px;
119+
width: 15px;
120+
border-width: 0 0 1px 1px;
121+
height: calc(50% + 20px);
122+
border-radius: 0;
123+
}
124+
125+
.query-builder.query-builder-branches .rule-group::after {
126+
content: '';
127+
position: absolute;
128+
border: 1px solid black;
129+
top: 50%;
130+
left: -20px;
131+
width: 15px;
132+
border-width: 0 0 0 1px;
133+
height: 54%;
134+
border-radius: 0;
135+
}
136+
137+
.query-builder.query-builder-branches .rule:last-child:before,
138+
.query-builder.query-builder-branches .rule-group:last-child:before {
139+
border-bottom-left-radius: 0.25rem;
140+
}
141+
142+
.query-builder.query-builder-branches .rule:last-child:after,
143+
.query-builder.query-builder-branches .rule-group:last-child:after {
144+
display: none;
145+
}
146+
147+
.query-builder.query-builder-branches .rule-group__body {
148+
margin-left: 20px;
95149
}
150+
151+
/** branches */

src/types/query.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ export type QueryBuilderConfig = {
4343
allowDragAndDrop?: boolean;
4444
/** If provided, it will allow add an single rule to new group, else it will be an empty group */
4545
addSingleRuleToGroup?: boolean;
46+
/** If provided, it will show default styled branches */
47+
showBranches?: boolean;
4648
};

0 commit comments

Comments
 (0)