Skip to content

Commit a5e4c62

Browse files
author
梁朝飞
committed
feat: 优化代码
1 parent 972ebc5 commit a5e4c62

File tree

4 files changed

+61
-67
lines changed

4 files changed

+61
-67
lines changed

assets/index.less

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
&-group {
2828
position: relative;
2929
display: flex;
30+
flex-direction: row;
3031
align-items: stretch;
3132
justify-content: flex-start;
3233
width: 100%;
@@ -74,37 +75,28 @@
7475
&-thumb {
7576
.segmented-item-selected();
7677
position: absolute;
78+
width: 0;
79+
height: 100%;
7780
padding: 4px 0;
7881
transition: transform @transition-duration @transition-timing-function,
7982
width @transition-duration @transition-timing-function;
8083
}
8184

82-
&-group {
83-
flex-direction: row;
85+
&-vertical &-group {
86+
flex-direction: column;
8487
}
8588

86-
&-thumb {
87-
width: 0;
88-
height: 100%;
89+
&-vertical &-item {
90+
width: 100%;
91+
text-align: left;
8992
}
9093

91-
&-vertical {
92-
&-group {
93-
flex-direction: column;
94-
}
95-
96-
&-item {
97-
width: 100%;
98-
text-align: left;
99-
}
100-
101-
&-thumb {
102-
width: 100%;
103-
height: 0;
104-
padding: 0 4px;
105-
transition: transform @transition-duration @transition-timing-function,
106-
height @transition-duration @transition-timing-function;
107-
}
94+
&-vertical &-thumb {
95+
width: 100%;
96+
height: 0;
97+
padding: 0 4px;
98+
transition: transform @transition-duration @transition-timing-function,
99+
height @transition-duration @transition-timing-function;
108100
}
109101

110102
// disabled styles

docs/demo/basic.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default function App() {
1313
</div>
1414
<div className="wrapper">
1515
<Segmented
16-
position="vertical"
16+
mode="vertical"
1717
options={['iOS', 'Android', 'Web']}
1818
onChange={(value) => console.log(value, typeof value)}
1919
/>

src/MotionThumb.tsx

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ export interface MotionThumbInterface {
2323
onMotionStart: VoidFunction;
2424
onMotionEnd: VoidFunction;
2525
direction?: 'ltr' | 'rtl';
26-
position?: 'horizontal' | 'vertical';
26+
mode?: 'horizontal' | 'vertical';
2727
}
2828

2929
const calcThumbStyle = (
3030
targetElement: HTMLElement | null | undefined,
31-
position: 'horizontal' | 'vertical',
31+
mode: 'horizontal' | 'vertical',
3232
): ThumbReact => {
3333
if (!targetElement) return null;
3434

@@ -47,7 +47,7 @@ const calcThumbStyle = (
4747
height: targetElement.clientHeight,
4848
};
4949

50-
if (position === 'vertical') {
50+
if (mode === 'vertical') {
5151
return {
5252
left: 0,
5353
right: 0,
@@ -81,7 +81,7 @@ export default function MotionThumb(props: MotionThumbInterface) {
8181
onMotionStart,
8282
onMotionEnd,
8383
direction,
84-
position = 'horizontal',
84+
mode = 'horizontal',
8585
} = props;
8686

8787
const thumbRef = React.useRef<HTMLDivElement>(null);
@@ -103,8 +103,8 @@ export default function MotionThumb(props: MotionThumbInterface) {
103103
const prev = findValueElement(prevValue);
104104
const next = findValueElement(value);
105105

106-
const calcPrevStyle = calcThumbStyle(prev, position);
107-
const calcNextStyle = calcThumbStyle(next, position);
106+
const calcPrevStyle = calcThumbStyle(prev, mode);
107+
const calcNextStyle = calcThumbStyle(next, mode);
108108

109109
setPrevValue(value);
110110
setPrevStyle(calcPrevStyle);
@@ -120,37 +120,45 @@ export default function MotionThumb(props: MotionThumbInterface) {
120120

121121
const thumbStart = React.useMemo(
122122
() =>
123-
position === 'vertical'
123+
mode === 'vertical'
124124
? toPX(prevStyle?.top ?? 0)
125-
: toPX(prevStyle?.left ?? 0),
126-
[position, prevStyle],
125+
: direction === 'rtl'
126+
? toPX(-(prevStyle?.right as number))
127+
: toPX(prevStyle?.left as number),
128+
[mode, direction, prevStyle],
127129
);
128130

129131
const thumbActive = React.useMemo(
130132
() =>
131-
position === 'vertical'
133+
mode === 'vertical'
132134
? toPX(nextStyle?.top ?? 0)
133-
: toPX(nextStyle?.left ?? 0),
134-
[position, nextStyle],
135+
: direction === 'rtl'
136+
? toPX(-(nextStyle?.right as number))
137+
: toPX(nextStyle?.left as number),
138+
[mode, direction, nextStyle],
135139
);
136140

137-
const onAppearStart = () => ({
138-
transform: `translate${
139-
position === 'vertical' ? 'Y' : 'X'
140-
}(var(--thumb-start-${position === 'vertical' ? 'top' : 'left'}))`,
141-
[position === 'vertical' ? 'height' : 'width']: `var(--thumb-start-${
142-
position === 'vertical' ? 'height' : 'width'
143-
})`,
144-
});
145-
146-
const onAppearActive = () => ({
147-
transform: `translate${
148-
position === 'vertical' ? 'Y' : 'X'
149-
}(var(--thumb-active-${position === 'vertical' ? 'top' : 'left'}))`,
150-
[position === 'vertical' ? 'height' : 'width']: `var(--thumb-active-${
151-
position === 'vertical' ? 'height' : 'width'
152-
})`,
153-
});
141+
const onAppearStart = () =>
142+
mode === 'vertical'
143+
? {
144+
transform: 'translateY(var(--thumb-start-top))',
145+
height: 'var(--thumb-start-height)',
146+
}
147+
: {
148+
transform: 'translateX(var(--thumb-start-left))',
149+
width: 'var(--thumb-start-width)',
150+
};
151+
152+
const onAppearActive = () =>
153+
mode === 'vertical'
154+
? {
155+
transform: 'translateY(var(--thumb-active-top))',
156+
height: 'var(--thumb-active-height)',
157+
}
158+
: {
159+
transform: 'translateX(var(--thumb-active-left))',
160+
width: 'var(--thumb-active-width)',
161+
};
154162

155163
const onVisibleChanged = () => {
156164
setPrevStyle(null);
@@ -187,11 +195,7 @@ export default function MotionThumb(props: MotionThumbInterface) {
187195
const motionProps = {
188196
ref: composeRef(thumbRef, ref),
189197
style: mergedStyle,
190-
className: classNames(
191-
`${prefixCls}-thumb`,
192-
`${prefixCls}-${position}-thumb`,
193-
motionClassName,
194-
),
198+
className: classNames(`${prefixCls}-thumb`, motionClassName),
195199
};
196200

197201
if (process.env.NODE_ENV === 'test') {

src/index.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export interface SegmentedLabeledOption<ValueType = SegmentedRawOption> {
1515
disabled?: boolean;
1616
label: React.ReactNode;
1717
value: ValueType;
18+
/**
19+
* html `title` property for label
20+
*/
1821
title?: string;
1922
}
2023

@@ -36,7 +39,7 @@ export interface SegmentedProps<ValueType = SegmentedValue>
3639
prefixCls?: string;
3740
direction?: 'ltr' | 'rtl';
3841
motionName?: string;
39-
position?: 'horizontal' | 'vertical'; // Add position prop
42+
mode?: 'horizontal' | 'vertical'; // Add mode prop
4043
}
4144

4245
function getValidTitle(option: SegmentedLabeledOption) {
@@ -124,7 +127,7 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
124127
const {
125128
prefixCls = 'rc-segmented',
126129
direction,
127-
position = 'horizontal', // Default to 'horizontal'
130+
mode,
128131
options = [],
129132
disabled,
130133
defaultValue,
@@ -175,20 +178,15 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
175178
{
176179
[`${prefixCls}-rtl`]: direction === 'rtl',
177180
[`${prefixCls}-disabled`]: disabled,
178-
[`${prefixCls}-vertical`]: position === 'vertical', // Apply vertical class if position is 'vertical'
181+
[`${prefixCls}-vertical`]: mode === 'vertical', // Apply vertical class if mode is 'vertical'
179182
},
180183
className,
181184
)}
182185
ref={mergedRef}
183186
>
184-
<div
185-
className={classNames(
186-
`${prefixCls}-group`,
187-
`${prefixCls}-${position}-group`,
188-
)}
189-
>
187+
<div className={classNames(`${prefixCls}-group`)}>
190188
<MotionThumb
191-
position={position}
189+
mode={mode}
192190
prefixCls={prefixCls}
193191
value={rawValue}
194192
containerRef={containerRef}

0 commit comments

Comments
 (0)