增加垂直SegmentedControl组件。
This commit is contained in:
parent
9a6db8b8de
commit
5662b1d4d3
42
src/components/VSegmentedControl.module.css
Normal file
42
src/components/VSegmentedControl.module.css
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
@layer components {
|
||||||
|
.segmented_control {
|
||||||
|
display: inline-block;
|
||||||
|
border-radius: var(--border-radius-xxs);
|
||||||
|
overflow: hidden;
|
||||||
|
user-select: none;
|
||||||
|
.options {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
position: relative;
|
||||||
|
.option {
|
||||||
|
border-radius: var(--border-radius-xxs);
|
||||||
|
padding: var(--spacing-xxs) var(--spacing-xs);
|
||||||
|
line-height: 1.5em;
|
||||||
|
white-space: nowrap;
|
||||||
|
cursor: pointer;
|
||||||
|
z-index: 5;
|
||||||
|
transition: background-color 300ms;
|
||||||
|
&.selected {
|
||||||
|
background-color: var(--color-primary-active);
|
||||||
|
}
|
||||||
|
&:hover {
|
||||||
|
background-color: var(--color-primary-hover);
|
||||||
|
}
|
||||||
|
&:active {
|
||||||
|
background-color: var(--color-primary-active);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.slider {
|
||||||
|
position: absolute;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
transition: top 300ms ease, height 300ms ease;
|
||||||
|
pointer-events: none;
|
||||||
|
border-radius: var(--border-radius-xxs);
|
||||||
|
background-color: var(--color-primary-active);
|
||||||
|
width: 100%;
|
||||||
|
z-index: 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
52
src/components/VSegmentedControl.tsx
Normal file
52
src/components/VSegmentedControl.tsx
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
import cx from 'clsx';
|
||||||
|
import { isEqual, isNil } from 'lodash-es';
|
||||||
|
import { useCallback, useRef, useState } from 'react';
|
||||||
|
import type { Option } from '../models';
|
||||||
|
import styles from './VSegmentedControl.module.css';
|
||||||
|
|
||||||
|
type VSegmentedControlProps = {
|
||||||
|
options?: Option[];
|
||||||
|
value?: Option['value'];
|
||||||
|
onChange?: (value: Option['value']) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function VSegmentedControl({ options = [], value, onChange }: VSegmentedControlProps) {
|
||||||
|
const [selected, setSelected] = useState(value ?? options[0].value ?? null);
|
||||||
|
const [sliderPosition, setSliderPosition] = useState(0);
|
||||||
|
const [sliderHeight, setSliderHeight] = useState(0);
|
||||||
|
const sliderRef = useRef<HTMLDivElement>(null);
|
||||||
|
const optionsRef = useRef<HTMLDivElement[]>([]);
|
||||||
|
|
||||||
|
const handleSelectAction = useCallback((option: Option['value'], index: number) => {
|
||||||
|
setSelected(option);
|
||||||
|
onChange?.(option);
|
||||||
|
if (optionsRef.current && optionsRef.current.length > index) {
|
||||||
|
const optionElement = optionsRef.current[index];
|
||||||
|
setSliderPosition(optionElement.offsetTop);
|
||||||
|
setSliderHeight(optionElement.offsetHeight);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={styles.segmented_control}>
|
||||||
|
<div className={styles.options}>
|
||||||
|
{options.map((option, index) => (
|
||||||
|
<div
|
||||||
|
key={`${index}_${option.value}`}
|
||||||
|
className={cx(styles.option, isEqual(selected, option.value) && styles.selected)}
|
||||||
|
ref={(el) => (optionsRef.current[index] = el!)}
|
||||||
|
onClick={() => handleSelectAction(option.value, index)}>
|
||||||
|
{option.label}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
{!isNil(selected) && (
|
||||||
|
<div
|
||||||
|
className={styles.slider}
|
||||||
|
ref={sliderRef}
|
||||||
|
style={{ top: `${sliderPosition}px`, height: `${sliderHeight}px` }}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user