-
Notifications
You must be signed in to change notification settings - Fork 66
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
feat(cascader): cascader #475
Open
ming680
wants to merge
1
commit into
Tencent:develop
Choose a base branch
from
ming680:feature/cascader
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,307 @@ | ||
import { useDeepCompareEffect } from 'ahooks'; | ||
import classNames from 'classnames'; | ||
import last from 'lodash/last'; | ||
import React, { forwardRef, useCallback, useContext, useEffect, useMemo, useState } from 'react'; | ||
import { CheckIcon, Icon } from 'tdesign-icons-react'; | ||
import useDefault from 'tdesign-mobile-react/_util/useDefault'; | ||
import { Popup } from 'tdesign-mobile-react/popup'; | ||
import { Radio, RadioGroup } from 'tdesign-mobile-react/radio'; | ||
import Tabs from 'tdesign-mobile-react/tabs'; | ||
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. tabs 已完成组件升级,辛苦rebase dev |
||
import TabContext from 'tdesign-mobile-react/tabs/context'; | ||
import { StyledProps, TNode, TreeOptionData } from '../common'; | ||
import { usePrefixClass } from '../hooks/useClass'; | ||
import useDefaultProps from '../hooks/useDefaultProps'; | ||
import { cascaderDefaultProps } from './defaultProps'; | ||
import { TdCascaderProps } from './type'; | ||
|
||
export interface CascaderProps extends TdCascaderProps, StyledProps {} | ||
|
||
const FixedTabs = ({ value }: { value: number }) => { | ||
const { onChange } = useContext(TabContext); | ||
|
||
useEffect(() => { | ||
onChange(value); | ||
}, [value, onChange]); | ||
|
||
return null; | ||
}; | ||
|
||
const Cascader = forwardRef<HTMLDivElement, CascaderProps>((props) => { | ||
const cascaderClass = usePrefixClass('cascader'); | ||
|
||
const { | ||
className, | ||
style, | ||
value, | ||
defaultValue, | ||
visible, | ||
title, | ||
placeholder, | ||
theme, | ||
subTitles, | ||
options: inputOptions, | ||
keys, | ||
checkStrictly, | ||
closeBtn, | ||
onChange, | ||
onClose, | ||
onPick, | ||
} = useDefaultProps<CascaderProps>(props, cascaderDefaultProps); | ||
|
||
const [internalValue, setInternalValue] = useDefault(value, defaultValue, onChange); | ||
const [internalVisible, setInternalVisible] = useDefault(visible, false, () => ({})); | ||
|
||
const [internalSelectedValues, setInternalSelectedValues] = useState<CascaderProps['value'][]>([]); | ||
|
||
// 根据 inputOptions 和 key 重新构建 options | ||
const options = useMemo(() => { | ||
const { label = 'label', value = 'value', children = 'children' } = keys || {}; | ||
|
||
const convert = (options: TreeOptionData[]) => | ||
options.map((item) => ({ | ||
label: item[label], | ||
value: item[value], | ||
children: Array.isArray(item[children]) ? convert(item[children]) : false, | ||
})); | ||
|
||
return convert(inputOptions); | ||
}, [inputOptions, keys]); | ||
|
||
const getOptionsList = useCallback((options: TreeOptionData[], internalSelectedValues: CascaderProps['value'][]) => { | ||
const optionsList: TreeOptionData[][] = [options]; | ||
|
||
for (const value of internalSelectedValues) { | ||
const lastOptions = last(optionsList); | ||
const next = lastOptions.find((item) => item.value === value); | ||
if (!next || !Array.isArray(next.children)) { | ||
break; | ||
} | ||
optionsList.push(next.children); | ||
} | ||
|
||
return optionsList; | ||
}, []); | ||
|
||
const optionsList = useMemo( | ||
() => getOptionsList(options, internalSelectedValues), | ||
[getOptionsList, options, internalSelectedValues], | ||
); | ||
|
||
const [stepIndex, setStepIndex] = useState(0); | ||
|
||
const labelList = useMemo(() => { | ||
const labelList: { | ||
label: TNode; | ||
isPlaceholder: boolean; | ||
}[] = []; | ||
|
||
optionsList.forEach((options, index) => { | ||
const value = internalSelectedValues[index]; | ||
const target = options.find((item) => item.value === value); | ||
if (target) { | ||
labelList.push({ | ||
label: target.label, | ||
isPlaceholder: false, | ||
}); | ||
return; | ||
} | ||
|
||
labelList.push({ | ||
label: placeholder, | ||
isPlaceholder: true, | ||
}); | ||
}); | ||
|
||
return labelList; | ||
}, [optionsList, internalSelectedValues, placeholder]); | ||
|
||
const selectedValuesByInterValue = useMemo(() => { | ||
/** | ||
* checkStrictly true 从外到内 匹配上就挺 返回整个链路上的value | ||
* checkStrictly false 最后一级的 value 匹配时,返回整个链路上的value | ||
*/ | ||
const findValues = (options: TreeOptionData[]): CascaderProps['value'][] => { | ||
for (const item of options) { | ||
if (checkStrictly && item.value === internalValue) { | ||
return [item.value]; | ||
} | ||
|
||
const isLast = !(Array.isArray(item.children) && item.children.length); | ||
if (isLast) { | ||
if (item.value === internalValue) { | ||
return [item.value]; | ||
} | ||
continue; | ||
} | ||
const targetValue = findValues(item.children as TreeOptionData[]); | ||
if (targetValue.length) { | ||
return [item.value, ...targetValue]; | ||
} | ||
} | ||
return []; | ||
}; | ||
|
||
return findValues(options); | ||
}, [options, internalValue, checkStrictly]); | ||
|
||
// 当 selectedValuesByInterValue 深度变化 的时候再控制 selectedValues | ||
useDeepCompareEffect(() => { | ||
setInternalSelectedValues(selectedValuesByInterValue); | ||
setStepIndex(selectedValuesByInterValue.length); | ||
}, [selectedValuesByInterValue]); | ||
|
||
useEffect(() => { | ||
const reviseStepIndex = Math.max(Math.min(stepIndex, optionsList.length - 1), 0); | ||
if (reviseStepIndex !== stepIndex) { | ||
setStepIndex(reviseStepIndex); | ||
} | ||
}, [optionsList, stepIndex]); | ||
|
||
// 结束了 | ||
const onFinish = useCallback( | ||
(selectedValues: CascaderProps['value'][]) => { | ||
const selectedOptions = [...optionsList].slice(0, selectedValues.length).map((options, index) => { | ||
const target = options.find((item) => item.value === selectedValues[index]); | ||
const { label = 'label', value = 'value' } = keys || {}; | ||
return { | ||
[label]: target?.label || '', | ||
[value]: target?.value || '', | ||
}; | ||
}); | ||
setInternalValue(last(selectedValues), selectedOptions as any); | ||
onClose?.('finish'); | ||
}, | ||
[onClose, optionsList, setInternalValue, keys], | ||
); | ||
|
||
return ( | ||
<Popup | ||
visible={internalVisible} | ||
placement="bottom" | ||
onVisibleChange={(visible, trigger) => { | ||
setInternalVisible(visible); | ||
onClose?.(trigger); | ||
}} | ||
> | ||
<div className={classNames(cascaderClass, className)} style={style}> | ||
<div className={`${cascaderClass}__title`}>{title}</div> | ||
<div | ||
className={`${cascaderClass}__close-btn`} | ||
onClick={() => { | ||
if (checkStrictly) { | ||
onFinish(internalSelectedValues); | ||
return; | ||
} | ||
|
||
setInternalVisible(false); | ||
onClose?.('close-btn'); | ||
}} | ||
> | ||
{closeBtn === true ? <Icon name="close" size={24} /> : closeBtn} | ||
</div> | ||
<div className={`${cascaderClass}__content`}> | ||
{labelList.length ? ( | ||
<div> | ||
{theme === 'step' ? ( | ||
<div className={`${cascaderClass}__steps`}> | ||
{labelList.map((labeItem, index) => ( | ||
<div | ||
key={index} | ||
className={`${cascaderClass}__step`} | ||
onClick={() => { | ||
setStepIndex(index); | ||
}} | ||
> | ||
<div | ||
className={classNames(`${cascaderClass}__step-dot`, { | ||
[`${cascaderClass}__step-dot--active`]: !labeItem.isPlaceholder, | ||
[`${cascaderClass}__step-dot--last`]: index === labelList.length - 1, | ||
})} | ||
/> | ||
<div | ||
className={classNames(`${cascaderClass}__step-label`, { | ||
[`${cascaderClass}__step-label--active`]: index === stepIndex, | ||
})} | ||
> | ||
{labeItem.label} | ||
</div> | ||
<Icon name="chevron-right" size={22} className={`${cascaderClass}__step-arrow`} /> | ||
</div> | ||
))} | ||
</div> | ||
) : null} | ||
{theme === 'tab' ? ( | ||
<div> | ||
<Tabs | ||
list={labelList.map((item, index) => ({ | ||
label: item.label as string, | ||
value: index, | ||
}))} | ||
defaultValue={stepIndex} | ||
change={(value) => { | ||
setStepIndex(value); | ||
}} | ||
> | ||
{/* TODO: Tabs 组加接收外部控制 通过子组件 调用 TabContext 中的 onChange 实现 */} | ||
<FixedTabs value={stepIndex} /> | ||
</Tabs> | ||
</div> | ||
) : null} | ||
</div> | ||
) : null} | ||
{subTitles[stepIndex] ? ( | ||
<div className={`${cascaderClass}__options-title`}>{subTitles[stepIndex]}</div> | ||
) : null} | ||
<div | ||
className={`${cascaderClass}__options-container`} | ||
style={{ | ||
width: `${optionsList.length}00vw`, | ||
transform: `translateX(-${stepIndex}00vw)`, | ||
}} | ||
> | ||
{optionsList.map((curOptions, index) => ( | ||
<div className={`${cascaderClass}__options`} key={index}> | ||
<div> | ||
<RadioGroup | ||
value={internalSelectedValues[index]} | ||
onChange={(value: string | number) => { | ||
const selectedValues = [...internalSelectedValues].slice(0, index); | ||
selectedValues.push(value); | ||
setInternalSelectedValues(selectedValues); | ||
|
||
setStepIndex(index + 1); | ||
|
||
onPick?.(value, index); | ||
|
||
const next = curOptions.find((item) => item.value === value); | ||
if (Array.isArray(next?.children)) { | ||
return; | ||
} | ||
|
||
onFinish(selectedValues); | ||
}} | ||
> | ||
{curOptions.map((item) => ( | ||
<Radio | ||
allowUncheck={true} | ||
align="right" | ||
key={item.value} | ||
value={item.value} | ||
label={item.label} | ||
icon={[<CheckIcon key="1" color="#0052d9" />]} | ||
/> | ||
))} | ||
</RadioGroup> | ||
</div> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
</Popup> | ||
); | ||
}); | ||
|
||
Cascader.displayName = 'Cascader'; | ||
|
||
export default Cascader; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
组件内部使用相对路径