Skip to content

Commit

Permalink
chore: multi choice model for later
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-ext committed Dec 13, 2023
1 parent 89ee166 commit 50aa53d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app/utils/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,30 @@ export const createRadioModel = <T>(getter: Accessor<T>, setter: (next: T) => vo
};
};
};

export const createMultipleChoiceModel = <T>(getter: Accessor<T[]>, setter: (next: T[]) => void) => {
return (value: T) => {
return (node: HTMLInputElement) => {
createEffect(() => {
node.checked = getter().includes(value);
});

node.addEventListener('input', () => {
const next = node.checked;
const array = getter().slice();

if (next) {
array.push(value);
setter(array);
} else {
const index = array.indexOf(value);

if (index !== -1) {
array.splice(index, 1);
setter(array);
}
}
});
};
};
};

0 comments on commit 50aa53d

Please sign in to comment.