ant-design-vue/components/typography/Editable.tsx
tangjinzhou 2ee3d43534
Feat css var (#5327)
* style: affix & util

* feat(alert): add customIcon slot

* feat(anchor): ts type

* style: auto-complete

* feat: avatar add crossOrigin & maxPopoverTrigger

* style(backTop): v-show instead v-if

* style: badge

* style: breadcrumb

* feat: button add global size

* feat: update i18n

* feat: picker add disabledTime

* test: update snap

* doc: update img url

* style: fix Card tabs of left position

* doc: update cascader doc

* feat: collapse

* style: comment

* style: configprovider

* feat: date-picker add soem icon slot

* style: update descriptions style

* feat: add divider orientationMargin

* doc: update drawer

* feat: dropdown add destroyPopupOnHide & loading

* style: update empty

* feat: form add labelWrap

* style: update grid

* test: update grid snap

* fix: image ts error

* fix: mentions cannot select, close #5233

* doc: update pagination change info, close #5293

* fix: table dynamic expand error, close #5295

* style: remove not use

* release 3.0.0-beta.11

* doc: update typo

* feat: input add showCount

* feat: inputNumber add prefix slot

* style: update layout

* style: update list

* feat: add locale i18

* style: update locale ts

* style: update mentions

* feat: menu divider add dashed

* perf: menu

* perf: menu animate

* feat: modal method add wrapClassName

* style: update pageheader

* feat: update pagination ts

* feat: confirm add showCancel & promise

* doc: update popover

* style: update progress

* style: radio

* style: update rate、result、row

* feat: select add fieldNames

* feat: add skeleton button & input

* feat: spin tip support slot

* style: slider & space

* stype: update steps ts type

* style: update switch

* feat: table add tree filter

* test: update input sanp

* feat: table add filterMode...

* fix: tree autoExpandParent bug

* test: update input snap

* doc: tabs add destroyInactiveTabPane

* style: update tag

* style: update timeline & time-picker

* fix: Tooltip arrowPointAtCenter 1px shift bug

* feat: typography add enterEnterIcon triggerType

* doc: update tree-select

* fix: deps and TypeScript types

* style: udpate transfer

* style: update style

* doc: add colorScheme

* chore: add css var builg

* doc: sort api

* style: lint code

* doc: add css var

* test: update snap

* chore: add pre script

* chore: update lint

* perf: collapse animate

* perf: collapse tree

* perf: typography shaking when edit

* doc: update auto-complete demo

* fix: table tree not have animate

* feat: deprecated dropdown center placement

* feat: deprecated dropdown center placement

* test: update snap
2022-03-12 09:56:32 +08:00

139 lines
3.7 KiB
Vue

import KeyCode from '../_util/KeyCode';
import PropTypes from '../_util/vue-types';
import TextArea from '../input/TextArea';
import EnterOutlined from '@ant-design/icons-vue/EnterOutlined';
import type { PropType } from 'vue';
import { defineComponent, ref, reactive, watch, onMounted, computed } from 'vue';
import type { Direction } from '../config-provider';
const Editable = defineComponent({
name: 'Editable',
props: {
prefixCls: PropTypes.string,
value: PropTypes.string,
maxlength: PropTypes.number,
autoSize: PropTypes.oneOfType([PropTypes.looseBool, PropTypes.object]),
onSave: PropTypes.func,
onCancel: PropTypes.func,
onEnd: PropTypes.func,
onChange: PropTypes.func,
originContent: PropTypes.string,
direction: String as PropType<Direction>,
},
emits: ['save', 'cancel', 'end', 'change'],
setup(props, { emit, slots }) {
const state = reactive({
current: props.value || '',
lastKeyCode: undefined,
inComposition: false,
cancelFlag: false,
});
watch(
() => props.value,
current => {
state.current = current;
},
);
const textArea = ref();
onMounted(() => {
if (textArea.value) {
const resizableTextArea = textArea.value?.resizableTextArea;
const innerTextArea = resizableTextArea?.textArea;
innerTextArea.focus();
const { length } = innerTextArea.value;
innerTextArea.setSelectionRange(length, length);
}
});
function saveTextAreaRef(node: any) {
textArea.value = node;
}
function onChange({ target: { value } }) {
state.current = value.replace(/[\r\n]/g, '');
emit('change', state.current);
}
function onCompositionStart() {
state.inComposition = true;
}
function onCompositionEnd() {
state.inComposition = false;
}
function onKeyDown(e: KeyboardEvent) {
const { keyCode } = e;
if (keyCode === KeyCode.ENTER) {
e.preventDefault();
}
// We don't record keyCode when IME is using
if (state.inComposition) return;
state.lastKeyCode = keyCode;
}
function onKeyUp(e: KeyboardEvent) {
const { keyCode, ctrlKey, altKey, metaKey, shiftKey } = e;
// Check if it's a real key
if (
state.lastKeyCode === keyCode &&
!state.inComposition &&
!ctrlKey &&
!altKey &&
!metaKey &&
!shiftKey
) {
if (keyCode === KeyCode.ENTER) {
confirmChange();
emit('end');
} else if (keyCode === KeyCode.ESC) {
state.current = props.originContent;
emit('cancel');
}
}
}
function onBlur() {
confirmChange();
emit('end');
}
function confirmChange() {
emit('save', state.current.trim());
}
const textAreaClassName = computed(() => ({
[`${props.prefixCls}`]: true,
[`${props.prefixCls}-edit-content`]: true,
[`${props.prefixCls}-rtl`]: props.direction === 'rtl',
}));
return () => (
<div class={textAreaClassName.value}>
<TextArea
ref={saveTextAreaRef}
maxlength={props.maxlength}
value={state.current}
onChange={onChange}
onKeydown={onKeyDown}
onKeyup={onKeyUp}
onCompositionstart={onCompositionStart}
onCompositionend={onCompositionEnd}
onBlur={onBlur}
rows={1}
autoSize={props.autoSize === undefined || props.autoSize}
/>
{slots.enterIcon ? (
slots.enterIcon({ className: `${props.prefixCls}-edit-content-confirm` })
) : (
<EnterOutlined class={`${props.prefixCls}-edit-content-confirm`} />
)}
</div>
);
},
});
export default Editable;