-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: taninsist <[email protected]>
- Loading branch information
Showing
27 changed files
with
2,374 additions
and
374 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,105 @@ | ||
import React, { forwardRef, Ref } from 'react'; | ||
import classNames from 'classnames'; | ||
import React, { forwardRef } from 'react'; | ||
import { Icon } from 'tdesign-icons-react'; | ||
import parseTNode from 'tdesign-mobile-react/_util/parseTNode'; | ||
import { StyledProps } from 'tdesign-mobile-react/common'; | ||
import useConfig from '../_util/useConfig'; | ||
import { TdCheckTagProps } from './type'; | ||
import useDefault from '../_util/useDefault'; | ||
import noop from '../_util/noop'; | ||
import useDefaultProps from '../hooks/useDefaultProps'; | ||
import { checkTagDefaultProps } from './defaultProps'; | ||
import { TdCheckTagProps } from './type'; | ||
|
||
export interface CheckTagProps extends TdCheckTagProps, StyledProps {} | ||
|
||
const CheckTag = forwardRef<HTMLSpanElement, CheckTagProps>((originProps, ref) => { | ||
const props = useDefaultProps(originProps, checkTagDefaultProps); | ||
const { | ||
checked, | ||
defaultChecked, | ||
content, | ||
children, | ||
style, | ||
className, | ||
icon, | ||
disabled, | ||
closable, | ||
size, | ||
shape, | ||
variant, | ||
onClick, | ||
onChange, | ||
onClose, | ||
...otherProps | ||
} = props; | ||
|
||
const { classPrefix } = useConfig(); | ||
|
||
export interface TagCheckProps extends TdCheckTagProps { | ||
className?: string; | ||
style?: object; | ||
} | ||
const [innerChecked, onInnerChecked] = useDefault(checked, defaultChecked, onChange); | ||
|
||
const TagCheck: React.FC<TagCheckProps> = React.memo( | ||
forwardRef((props, ref: Ref<HTMLButtonElement>) => { | ||
const { | ||
checked = undefined, | ||
defaultChecked = undefined, | ||
content = '', | ||
children, | ||
style = {}, | ||
className = '', | ||
icon = '', | ||
disabled = false, | ||
closable = false, | ||
size = 'medium', | ||
shape = 'square', | ||
onClick = noop, | ||
onChange = noop, | ||
...other | ||
} = props; | ||
const baseClass = `${classPrefix}-tag`; | ||
|
||
const { classPrefix } = useConfig(); | ||
const checkTagClass = classNames( | ||
`${baseClass}`, | ||
// `${baseClass}--checkable`, | ||
`${baseClass}--${shape}`, | ||
`${baseClass}--${innerChecked ? 'primary' : 'default'}`, | ||
`${baseClass}--${size}`, | ||
`${baseClass}--${variant}`, | ||
|
||
const [innerChecked, onInnerChecked] = useDefault(checked, defaultChecked, onChange); | ||
{ | ||
[`${baseClass}--closable`]: closable, | ||
[`${baseClass}--disabled`]: disabled, | ||
[`${baseClass}--checked`]: !disabled && innerChecked, | ||
}, | ||
className, | ||
); | ||
|
||
const baseClass = `${classPrefix}-tag`; | ||
const renderText = () => { | ||
if (Array.isArray(content) && content.length === 2) { | ||
return innerChecked ? content[0] : content[1]; | ||
} | ||
return content; | ||
}; | ||
|
||
const checkTagClass = classNames( | ||
`${baseClass}`, | ||
`${baseClass}--checkable`, | ||
`${baseClass}--shape-${shape}`, | ||
`${baseClass}--size-${size}`, | ||
{ | ||
[`${classPrefix}-is-closable ${baseClass}--closable`]: closable, | ||
[`${classPrefix}-is-disabled ${baseClass}--disabled`]: disabled, | ||
[`${classPrefix}-is-checked ${baseClass}--checked`]: innerChecked, | ||
}, | ||
className, | ||
); | ||
const childNode = parseTNode(renderText()) || parseTNode(children); | ||
|
||
const tagStyle = { | ||
...style, | ||
}; | ||
const tagStyle = { | ||
...style, | ||
}; | ||
|
||
const handleClick = (e) => { | ||
if (disabled || closable) { | ||
return; | ||
} | ||
const handleClick = (e) => { | ||
if (!props.disabled) { | ||
onClick?.(e); | ||
onInnerChecked(!innerChecked); | ||
onClick({ e }); | ||
}; | ||
} | ||
}; | ||
|
||
const handleClose = (e) => { | ||
if (disabled) { | ||
return; | ||
} | ||
e.stopPropagation(); | ||
onClick({ e }); | ||
}; | ||
const handleClose = (e) => { | ||
e.stopPropagation(); | ||
if (!props.disabled) { | ||
onClose?.({ e }); | ||
} | ||
}; | ||
|
||
return ( | ||
<span className={checkTagClass} style={tagStyle} onClick={handleClick} ref={ref} {...other}> | ||
<span className={`${baseClass}__icon`}>{icon}</span> | ||
<span className={`${baseClass}__text`}>{content || children}</span> | ||
{closable ? ( | ||
<span className={`${baseClass}__icon-close`} onClick={handleClose}> | ||
<Icon name="close" /> | ||
</span> | ||
) : null} | ||
</span> | ||
); | ||
}), | ||
); | ||
return ( | ||
<span | ||
ref={ref} | ||
className={checkTagClass} | ||
aria-disabled={disabled} | ||
role="button" | ||
onClick={handleClick} | ||
style={tagStyle} | ||
{...otherProps} | ||
> | ||
{icon && <span className={`${baseClass}__icon`}>{icon}</span>} | ||
<span className={`${baseClass}__text`}>{childNode}</span> | ||
{props.closable && ( | ||
<span className={`${baseClass}__icon-close`} onClick={handleClose}> | ||
<Icon name="close" /> | ||
</span> | ||
)} | ||
</span> | ||
); | ||
}); | ||
|
||
export default TagCheck; | ||
export default CheckTag; |
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
Oops, something went wrong.