mirror of
https://github.com/zhigang1992/react.git
synced 2026-04-28 12:15:32 +08:00
* feat(snippet): add custom symbol and toast * docs(snippet): add docs for custom symbol and toast * fix(snippet): default toast type as normal type * test(snippet): add custom symbol and toast * docs(snippet): add enum type of APIs * docs(toast): add normal-types * chore: release v1.8.0-canary.1 * feat(modal): optimize the animation of layer * test: update snapshots * chore: release v1.8.0-canary.2 * docs: format import statement * feat(modal): optimize the max width, height and alignment of layer * test: update snapshots * chore: release v1.8.0-canary.3 * Button (#319) * feat(button): center the icon if the button is empty * test(button): add testcase for icon only * docs(button): add example for icon only * chore: release v1.8.0-canary.4 * fix(snippet): remove space when the symbol is empty (#325) * fix(snippet): remove space when the symbol is empty style(snippet): remove unnecessary escape symbols * fix(snippet): ignore spaces in symbol style: fix lint warning * chore: release v1.8.0-canary.5 * feat(tabs): sync the label and set value to required (#334) * feat(tabs): sync the label and set value to required * test(tabs): add testcase for label sync * docs(tabs): update value to required * feat(modal): use Button to reconstrust Modal.Action (#332) * feat(modal): use Button to reconstrust Modal.Action * docs(modal): add example for action loading * test: update snapshots * chore: release v1.8.0-canary.6 * fix(input): always synchronize external value and ignore changes (#336) * fix(input): always synchronize external value and ignore changes * feat(input): support imperative API to update * fix(textarea): imperative api same as input (#341) * feat(dropdown): allow dropdown to set the specified container (#344) * feat(dropdown): allow dropdown to set the specified container * test(modal): update snapshots * docs(select): add example for custom popup container * fix(dropdown): fix type of getPopupContainer * test(dropdown): add testcase for specified container rendering * chore: release v1.8.0-canary.7 * fix(link): fix hard-coded background color (#347) * style(description): fix title font-weight (#348) * docs(link): fix duplicate examples (#346) * style(popover-item): the whole item should be clickable when using with link (#345) * fix(modal): fix type of Modal.Action (#351) * chore: release v1.8.0-canary.8 * feat(modal): lock tab action inside modal (#354) * feat(button): add style to focus buttons * feat(collections): add util function * feat(modal): lock tab action inside modal * test(modal): add tests for modal focus * test: update style of button * fix(table): fix column's props are not tracked (#362) * chore: release v1.8.0-canary.9 * fix(table): children of column should be kept in sync (#364) * chore: release v1.8.0-canary.10 Co-authored-by: Augusto <augustoconti10@gmail.com> Co-authored-by: yqrashawn <namy.19@gmail.com> Co-authored-by: Zhao Lei <firede@firede.us>
162 lines
4.5 KiB
TypeScript
162 lines
4.5 KiB
TypeScript
import React, { useRef, useImperativeHandle, useEffect, useMemo, useState } from 'react'
|
|
import useTheme from '../styles/use-theme'
|
|
import withDefaults from '../utils/with-defaults'
|
|
import { NormalTypes } from '../utils/prop-types'
|
|
import { getColors } from '../input/styles'
|
|
|
|
interface Props {
|
|
value?: string
|
|
initialValue?: string
|
|
placeholder?: string
|
|
status?: NormalTypes
|
|
width?: string
|
|
minHeight?: string
|
|
disabled?: boolean
|
|
readOnly?: boolean
|
|
onChange?: (e: React.ChangeEvent<HTMLTextAreaElement>) => void
|
|
onFocus?: (e: React.FocusEvent<HTMLTextAreaElement>) => void
|
|
onBlur?: (e: React.FocusEvent<HTMLTextAreaElement>) => void
|
|
className?: string
|
|
}
|
|
|
|
const defaultProps = {
|
|
initialValue: '',
|
|
status: 'default' as NormalTypes,
|
|
width: 'initial',
|
|
minHeight: '6.25rem',
|
|
disabled: false,
|
|
readOnly: false,
|
|
className: '',
|
|
}
|
|
|
|
type NativeAttrs = Omit<React.TextareaHTMLAttributes<any>, keyof Props>
|
|
export type TextareaProps = Props & typeof defaultProps & NativeAttrs
|
|
|
|
const Textarea = React.forwardRef<HTMLTextAreaElement, React.PropsWithChildren<TextareaProps>>(
|
|
(
|
|
{
|
|
width,
|
|
status,
|
|
minHeight,
|
|
disabled,
|
|
readOnly,
|
|
onFocus,
|
|
onBlur,
|
|
className,
|
|
initialValue,
|
|
onChange,
|
|
value,
|
|
placeholder,
|
|
...props
|
|
},
|
|
ref: React.Ref<HTMLTextAreaElement | null>,
|
|
) => {
|
|
const theme = useTheme()
|
|
const textareaRef = useRef<HTMLTextAreaElement>(null)
|
|
useImperativeHandle(ref, () => textareaRef.current)
|
|
const isControlledComponent = useMemo(() => value !== undefined, [value])
|
|
const [selfValue, setSelfValue] = useState<string>(initialValue)
|
|
const [hover, setHover] = useState<boolean>(false)
|
|
const { color, borderColor, hoverBorder } = useMemo(() => getColors(theme.palette, status), [
|
|
theme.palette,
|
|
status,
|
|
])
|
|
|
|
const changeHandler = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
if (disabled || readOnly) return
|
|
setSelfValue(event.target.value)
|
|
onChange && onChange(event)
|
|
}
|
|
const focusHandler = (e: React.FocusEvent<HTMLTextAreaElement>) => {
|
|
setHover(true)
|
|
onFocus && onFocus(e)
|
|
}
|
|
const blurHandler = (e: React.FocusEvent<HTMLTextAreaElement>) => {
|
|
setHover(false)
|
|
onBlur && onBlur(e)
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (isControlledComponent) {
|
|
setSelfValue(value as string)
|
|
}
|
|
})
|
|
|
|
const controlledValue = isControlledComponent
|
|
? { value: selfValue }
|
|
: { defaultValue: initialValue }
|
|
const textareaProps = {
|
|
...props,
|
|
...controlledValue,
|
|
}
|
|
|
|
return (
|
|
<div className={`wrapper ${hover ? 'hover' : ''} ${disabled ? 'disabled' : ''} ${className}`}>
|
|
<textarea
|
|
ref={textareaRef}
|
|
disabled={disabled}
|
|
placeholder={placeholder}
|
|
readOnly={readOnly}
|
|
onFocus={focusHandler}
|
|
onBlur={blurHandler}
|
|
onChange={changeHandler}
|
|
{...textareaProps}
|
|
/>
|
|
<style jsx>{`
|
|
.wrapper {
|
|
display: inline-flex;
|
|
box-sizing: border-box;
|
|
user-select: none;
|
|
width: ${width};
|
|
min-width: 12.5rem;
|
|
max-width: 95vw;
|
|
height: auto;
|
|
border-radius: ${theme.layout.radius};
|
|
border: 1px solid ${borderColor};
|
|
color: ${color};
|
|
transition: border 0.2s ease 0s, color 0.2s ease 0s;
|
|
}
|
|
|
|
.wrapper.hover {
|
|
border-color: ${hoverBorder};
|
|
}
|
|
|
|
.wrapper.disabled {
|
|
background-color: ${theme.palette.accents_1};
|
|
border-color: ${theme.palette.accents_2};
|
|
cursor: not-allowed;
|
|
}
|
|
|
|
textarea {
|
|
background-color: transparent;
|
|
box-shadow: none;
|
|
display: block;
|
|
font-family: ${theme.font.sans};
|
|
font-size: 0.875rem;
|
|
width: 100%;
|
|
height: 100%;
|
|
min-height: ${minHeight};
|
|
resize: none;
|
|
border: none;
|
|
outline: none;
|
|
padding: ${theme.layout.gapHalf};
|
|
}
|
|
|
|
.disabled > textarea {
|
|
cursor: not-allowed;
|
|
}
|
|
|
|
textarea:-webkit-autofill,
|
|
textarea:-webkit-autofill:hover,
|
|
textarea:-webkit-autofill:active,
|
|
textarea:-webkit-autofill:focus {
|
|
-webkit-box-shadow: 0 0 0 30px ${theme.palette.background} inset !important;
|
|
}
|
|
`}</style>
|
|
</div>
|
|
)
|
|
},
|
|
)
|
|
|
|
export default withDefaults(Textarea, defaultProps)
|