-
Notifications
You must be signed in to change notification settings - Fork 211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Addition of explicit value of sliders #211
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,75 @@ | ||
import React from 'react'; | ||
import React, {useState, useRef, useEffect} from 'react'; | ||
import styled from 'styled-components'; | ||
|
||
const Container = styled.span` | ||
display: inline-block; | ||
display: flex; | ||
align-items: center; | ||
line-height: initial; | ||
width: 200px; | ||
width: 250px; // Increase the width | ||
`; | ||
|
||
const SliderInput = styled.input.attrs({type: 'range'})<any>` | ||
const SliderInput = styled.input.attrs({type: 'range'})` | ||
accent-color: var(--color_accent); | ||
width: 100%; | ||
`; | ||
|
||
export const AccentRange: React.FC< | ||
Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange'> & { | ||
onChange: (x: number) => void; | ||
const ValueDisplay = styled.span` | ||
margin-right: 10px; | ||
width: 50px; // Set a fixed width | ||
text-align: right; // Align the text to the right | ||
flex-shrink: 0; // Prevent it from shrinking | ||
`; | ||
|
||
interface AccentRangeProps | ||
extends Omit< | ||
React.InputHTMLAttributes<HTMLInputElement>, | ||
'onChange' | 'type' | ||
> { | ||
onChange: (x: number) => void; | ||
showingSliderValue?: boolean; // Make showSliderValue optional | ||
} | ||
|
||
export const AccentRange: React.FC<AccentRangeProps> = (props) => { | ||
const {showingSliderValue = false} = props; // Default showSliderValue to false if not provided | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
const [sliderValue, setSliderValue] = useState<number>(0); // Initialize with 0 | ||
|
||
useEffect(() => { | ||
if (inputRef.current) { | ||
setSliderValue(+inputRef.current.value || 0); // Provide a default value | ||
} | ||
}, [props.value]); | ||
|
||
Comment on lines
+37
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whats this for? it already initializes to zero... , setting the sliderValue each time props.value changes? doesnt the onChange below already do that? |
||
if (!showingSliderValue) { | ||
// Render original version without value display | ||
return ( | ||
<Container> | ||
<SliderInput | ||
ref={inputRef} | ||
{...props} | ||
onChange={(e: React.ChangeEvent<HTMLInputElement>) => { | ||
const value = +e.target.value; | ||
setSliderValue(value); | ||
props.onChange?.(value); | ||
}} | ||
/> | ||
</Container> | ||
); | ||
} | ||
> = (props) => ( | ||
<Container> | ||
<SliderInput | ||
{...props} | ||
onChange={(e: React.ChangeEvent<HTMLInputElement>) => { | ||
props.onChange && props.onChange(+e.target.value); | ||
}} | ||
/> | ||
</Container> | ||
); | ||
|
||
// Render enhanced version with value display | ||
return ( | ||
<Container> | ||
<ValueDisplay>{sliderValue}</ValueDisplay> | ||
<SliderInput | ||
ref={inputRef} | ||
{...props} | ||
onChange={(e: React.ChangeEvent<HTMLInputElement>) => { | ||
const value = +e.target.value; | ||
setSliderValue(value); | ||
props.onChange?.(value); | ||
}} | ||
/> | ||
</Container> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import React from 'react'; | ||
import {useSelector} from 'react-redux'; | ||
import {PelpiKeycodeInput} from '../../../inputs/pelpi/keycode-input'; | ||
import {AccentButton} from '../../../inputs/accent-button'; | ||
import {AccentSlider} from '../../../inputs/accent-slider'; | ||
|
@@ -10,6 +11,7 @@ import type {LightingData} from '../../../../types/types'; | |
import {ArrayColorPicker} from '../../../inputs/color-picker'; | ||
import {ConnectedColorPalettePicker} from 'src/components/inputs/color-palette-picker'; | ||
import {shiftFrom16Bit, shiftTo16Bit} from 'src/utils/keyboard-api'; | ||
import {getShowSliderValue} from 'src/store/settingsSlice'; | ||
|
||
type Props = { | ||
lightingData: LightingData; | ||
|
@@ -28,29 +30,35 @@ type ControlMeta = [ | |
type AdvancedControlProps = Props & {meta: ControlMeta}; | ||
|
||
export const VIACustomItem = React.memo( | ||
(props: VIACustomControlProps & {_id: string}) => ( | ||
<ControlRow id={props._id}> | ||
<Label>{props.label}</Label> | ||
<Detail> | ||
{'type' in props ? ( | ||
<VIACustomControl | ||
{...props} | ||
value={props.value && Array.from(props.value)} | ||
/> | ||
) : ( | ||
props.content | ||
)} | ||
</Detail> | ||
</ControlRow> | ||
), | ||
(props: VIACustomControlProps & {_id: string}) => { | ||
const showSliderValue = useSelector(getShowSliderValue); | ||
|
||
return ( | ||
<ControlRow id={props._id}> | ||
<Label>{props.label}</Label> | ||
<Detail> | ||
{'type' in props ? ( | ||
<VIACustomControl | ||
{...props} | ||
value={props.value && Array.from(props.value)} | ||
showSliderValue={showSliderValue} | ||
/> | ||
) : ( | ||
props.content | ||
)} | ||
</Detail> | ||
</ControlRow> | ||
); | ||
}, | ||
Comment on lines
+33
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't need to edit this file in general? |
||
); | ||
|
||
type ControlGetSet = { | ||
value: number[]; | ||
updateValue: (name: string, ...command: number[]) => void; | ||
}; | ||
|
||
type VIACustomControlProps = VIAItem & ControlGetSet; | ||
type VIACustomControlProps = VIAItem & | ||
ControlGetSet & {label: string; showSliderValue: boolean}; | ||
|
||
const boxOrArr = <N extends any>(elem: N | N[]) => | ||
Array.isArray(elem) ? elem : [elem]; | ||
|
@@ -76,17 +84,17 @@ const getRangeBytes = (value: number, max: number) => { | |
}; | ||
|
||
const VIACustomControl = (props: VIACustomControlProps) => { | ||
const {content, type, options, value} = props as any; | ||
const {content, type, options, value, showSliderValue} = props as any; | ||
const [name, ...command] = content; | ||
switch (type) { | ||
case 'button': { | ||
const buttonOption: any[] = options || [1]; | ||
return ( | ||
<AccentButton | ||
onClick={() => | ||
props.updateValue(name, ...command, buttonOption[0]) | ||
} | ||
>Click</AccentButton> | ||
onClick={() => props.updateValue(name, ...command, buttonOption[0])} | ||
> | ||
Click | ||
</AccentButton> | ||
); | ||
} | ||
case 'range': { | ||
|
@@ -102,6 +110,7 @@ const VIACustomControl = (props: VIACustomControlProps) => { | |
...getRangeBytes(val, options[1]), | ||
) | ||
} | ||
showingSliderValue={showSliderValue} | ||
/> | ||
); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should read the settings directly from here?