support local value changes

This commit is contained in:
SrGooglo 2024-02-02 23:38:29 +01:00
parent fd64d24470
commit bdc0f6fbbd

View File

@ -32,9 +32,21 @@ const PKGConfigs = (props) => {
const config = defaultConfigs[key] const config = defaultConfigs[key]
const storagedValue = configs[key] const storagedValue = configs[key]
const [localValue, setLocalValue] = React.useState(storagedValue ?? config.default)
const ComponentType = config.ui_component ?? PKGConfigsComponentByTypes[config.type] ?? "input" const ComponentType = config.ui_component ?? PKGConfigsComponentByTypes[config.type] ?? "input"
const ConfigComponent = PKGConfigsComponents[ComponentType] const ConfigComponent = PKGConfigsComponents[ComponentType]
function handleOnChange(value) {
if (typeof value === "string" && config.string_trim === true) {
value = value.trim()
}
setLocalValue(value)
return props.onChange(key, value)
}
if (ConfigComponent == null) { if (ConfigComponent == null) {
return null return null
} }
@ -42,33 +54,34 @@ const PKGConfigs = (props) => {
const ComponentsProps = { const ComponentsProps = {
...config.ui_component_props, ...config.ui_component_props,
defaultValue: storagedValue ?? config.default, defaultValue: storagedValue ?? config.default,
value: localValue
} }
switch (ComponentType) { switch (ComponentType) {
case "input": { case "input": {
ComponentsProps.onChange = (e) => { ComponentsProps.onChange = (e) => {
props.onChange(key, e.target.value) handleOnChange(e.target.value)
} }
break break
} }
case "slider": { case "slider": {
ComponentsProps.onChange = (value) => { ComponentsProps.onChange = (value) => {
props.onChange(key, value) handleOnChange(value)
} }
break break
} }
case "switch": { case "switch": {
ComponentsProps.onChange = (checked) => { ComponentsProps.onChange = (checked) => {
props.onChange(key, checked) handleOnChange(checked)
} }
break break
} }
default: { default: {
ComponentsProps.onChange = (value) => { ComponentsProps.onChange = (value) => {
props.onChange(key, value) handleOnChange(value)
} }
break; break;
} }