Skip to content

Commit

Permalink
Merge pull request #904 from ynput/903-settings-default-values-of-ite…
Browse files Browse the repository at this point in the history
…m-in-list-are-not-propagated

fix(Settings): Using default value for new/unset fields
  • Loading branch information
flynput authored Nov 8, 2024
2 parents c12a90c + 3627eaf commit 3c6590d
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 25 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"@dnd-kit/core": "^6.0.8",
"@dnd-kit/sortable": "^8.0.0",
"@reduxjs/toolkit": "^2.2.7",
"@rjsf/core": "^5.21.2",
"@rjsf/utils": "^5.21.2",
"@rjsf/validator-ajv8": "^5.21.2",
"@rjsf/core": "^5.22.1",
"@rjsf/utils": "^5.22.1",
"@rjsf/validator-ajv8": "^5.22.1",
"@rtk-query/codegen-openapi": "^1.2.0",
"@rtk-query/graphql-request-base-query": "^2.3.1",
"@uiw/react-textarea-code-editor": "^2.1.9",
Expand Down
1 change: 0 additions & 1 deletion src/containers/AddonSettings/searchTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ const attachLabels = (settings: $Any, relSchema: $Any, globalSchema: $Any): $Any
}
}

console.log('hydrated object', hydratedObject)
return hydratedObject
}

Expand Down
6 changes: 4 additions & 2 deletions src/containers/SettingsEditor/SettingsPanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,11 @@ const SettingsPanel = ({
const [expandedObjects, setExpandedObjects] = useLocalStorage('expanded-settings-keys', [])

const onToggle = () => {
if (expandedObjects.includes(objId))
if (expandedObjects.includes(objId)) {
setExpandedObjects(expandedObjects.filter((id) => id !== objId))
else setExpandedObjects([...expandedObjects, objId])
} else {
setExpandedObjects([...expandedObjects, objId])
}
}

const expanded = expandedObjects.includes(objId)
Expand Down
18 changes: 13 additions & 5 deletions src/containers/SettingsEditor/Widgets/CheckboxWidget.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useState, useEffect } from 'react'
import { InputSwitch } from '@ynput/ayon-react-components'

import { updateChangedKeys, parseContext } from '../helpers'
import { updateChangedKeys, parseContext, getDefaultValue } from '../helpers'
import { $Any } from '@types'

const CheckboxWidget = function (props: $Any) {
const { originalValue, path } = parseContext(props)
const [value, setValue] = useState(null)
const [value, setValue] = useState(props.value || getDefaultValue(props))
const [initialized, setInitialized] = useState(false)

useEffect(() => {
Expand All @@ -17,6 +17,9 @@ const CheckboxWidget = function (props: $Any) {
if (value === null) return
if (value === props.value) return
if (initialized) return
if (props.value === undefined && value === getDefaultValue(props)) {
return
}

setInitialized(true)
if (path?.length) return
Expand All @@ -27,9 +30,11 @@ const CheckboxWidget = function (props: $Any) {

useEffect(() => {
// Sync the local state with the formData
if (props.value === undefined) return
if (value === props.value) return
setValue(props.value || false)
if (value === props.value) {
return
}

setValue(props.value || getDefaultValue(props))
}, [props.value])

useEffect(() => {
Expand All @@ -44,6 +49,9 @@ const CheckboxWidget = function (props: $Any) {
if (!props.onChange) return
if (value === null) return
if (value === props.value) return
if (props.value === undefined && value === getDefaultValue(props)) {
return
}
// this timeout must be here. idk why. if not,
// the value will be set to the original value or smth
props.onChange(value)
Expand Down
15 changes: 12 additions & 3 deletions src/containers/SettingsEditor/Widgets/SelectWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,18 @@ const SelectWidget = (props: $Any) => {

useEffect(() => {
// Sync the local state with the formData
if (props.value === undefined) return
if (equiv(value, props.value)) return
setValue(props.value || (props.multiple ? [] : ''))
if (equiv(value, props.value)) {
return
}

let defaultValue
if (props.multiple) {
defaultValue = props.schema.default || []
} else {
defaultValue = props.schema.default || ''
}

setValue(props.value || defaultValue)
}, [props.value])

useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/containers/SettingsEditor/Widgets/TextWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const TextWidget = (props: $Any) => {

if (['integer', 'number'].includes(props.schema.type)) {
Input = InputNumber
opts.value = value === undefined || value === null ? '' : value
opts.value = value === undefined || value === null ? getDefaultValue(props) : value
opts.onBlur = () => onChangeCommit(props.schema.type)
opts.onChange = (e: $Any) => {
// ensure that the value is a number. decimal points are allowed
Expand Down Expand Up @@ -147,7 +147,7 @@ export const TextWidget = (props: $Any) => {
} else {
// Default text input
Input = InputText
opts.value = value || ''
opts.value = value || getDefaultValue(props)
opts.onBlur = onChangeCommit
opts.onChange = (e: $Any) => {
onChange(e.target.value)
Expand Down
26 changes: 21 additions & 5 deletions src/containers/SettingsEditor/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,29 @@ const updateChangedKeys = (props: $Any, changed: $Any, path: $Any) => {
}

const getDefaultValue = (props: $Any) => {
//console.log("Creating default value for", props.id)
if (props.schema.widget === 'color' && !props.value) {
if (props.schema.colorFormat === 'hex') return props.schema.colorAlpha ? '#00000000' : '#000000'
if (props.schema.default) {
return props.schema.default
}
if (props.schema.colorFormat === 'hex') {
return props.schema.colorAlpha ? '#00000000' : '#000000'
}
return props.schema.colorAlpha ? [0, 0, 0, 0] : [0, 0, 0]
}
if (props.value !== undefined) return props.value
if (props.schema.type === 'string') return ''
if (props.schema.type === 'integer') return 0
if (props.value !== undefined) {
return props.value
}

if (props.schema.type === 'boolean') {
return props.schema.default || false
}

if (props.schema.type === 'string') {
return props.schema.default || ''
}

if (props.schema.type === 'integer') {
return props.schema.default || 0
}
}
export { getDefaultValue, equiv, parseContext, updateChangedKeys }
6 changes: 2 additions & 4 deletions src/hooks/useLocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
import { useEffect, useState } from 'react'

export default function useLocalStorage<T>(key: string, defaultValue: T): [T, (value: T) => void] {
const [value, setValue] = useState(defaultValue)
const item = localStorage.getItem(key)
const [value, setValue] = useState(item ? JSON.parse(item) : defaultValue)

useEffect(() => {
const item = localStorage.getItem(key)

if (!item) {
localStorage.setItem(key, JSON.stringify(defaultValue))
}

setValue(item ? JSON.parse(item) : defaultValue)

function handler(e: StorageEvent) {
if (e.key !== key) return

Expand Down

0 comments on commit 3c6590d

Please sign in to comment.