fix: linux display main page
This commit is contained in:
@@ -1,60 +1,97 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { JSX, useEffect, useState } from 'react';
|
||||
import Button from '../Button/Button';
|
||||
import { TriangleUpIcon, TriangleDownIcon } from '../../assets/icons/Triangle';
|
||||
import './ChoiceStepper.css';
|
||||
|
||||
type ChoiceStepperProps<T> = {
|
||||
function StepperArrows({
|
||||
onUp,
|
||||
onDown,
|
||||
canUp,
|
||||
canDown,
|
||||
disabled,
|
||||
}: {
|
||||
onUp: () => void;
|
||||
onDown: () => void;
|
||||
canUp: boolean;
|
||||
canDown: boolean;
|
||||
disabled?: boolean;
|
||||
}) {
|
||||
return (
|
||||
<div className="choice-stepper__arrows">
|
||||
<Button
|
||||
variant="toggle"
|
||||
className="choice-stepper__arrow-btn"
|
||||
onClick={onUp}
|
||||
disabled={disabled || !canUp}
|
||||
>
|
||||
<TriangleUpIcon />
|
||||
</Button>
|
||||
<Button
|
||||
variant="toggle"
|
||||
className="choice-stepper__arrow-btn"
|
||||
onClick={onDown}
|
||||
disabled={disabled || !canDown}
|
||||
>
|
||||
<TriangleDownIcon />
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ---- List variant ----
|
||||
|
||||
type ChoiceStepperListProps<T> = {
|
||||
value: T;
|
||||
values: readonly T[];
|
||||
formatLabel: (value: T) => string;
|
||||
onChange: (value: T) => void;
|
||||
id?: string;
|
||||
disabled?: boolean;
|
||||
className?: string;
|
||||
};
|
||||
|
||||
function ChoiceStepper<T>({ value, values, formatLabel, onChange }: ChoiceStepperProps<T>) {
|
||||
function ChoiceStepperList<T>({
|
||||
value,
|
||||
values,
|
||||
formatLabel,
|
||||
onChange,
|
||||
id,
|
||||
disabled,
|
||||
className,
|
||||
}: ChoiceStepperListProps<T>) {
|
||||
const index = values.indexOf(value);
|
||||
|
||||
const canUp = index === -1 || index < values.length - 1;
|
||||
const canDown = index > 0;
|
||||
|
||||
const [text, setText] = useState(formatLabel(value));
|
||||
|
||||
useEffect(() => {
|
||||
setText(formatLabel(value));
|
||||
}, [value, formatLabel]);
|
||||
useEffect(() => setText(formatLabel(value)), [value, formatLabel]);
|
||||
|
||||
const step = (direction: 1 | -1) => {
|
||||
const currentIndex = index === -1 ? 0 : index;
|
||||
|
||||
const nextIndex = Math.min(values.length - 1, Math.max(0, currentIndex + direction));
|
||||
|
||||
if (nextIndex !== currentIndex) {
|
||||
onChange(values[nextIndex]);
|
||||
}
|
||||
if (nextIndex !== currentIndex) onChange(values[nextIndex]);
|
||||
};
|
||||
|
||||
const commitText = () => {
|
||||
const match = values.find(
|
||||
(v) => formatLabel(v).trim().toLowerCase() === text.trim().toLowerCase()
|
||||
);
|
||||
|
||||
if (match !== undefined) {
|
||||
onChange(match);
|
||||
} else {
|
||||
setText(formatLabel(value));
|
||||
}
|
||||
if (match !== undefined) onChange(match);
|
||||
else setText(formatLabel(value));
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="choice-stepper">
|
||||
<div className={`choice-stepper ${className ?? ''}`} data-disabled={disabled || undefined}>
|
||||
<input
|
||||
id={id}
|
||||
className="choice-stepper__input"
|
||||
value={text}
|
||||
disabled={disabled}
|
||||
onChange={(e) => setText(e.target.value)}
|
||||
onBlur={commitText}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter') {
|
||||
e.currentTarget.blur();
|
||||
} else if (e.key === 'ArrowUp') {
|
||||
if (e.key === 'Enter') e.currentTarget.blur();
|
||||
else if (e.key === 'ArrowUp') {
|
||||
e.preventDefault();
|
||||
step(1);
|
||||
} else if (e.key === 'ArrowDown') {
|
||||
@@ -63,28 +100,99 @@ function ChoiceStepper<T>({ value, values, formatLabel, onChange }: ChoiceSteppe
|
||||
}
|
||||
}}
|
||||
/>
|
||||
|
||||
<div className="choice-stepper__arrows">
|
||||
<Button
|
||||
variant="toggle"
|
||||
className="choice-stepper__arrow-btn"
|
||||
onClick={() => step(1)}
|
||||
disabled={!canUp}
|
||||
>
|
||||
<TriangleUpIcon />
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
variant="toggle"
|
||||
className="choice-stepper__arrow-btn"
|
||||
onClick={() => step(-1)}
|
||||
disabled={!canDown}
|
||||
>
|
||||
<TriangleDownIcon />
|
||||
</Button>
|
||||
</div>
|
||||
<StepperArrows
|
||||
onUp={() => step(1)}
|
||||
onDown={() => step(-1)}
|
||||
canUp={canUp}
|
||||
canDown={canDown}
|
||||
disabled={disabled}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ---- Range variant (concrete, no generic) ----
|
||||
|
||||
type ChoiceStepperRangeProps = {
|
||||
value: number;
|
||||
min: number;
|
||||
max: number;
|
||||
step?: number;
|
||||
formatLabel: (value: number) => string;
|
||||
onChange: (value: number) => void;
|
||||
id?: string;
|
||||
disabled?: boolean;
|
||||
className?: string;
|
||||
};
|
||||
|
||||
function ChoiceStepperRange({
|
||||
value,
|
||||
min,
|
||||
max,
|
||||
step: stepSize = 1,
|
||||
formatLabel,
|
||||
onChange,
|
||||
id,
|
||||
disabled,
|
||||
className,
|
||||
}: ChoiceStepperRangeProps) {
|
||||
const [text, setText] = useState(formatLabel(value));
|
||||
useEffect(() => setText(formatLabel(value)), [value, formatLabel]);
|
||||
|
||||
const clamp = (n: number) => Math.min(max, Math.max(min, n));
|
||||
|
||||
const canUp = value < max;
|
||||
const canDown = value > min;
|
||||
|
||||
const step = (direction: 1 | -1) => {
|
||||
const next = clamp(value + direction * stepSize);
|
||||
if (next !== value) onChange(next);
|
||||
};
|
||||
|
||||
const commitText = () => {
|
||||
const parsed = Number(text);
|
||||
if (Number.isFinite(parsed)) onChange(clamp(parsed));
|
||||
else setText(formatLabel(value));
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={`choice-stepper ${className ?? ''}`} data-disabled={disabled || undefined}>
|
||||
<input
|
||||
id={id}
|
||||
className="choice-stepper__input"
|
||||
value={text}
|
||||
disabled={disabled}
|
||||
onChange={(e) => setText(e.target.value)}
|
||||
onBlur={commitText}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter') e.currentTarget.blur();
|
||||
else if (e.key === 'ArrowUp') {
|
||||
e.preventDefault();
|
||||
step(1);
|
||||
} else if (e.key === 'ArrowDown') {
|
||||
e.preventDefault();
|
||||
step(-1);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<StepperArrows
|
||||
onUp={() => step(1)}
|
||||
onDown={() => step(-1)}
|
||||
canUp={canUp}
|
||||
canDown={canDown}
|
||||
disabled={disabled}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function ChoiceStepper<T>(props: ChoiceStepperListProps<T>): JSX.Element;
|
||||
function ChoiceStepper(props: ChoiceStepperRangeProps): JSX.Element;
|
||||
function ChoiceStepper(props: ChoiceStepperListProps<unknown> | ChoiceStepperRangeProps) {
|
||||
if ('values' in props) {
|
||||
return <ChoiceStepperList {...props} />;
|
||||
}
|
||||
return <ChoiceStepperRange {...props} />;
|
||||
}
|
||||
|
||||
export default ChoiceStepper;
|
||||
|
||||
Reference in New Issue
Block a user