ant-design/components/typography/Editable.tsx
vagusX 149729e524
use ant design icons 4.0 (#18217)
* feat: use @ant-design/icons@4.0

* feat: use createFromIconfontCN to make site works

* feat: update doc for Icon

* feat: use icon in component Alert

* feat: use icon in component Avatar

* feat: use icon in component Breadcrumb

* feat: use icon in component Button

* feat: use icon in component Cascader

* feat: use icon in component Collapse

* feat: use icon in component Datepicker

* feat: use icon in component Dropdown

* feat: use icon in component Form

* feat: use icon in component Input

* feat: use icon in component InputNumber

* feat: use icon in component Layout

* feat: use icon in component Mention

* feat: use icon in component Message

* feat: use icon in component Modal

* feat: use icon in component Notification

* feat: use icon in component PageHeader

* feat: use icon in component Pagination

* feat: use icon in component Popconfirm

* feat: use icon in component Progress

* feat: use icon in component Rate

* feat: use icon in component Result

* feat: use icon in component Select

* feat: use icon in component Step

* feat: use icon in component Switch

* feat: use icon in component Table

* feat: use icon in component Tab

* feat: use icon in component Tag

* feat: handle rest component which using Icon

* fix: remove unused vars

* feat: use latest alpha ant design icons

* fix: failed test in uploadlist.test.js

* test: update snapshot for icons

* doc: add Icon for site

* doc: use @ant-design/icons in site

* chore: use latest icons

* fix: tslint issue

* fix: test cases

* fix: types for react

* fix: lint rules for import orders

* fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render

* fix: eslint error in demo/**.md

* inject antd icons

* update snapshot

* fix site

* doc: update docs

* fix: code snippets icon in site

* feat: use latest @ant-design/icons

* fix: icon props in message
2019-08-13 14:07:17 +08:00

140 lines
3.1 KiB
TypeScript

import * as React from 'react';
import KeyCode from 'rc-util/lib/KeyCode';
import { polyfill } from 'react-lifecycles-compat';
import { Enter } from '@ant-design/icons';
import TextArea from '../input/TextArea';
interface EditableProps {
prefixCls?: string;
value?: string;
['aria-label']?: string;
onSave: (value: string) => void;
onCancel: () => void;
className?: string;
style?: React.CSSProperties;
}
interface EditableState {
current: string;
prevValue?: string;
}
class Editable extends React.Component<EditableProps, EditableState> {
static getDerivedStateFromProps(nextProps: EditableProps, prevState: EditableState) {
const { prevValue } = prevState;
const { value } = nextProps;
const newState: Partial<EditableState> = {
prevValue: value,
};
if (prevValue !== value) {
newState.current = value;
}
return newState;
}
textarea?: TextArea;
lastKeyCode?: number;
inComposition?: boolean = false;
state = {
current: '',
};
componentDidMount() {
if (this.textarea) {
this.textarea.focus();
}
}
onChange: React.ChangeEventHandler<HTMLTextAreaElement> = ({ target: { value } }) => {
this.setState({ current: value.replace(/[\r\n]/g, '') });
};
onCompositionStart = () => {
this.inComposition = true;
};
onCompositionEnd = () => {
this.inComposition = false;
};
onKeyDown: React.KeyboardEventHandler<HTMLTextAreaElement> = ({ keyCode }) => {
// We don't record keyCode when IME is using
if (this.inComposition) return;
this.lastKeyCode = keyCode;
};
onKeyUp: React.KeyboardEventHandler<HTMLTextAreaElement> = ({
keyCode,
ctrlKey,
altKey,
metaKey,
shiftKey,
}) => {
const { onCancel } = this.props;
// Check if it's a real key
if (
this.lastKeyCode === keyCode &&
!this.inComposition &&
!ctrlKey &&
!altKey &&
!metaKey &&
!shiftKey
) {
if (keyCode === KeyCode.ENTER) {
this.confirmChange();
} else if (keyCode === KeyCode.ESC) {
onCancel();
}
}
};
onBlur: React.FocusEventHandler<HTMLTextAreaElement> = () => {
this.confirmChange();
};
confirmChange = () => {
const { current } = this.state;
const { onSave } = this.props;
onSave(current.trim());
};
setTextarea = (textarea: TextArea) => {
this.textarea = textarea;
};
render() {
const { current } = this.state;
const { prefixCls, 'aria-label': ariaLabel, className, style } = this.props;
return (
<div className={`${prefixCls} ${prefixCls}-edit-content ${className}`} style={style}>
<TextArea
ref={this.setTextarea}
value={current}
onChange={this.onChange}
onKeyDown={this.onKeyDown}
onKeyUp={this.onKeyUp}
onCompositionStart={this.onCompositionStart}
onCompositionEnd={this.onCompositionEnd}
onBlur={this.onBlur}
aria-label={ariaLabel}
autosize
/>
<Enter className={`${prefixCls}-edit-content-confirm`} />
</div>
);
}
}
polyfill(Editable);
export default Editable;