From 27c1ec54aaf7d5f50f28d577a2fb82d56cc9f1bc Mon Sep 17 00:00:00 2001 From: unix Date: Sat, 28 Mar 2020 00:50:24 +0800 Subject: [PATCH] feat(input): add clearable and clear event --- components/input/input-icon-clear.tsx | 58 +++++++++++++++++++++++++++ components/input/input.tsx | 15 ++++++- pages/docs/components/input.mdx | 13 +++++- 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 components/input/input-icon-clear.tsx diff --git a/components/input/input-icon-clear.tsx b/components/input/input-icon-clear.tsx new file mode 100644 index 0000000..ded07ae --- /dev/null +++ b/components/input/input-icon-clear.tsx @@ -0,0 +1,58 @@ +import React, { useMemo } from 'react' +import useTheme from '../styles/use-theme' + +interface Props { + onClick?: (event: React.MouseEvent) => void + heightRatio?: string | undefined + disabled?: boolean +} + +const InputIconClear: React.FC = ({ + onClick, heightRatio, disabled, +}) => { + const theme = useTheme() + const width = useMemo(() => { + return heightRatio ? `calc(10.66px * ${heightRatio})` : '18px' + }, [heightRatio]) + const clickHandler = (event: React.MouseEvent) => { + event.preventDefault() + event.stopPropagation() + event.nativeEvent.stopImmediatePropagation() + onClick && onClick(event) + } + return ( +
+ + + + + + +
+ ) +} + +export default InputIconClear diff --git a/components/input/input.tsx b/components/input/input.tsx index 81d05f8..3d4344b 100644 --- a/components/input/input.tsx +++ b/components/input/input.tsx @@ -3,6 +3,7 @@ import withDefaults from '../utils/with-defaults' import useTheme from '../styles/use-theme' import InputLabel from './input-label' import InputIcon from './input-icon' +import InputClearIcon from './input-icon-clear' import { getSizes, getColors } from './styles' import { NormalSizes, NormalTypes } from '../utils/prop-types' @@ -20,12 +21,15 @@ interface Props { iconRight?: React.ReactNode width?: string className?: string + clearable?: boolean onChange?: (e: React.ChangeEvent) => void + onClearClick?: (e: React.MouseEvent) => void } const defaultProps = { disabled: false, readOnly: false, + clearable: false, width: 'initial', size: 'medium', status: 'default', @@ -39,7 +43,7 @@ export type InputProps = Props & typeof defaultProps & React.InputHTMLAttributes const Input: React.FC = ({ placeholder, label, labelRight, size, status, disabled, icon, iconRight, initialValue, onChange, readOnly, value, - width, className, ...props + onClearClick, clearable, width, className, ...props }) => { const theme = useTheme() const [selfValue, setSelfValue] = useState(initialValue) @@ -59,9 +63,15 @@ const Input: React.FC = ({ ) const changeHandler = (event: React.ChangeEvent) => { if (disabled || readOnly) return + console.log(123, event.target.value) setSelfValue(event.target.value) onChange && onChange(event) } + + const clearHandler = (event: React.MouseEvent) => { + setSelfValue('') + onClearClick && onClearClick(event) + } useEffect(() => { if (value === undefined) return @@ -83,6 +93,9 @@ const Input: React.FC = ({ onChange={changeHandler} {...props} /> + {clearable && } {iconRight && } {labelRight && {labelRight}} diff --git a/pages/docs/components/input.mdx b/pages/docs/components/input.mdx index 18670c5..6c6c83a 100644 --- a/pages/docs/components/input.mdx +++ b/pages/docs/components/input.mdx @@ -75,7 +75,7 @@ Retrieve text input from a user. `} /> @@ -83,6 +83,15 @@ Retrieve text input from a user. `} /> + + + +`} /> + void` | - | - | +| **onClearClick** | clear icon event | `(e: React.MouseEvent) => void` | - | - | | ... | native props | `InputHTMLAttributes` | `'alt', 'type', 'className', ...` | - | useInput