Skip to content

Commit ad3b5f4

Browse files
committed
feat: 支持受控
1 parent e248e3d commit ad3b5f4

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

src/packages/elevator/elevator.taro.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { harmony } from '@/utils/taro/platform'
1414
import { useUuid } from '@/hooks/use-uuid'
1515
import raf from '@/utils/raf'
1616
import { ElevatorItem, ElevatorList, TaroElevatorProps } from '@/types'
17+
import { usePropsValue } from '@/hooks'
1718

1819
export const elevatorContext = createContext({} as ElevatorItem)
1920

@@ -26,12 +27,16 @@ const defaultProps = {
2627
sticky: false,
2728
spaceHeight: 18,
2829
showKeys: true,
30+
defaultValue: undefined,
31+
value: undefined,
2932
} as TaroElevatorProps
3033

3134
export const Elevator: FunctionComponent<
3235
Partial<TaroElevatorProps> & React.HTMLAttributes<HTMLDivElement>
3336
> & { Context: typeof elevatorContext } = (props) => {
3437
const {
38+
value,
39+
defaultValue,
3540
mode,
3641
height,
3742
floorKey,
@@ -62,10 +67,10 @@ export const Elevator: FunctionComponent<
6267
y2: 0,
6368
})
6469

65-
const [currentData, setCurrentData] = useState<ElevatorItem>(
66-
{} as ElevatorItem
67-
)
68-
const [currentKey, setCurrentKey] = useState('')
70+
const [currentData, setCurrentData] = usePropsValue<ElevatorItem>({
71+
value,
72+
defaultValue: defaultValue || ({} as ElevatorItem),
73+
})
6974
const [codeIndex, setCodeIndex] = useState<number>(0)
7075
const [scrollStart, setScrollStart] = useState<boolean>(false)
7176
const state = useRef(initData)
@@ -159,7 +164,6 @@ export const Elevator: FunctionComponent<
159164
const handleClickItem = (key: string, item: ElevatorItem) => {
160165
onItemClick && onItemClick(key, item)
161166
setCurrentData(item)
162-
setCurrentKey(key)
163167
}
164168

165169
const handleClickIndex = (key: string) => {
@@ -241,8 +245,7 @@ export const Elevator: FunctionComponent<
241245
className={classNames({
242246
[`${classPrefix}-list-item-name`]: true,
243247
[`${classPrefix}-list-item-name-highcolor`]:
244-
currentData.id === subitem.id &&
245-
currentKey === item[floorKey],
248+
currentData.id === subitem.id,
246249
})}
247250
key={subitem.id}
248251
onClick={() => handleClickItem(item[floorKey], subitem)}

src/packages/elevator/elevator.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { animated } from '@react-spring/web'
1010
import classNames from 'classnames'
1111
import { ComponentDefaults } from '@/utils/typings'
1212
import { ElevatorItem, WebElevatorProps, ElevatorList } from '@/types'
13+
import { usePropsValue } from '@/hooks'
1314

1415
export const elevatorContext = createContext({} as ElevatorItem)
1516

@@ -22,12 +23,16 @@ const defaultProps = {
2223
sticky: false,
2324
spaceHeight: 18,
2425
showKeys: true,
26+
defaultValue: undefined,
27+
value: undefined,
2528
} as WebElevatorProps
2629

2730
export const Elevator: FunctionComponent<
2831
Partial<WebElevatorProps> & React.HTMLAttributes<HTMLDivElement>
2932
> & { Context: typeof elevatorContext } = (props) => {
3033
const {
34+
value,
35+
defaultValue,
3136
mode,
3237
height,
3338
floorKey,
@@ -58,10 +63,10 @@ export const Elevator: FunctionComponent<
5863
y2: 0,
5964
})
6065
const [scrollY, setScrollY] = useState(0)
61-
const [currentData, setCurrentData] = useState<ElevatorItem>(
62-
{} as ElevatorItem
63-
)
64-
const [currentKey, setCurrentKey] = useState('')
66+
const [currentData, setCurrentData] = usePropsValue<ElevatorItem>({
67+
value,
68+
defaultValue: defaultValue || ({} as ElevatorItem),
69+
})
6570
const [currentIndex, setCurrentIndex] = useState<number>(0)
6671
const [codeIndex, setCodeIndex] = useState<number>(0)
6772
const [scrollStart, setScrollStart] = useState<boolean>(false)
@@ -141,7 +146,6 @@ export const Elevator: FunctionComponent<
141146
const handleClickItem = (key: string, item: ElevatorItem) => {
142147
onItemClick && onItemClick(key, item)
143148
setCurrentData(item)
144-
setCurrentKey(key)
145149
}
146150

147151
const handleClickIndex = (key: string) => {
@@ -220,8 +224,7 @@ export const Elevator: FunctionComponent<
220224
className={classNames({
221225
[`${classPrefix}-list-item-name`]: true,
222226
[`${classPrefix}-list-item-name-highcolor`]:
223-
currentData.id === subitem.id &&
224-
currentKey === item[floorKey],
227+
currentData.id === subitem.id,
225228
})}
226229
key={subitem.id}
227230
onClick={() => handleClickItem(item[floorKey], subitem)}

src/types/spec/elevator/base.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { SimpleValue } from '../../base/atoms'
44
export interface ElevatorItem {
55
name: string
66
id: SimpleValue
7-
87
[key: string]: SimpleValue
98
}
109

@@ -17,6 +16,8 @@ export type ElevatorList = {
1716
export type ElevatorMode = 'horizontal' | 'vertical'
1817

1918
export interface BaseElevator extends BaseProps {
19+
defaultValue?: ElevatorItem
20+
value?: ElevatorItem
2021
mode: ElevatorMode
2122
height: SimpleValue
2223
floorKey: ElevatorFloorKey

0 commit comments

Comments
 (0)