chore: use writable derived in more places (#17248)

chore(web): use writable derived in more places
This commit is contained in:
Ben McCann 2025-03-31 07:15:52 -07:00 committed by GitHub
parent d613f15606
commit b25914c2a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 14 additions and 37 deletions

View File

@ -13,11 +13,7 @@
let { id, albumName = $bindable(), isOwned, onUpdate }: Props = $props(); let { id, albumName = $bindable(), isOwned, onUpdate }: Props = $props();
let newAlbumName = $state(albumName); let newAlbumName = $derived(albumName);
$effect(() => {
newAlbumName = albumName;
});
const handleUpdateName = async () => { const handleUpdateName = async () => {
if (newAlbumName === albumName) { if (newAlbumName === albumName) {

View File

@ -16,10 +16,7 @@
// Updating `value` directly causes the date input to reset itself or // Updating `value` directly causes the date input to reset itself or
// interfere with user changes. // interfere with user changes.
let updatedValue = $state<string>(); let updatedValue = $derived(value);
$effect(() => {
updatedValue = value;
});
</script> </script>
<input <input

View File

@ -11,10 +11,7 @@
let { content = '', class: className = '', onContentUpdate = () => null, placeholder = '' }: Props = $props(); let { content = '', class: className = '', onContentUpdate = () => null, placeholder = '' }: Props = $props();
let newContent = $state(content); let newContent = $derived(content);
$effect(() => {
newContent = content;
});
const updateContent = () => { const updateContent = () => {
if (content === newContent) { if (content === newContent) {

View File

@ -71,7 +71,7 @@
* Keeps track of whether the combobox is actively being used. * Keeps track of whether the combobox is actively being used.
*/ */
let isActive = $state(false); let isActive = $state(false);
let searchQuery = $state(selectedOption?.label || ''); let searchQuery = $derived(selectedOption?.label || '');
let selectedIndex: number | undefined = $state(); let selectedIndex: number | undefined = $state();
let optionRefs: HTMLElement[] = $state([]); let optionRefs: HTMLElement[] = $state([]);
let input = $state<HTMLInputElement>(); let input = $state<HTMLInputElement>();
@ -228,10 +228,6 @@
const getInputPosition = () => input?.getBoundingClientRect(); const getInputPosition = () => input?.getBoundingClientRect();
$effect(() => {
searchQuery = selectedOption ? selectedOption.label : '';
});
let filteredOptions = $derived.by(() => { let filteredOptions = $derived.by(() => {
const _options = options.filter((option) => option.label.toLowerCase().includes(searchQuery.toLowerCase())); const _options = options.filter((option) => option.label.toLowerCase().includes(searchQuery.toLowerCase()));

View File

@ -14,15 +14,11 @@
let { count = 5, rating, readOnly = false, onRating }: Props = $props(); let { count = 5, rating, readOnly = false, onRating }: Props = $props();
let ratingSelection = $state(rating); let ratingSelection = $derived(rating);
let hoverRating = $state(0); let hoverRating = $state(0);
let focusRating = $state(0); let focusRating = $state(0);
let timeoutId: ReturnType<typeof setTimeout> | undefined; let timeoutId: ReturnType<typeof setTimeout> | undefined;
$effect(() => {
ratingSelection = rating;
});
const starIcon = const starIcon =
'M10.788 3.21c.448-1.077 1.976-1.077 2.424 0l2.082 5.007 5.404.433c1.164.093 1.636 1.545.749 2.305l-4.117 3.527 1.257 5.273c.271 1.136-.964 2.033-1.96 1.425L12 18.354 7.373 21.18c-.996.608-2.231-.29-1.96-1.425l1.257-5.273-4.117-3.527c-.887-.76-.415-2.212.749-2.305l5.404-.433 2.082-5.006z'; 'M10.788 3.21c.448-1.077 1.976-1.077 2.424 0l2.082 5.007 5.404.433c1.164.093 1.636 1.545.749 2.305l-4.117 3.527 1.257 5.273c.271 1.136-.964 2.033-1.96 1.425L12 18.354 7.373 21.18c-.996.608-2.231-.29-1.96-1.425l1.257-5.273-4.117-3.527c-.887-.76-.415-2.212.749-2.305l5.404-.433 2.082-5.006z';
const id = generateId(); const id = generateId();

View File

@ -16,14 +16,11 @@
let { tree, parent, value, active = '', icons, getLink, getColor }: Props = $props(); let { tree, parent, value, active = '', icons, getLink, getColor }: Props = $props();
let path = $derived(normalizeTreePath(`${parent}/${value}`)); const path = $derived(normalizeTreePath(`${parent}/${value}`));
let isActive = $derived(active === path || active.startsWith(`${path}/`)); const isActive = $derived(active === path || active.startsWith(`${path}/`));
let isOpen = $state(false); const isTarget = $derived(active === path);
$effect(() => { const color = $derived(getColor(path));
isOpen = isActive; let isOpen = $derived(isActive);
});
let isTarget = $derived(active === path);
let color = $derived(getColor(path));
const onclick = (event: MouseEvent) => { const onclick = (event: MouseEvent) => {
event.preventDefault(); event.preventDefault();

View File

@ -13,8 +13,10 @@
let password = $state(''); let password = $state('');
let confirmPassword = $state(''); let confirmPassword = $state('');
let name = $state(''); let name = $state('');
let errorMessage = $state(''); let errorMessage = $derived(
let valid = $derived(password === confirmPassword && confirmPassword.length > 0); password === confirmPassword || confirmPassword.length === 0 ? '' : $t('password_does_not_match'),
);
const valid = $derived(password === confirmPassword && confirmPassword.length > 0);
interface Props { interface Props {
data: PageData; data: PageData;
@ -22,10 +24,6 @@
let { data }: Props = $props(); let { data }: Props = $props();
$effect(() => {
errorMessage = password === confirmPassword || confirmPassword.length === 0 ? '' : $t('password_does_not_match');
});
const onSubmit = async (event: Event) => { const onSubmit = async (event: Event) => {
event.preventDefault(); event.preventDefault();