fix(auto-complete): fix the height of custom options

This commit is contained in:
unix
2020-07-01 13:20:11 +08:00
parent b612dba0db
commit ee020acf2c
2 changed files with 16 additions and 3 deletions

View File

@@ -7,6 +7,7 @@ import Ellipsis from '../shared/ellipsis'
interface Props {
value: string
isLabelOnly?: boolean
}
const defaultProps = {}
@@ -26,6 +27,7 @@ const getSizes = (size: NormalSizes) => {
const AutoCompleteItem: React.FC<React.PropsWithChildren<AutoCompleteItemProps>> = ({
value: identValue,
children,
isLabelOnly,
}) => {
const theme = useTheme()
const { value, updateValue, size, updateVisible } = useAutoCompleteContext()
@@ -37,9 +39,20 @@ const AutoCompleteItem: React.FC<React.PropsWithChildren<AutoCompleteItemProps>>
const isActive = useMemo(() => value === identValue, [identValue, value])
const fontSize = useMemo(() => getSizes(size), [size])
// The 'isLabelOnly' is only used inside the component,
// Automatically adjust width when only label children is included.
const itemHeight = useMemo(() => {
if (isLabelOnly) return `calc(1.688 * ${theme.layout.gap})`
return 'auto'
}, [isLabelOnly, theme.layout.gap])
return (
<div className={`item ${isActive ? 'active' : ''}`} onClick={selectHandler}>
<Ellipsis height={`calc(1.688 * ${theme.layout.gap})`}>{children}</Ellipsis>
{isLabelOnly ? (
<Ellipsis height={`calc(1.688 * ${theme.layout.gap})`}>{children}</Ellipsis>
) : (
children
)}
<style jsx>{`
.item {
display: flex;
@@ -49,7 +62,7 @@ const AutoCompleteItem: React.FC<React.PropsWithChildren<AutoCompleteItemProps>>
white-space: pre;
font-size: ${fontSize};
padding: 0 ${theme.layout.gapHalf};
height: calc(1.688 * ${theme.layout.gap});
height: ${itemHeight};
background-color: ${theme.palette.background};
color: ${theme.palette.foreground};
user-select: none;

View File

@@ -56,7 +56,7 @@ const childrenToOptionsNode = (options: Array<AutoCompleteOption>) =>
if (React.isValidElement(item)) return React.cloneElement(item, { key })
const validItem = item as AutoCompleteOption
return (
<AutoCompleteItem key={key} value={validItem.value}>
<AutoCompleteItem key={key} value={validItem.value} isLabelOnly>
{validItem.label}
</AutoCompleteItem>
)