Skip to content

Commit

Permalink
Fix hierarchical subject select
Browse files Browse the repository at this point in the history
  • Loading branch information
arackaf committed Dec 7, 2022
1 parent cf9de3d commit a3678e3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
14 changes: 7 additions & 7 deletions svelte-kit/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion svelte-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
"classnames": "^2.3.2",
"mongodb": "^4.12.1",
"sass": "^1.56.1",
"svelte-helpers": "^0.2.10"
"svelte-helpers": "^0.2.12"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,23 @@
export let inputProps = {};
export let search = "";
export let onItemSelected: (option: any, inputEl: HTMLInputElement) => void;
export let noFiltering = false;
let inputStyles = "width: 100px; border-top-width: 0; border-right-width: 0; border-left-width: 0; border-radius: 0;";
</script>

<div>
<AutoSuggest {options} {inputProps} {placeholder} {onItemSelected} {inputStyles} filterField="name" bind:currentSearch={search}>
<AutoSuggest
keyField="_id"
{options}
{inputProps}
{placeholder}
{onItemSelected}
{inputStyles}
{noFiltering}
filterField="name"
bind:currentSearch={search}
>
<span slot="result" let:option>
<GenericLabelDisplayItem item={option} />
</span>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
<script lang="ts">
import type { Subject } from "$data/types";
import type { Label } from "../types";
import { filterSubjects, subjectState } from "$lib/state/subjectsState";
//import { stackedSubjects, filterSubjects, subjectsState } from "app/state/subjectsState";
import GenericLabelSelect from "../GenericLabelSelect.svelte";
import type { Label } from "../types";
export let onSelect: (item: Label) => void;
export let placeholder = "Subjects";
export let currentlySelected: string[] = [];
export let subjects: Subject[];
console.log(subjects);
//$: ({ subjectsUnwound } = $stackedSubjects);
//$: ({ subjectHash } = $subjectsState);
let search = "";
const doSelect = (item: Label) => {
Expand All @@ -32,4 +26,4 @@
$: eligible = filterSubjects(subjectsPacket.subjectsUnwound, search, subjectsPacket.subjectHash, itemHash);
</script>

<GenericLabelSelect {placeholder} bind:search options={eligible} onItemSelected={doSelect} />
<GenericLabelSelect {placeholder} noFiltering={true} bind:search options={eligible} onItemSelected={doSelect} />
15 changes: 7 additions & 8 deletions svelte-kit/src/lib/state/subjectsState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const filterSubjects = (subjects: Subject[], search?: string, lookupMap:
let regex = new RegExp(search, "i");
searchFn = s => regex.test(s.name) && !alreadySelected[s._id];
}
const selectedLookup: Set<string> = new Set([]);
const forcedLookup: Set<string> = new Set([]);
return subjects.reduce<DisablableSubject[]>((result, s) => {
if (searchFn(s)) {
const entry: DisablableSubject = { ...s, disabled: false };
Expand All @@ -61,18 +61,17 @@ export const filterSubjects = (subjects: Subject[], search?: string, lookupMap:
let parentId;

while ((parentId = computeParentId(currentSubject.path))) {
if (!parentId || selectedLookup.has(parentId)) {
break;
}
let parent = lookupMap[parentId];
if (!parent) {
const parent = lookupMap[parentId];

if (!parent || forcedLookup.has(parentId)) {
break;
}
let parentEntry: DisablableSubject = { ...parent, disabled: false };

const parentEntry: DisablableSubject = { ...parent, disabled: false };

if (alreadySelected[parent._id] || !searchFn(parent)) {
toAdd.unshift(parentEntry);
selectedLookup.add(parentId);
forcedLookup.add(parentId);

if (alreadySelected[parent._id]) {
parentEntry.disabled = true;
Expand Down

1 comment on commit a3678e3

@vercel
Copy link

@vercel vercel bot commented on a3678e3 Dec 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.