@@ -2,7 +2,7 @@ import AppSideBar from "../../components/AppSideBar";
22import { getPages } from "./settingsPageConfig" ;
33
44import "./Settings.scss" ;
5- import { HTMLInputTypeAttribute , useState } from "react" ;
5+ import React , { HTMLInputTypeAttribute , useRef , useState } from "react" ;
66import useSystemSettings from "../../stores/systemSettingsStore" ;
77
88interface SettingsSectionProps {
@@ -16,6 +16,12 @@ interface SettingsSectionProps {
1616}
1717
1818function SettingsSection ( section : SettingsSectionProps ) {
19+ // We'll keep the section value in local state for a short time
20+ // before updating global state with it. This will allow values which
21+ // are actively being input and at the time are technically "invalid"
22+ // to still be shown on the screen
23+ const updateStateDelay = useRef < NodeJS . Timeout | null > ( null ) ;
24+ const [ value , setValue ] = useState ( section . currentValue ) ;
1925 function getInputType ( type : string ) : HTMLInputTypeAttribute {
2026 switch ( type ) {
2127 case "color" :
@@ -26,6 +32,26 @@ function SettingsSection(section: SettingsSectionProps) {
2632 return "text" ;
2733 }
2834 }
35+ function handleChange ( event : React . ChangeEvent < HTMLInputElement > ) {
36+ setValue ( event . currentTarget . value ) ;
37+ updateStateDelay . current && clearTimeout ( updateStateDelay . current ) ;
38+ updateStateDelay . current = setTimeout ( ( ) => {
39+ const newValue = event . target . value ;
40+ if ( section . validValues && ! section . validValues . includes ( newValue ) ) {
41+ console . error ( "Not an allowed value" ) ;
42+ return ;
43+ }
44+
45+ if ( section . valueValidation ) {
46+ console . error ( "Not a valid value" ) ;
47+ const error = section . valueValidation ( newValue ) ;
48+ if ( error ) return ;
49+ }
50+ section . onValueChanged ( newValue ) ;
51+ } , 1000 ) ;
52+ }
53+
54+ console . log ( "value is " , value ) ;
2955 return (
3056 < div className = "settings__page-section" >
3157 < h1 className = "settings__page-section__title" > { section . title } </ h1 >
@@ -35,11 +61,8 @@ function SettingsSection(section: SettingsSectionProps) {
3561 < input
3662 className = "settings__page-section__value"
3763 type = { getInputType ( section . type ) }
38- value = { String ( section . currentValue ) }
39- onChange = { ( e ) => {
40- console . log ( "value changed to" , e . currentTarget . value ) ;
41- section . onValueChanged ( e . currentTarget . value ) ;
42- } }
64+ value = { String ( value ) }
65+ onChange = { handleChange }
4366 > </ input >
4467 </ div >
4568 ) ;
0 commit comments