Skip to content

Commit

Permalink
fix: Option clear bug (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhea-so committed Apr 13, 2024
2 parents a8ce798 + 938668a commit 5f84483
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
25 changes: 16 additions & 9 deletions web/src/pages/root/components/theme-option.list-input.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
import { ListInput } from 'konsta/react';
import { useThemeStore } from '../../../themes';
import { useState } from 'react';
import { useEffect, useState } from 'react';

interface ThemeOptionListInputProps {
index: number;
key: string;
optionKey: string;
description?: string;
defaultValue: string | number;
type: typeof String | typeof Number;
}

const ThemeOptionListInput = (props: ThemeOptionListInputProps) => {
const { index, key, description, defaultValue, type } = props;
const { index, optionKey, description, defaultValue, type } = props;
const { option, setOption } = useThemeStore();
const [value, setValue] = useState(option.get(key) ?? defaultValue);
const [value, setValue] = useState(option.get(optionKey) ?? defaultValue);

useEffect(() => {
setValue(option.get(optionKey) ?? defaultValue);
}, [option, optionKey, defaultValue]);

return (
<ListInput
key={index}
name={key}
title={key}
name={optionKey}
title={optionKey}
info={description}
value={value}
onChange={(e) => {
const value = e.target.value;
if (type === Number) {
if (isNaN(Number(value))) setOption(key, defaultValue);
else setOption(key, Number(value));
if (isNaN(Number(value))) {
setOption(optionKey, defaultValue);
} else {
setOption(optionKey, Number(value));
}
} else {
setOption(key, value);
setOption(optionKey, value);
}
setValue(value);
}}
Expand Down
20 changes: 11 additions & 9 deletions web/src/pages/root/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,17 @@ const RootPage = () => {
{theme?.options.length !== 0 && <BlockTitle>{t('root.themes.customize')}</BlockTitle>}

<List strongIos inset>
{theme?.options.map((themeOption, index) => (
<ThemeOptionListInput
index={index}
key={themeOption.key}
description={themeOption.description}
defaultValue={themeOption.default}
type={themeOption.type}
/>
))}
{theme?.options.map((option, index) => {
return (
<ThemeOptionListInput
index={index}
optionKey={option.key}
description={option.description}
defaultValue={option.default}
type={option.type}
/>
);
})}
</List>
</>
)}
Expand Down

0 comments on commit 5f84483

Please sign in to comment.