2021-03-16 04:54:22 +00:00
|
|
|
import KeyCode from '../_util/KeyCode';
|
|
|
|
import TextArea from '../input/TextArea';
|
|
|
|
import EnterOutlined from '@ant-design/icons-vue/EnterOutlined';
|
2022-03-26 14:52:54 +00:00
|
|
|
import type { ExtractPropTypes, PropType } from 'vue';
|
2022-03-12 01:56:32 +00:00
|
|
|
import { defineComponent, ref, reactive, watch, onMounted, computed } from 'vue';
|
|
|
|
import type { Direction } from '../config-provider';
|
2022-03-26 14:52:54 +00:00
|
|
|
import type { ChangeEventHandler } from '../_util/EventInterface';
|
|
|
|
import type { AutoSizeType } from '../input/inputProps';
|
2021-03-16 04:54:22 +00:00
|
|
|
|
2022-03-26 14:52:54 +00:00
|
|
|
const editableProps = () => ({
|
|
|
|
prefixCls: String,
|
|
|
|
value: String,
|
|
|
|
maxlength: Number,
|
|
|
|
autoSize: { type: [Boolean, Object] as PropType<boolean | AutoSizeType> },
|
|
|
|
onSave: Function as PropType<(val: string) => void>,
|
|
|
|
onCancel: Function as PropType<() => void>,
|
|
|
|
onEnd: Function as PropType<() => void>,
|
|
|
|
onChange: Function as PropType<(val: string) => void>,
|
|
|
|
originContent: String,
|
|
|
|
direction: String as PropType<Direction>,
|
|
|
|
});
|
|
|
|
export type EditableProps = Partial<ExtractPropTypes<ReturnType<typeof editableProps>>>;
|
2021-03-16 04:54:22 +00:00
|
|
|
const Editable = defineComponent({
|
2022-09-26 13:33:41 +00:00
|
|
|
compatConfig: { MODE: 3 },
|
2021-06-19 07:49:39 +00:00
|
|
|
name: 'Editable',
|
2022-03-26 14:52:54 +00:00
|
|
|
props: editableProps(),
|
|
|
|
// emits: ['save', 'cancel', 'end', 'change'],
|
2022-03-12 01:56:32 +00:00
|
|
|
setup(props, { emit, slots }) {
|
2021-03-16 04:54:22 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
function confirmChange() {
|
|
|
|
emit('save', state.current.trim());
|
|
|
|
}
|
2022-03-12 01:56:32 +00:00
|
|
|
const textAreaClassName = computed(() => ({
|
|
|
|
[`${props.prefixCls}`]: true,
|
|
|
|
[`${props.prefixCls}-edit-content`]: true,
|
|
|
|
[`${props.prefixCls}-rtl`]: props.direction === 'rtl',
|
|
|
|
}));
|
2021-03-16 04:54:22 +00:00
|
|
|
return () => (
|
2022-03-12 01:56:32 +00:00
|
|
|
<div class={textAreaClassName.value}>
|
2021-03-16 04:54:22 +00:00
|
|
|
<TextArea
|
|
|
|
ref={saveTextAreaRef}
|
|
|
|
maxlength={props.maxlength}
|
|
|
|
value={state.current}
|
2022-03-26 14:52:54 +00:00
|
|
|
onChange={onChange as ChangeEventHandler}
|
2021-03-16 04:54:22 +00:00
|
|
|
onKeydown={onKeyDown}
|
|
|
|
onKeyup={onKeyUp}
|
|
|
|
onCompositionstart={onCompositionStart}
|
|
|
|
onCompositionend={onCompositionEnd}
|
|
|
|
onBlur={onBlur}
|
2022-03-12 01:56:32 +00:00
|
|
|
rows={1}
|
2021-03-16 04:54:22 +00:00
|
|
|
autoSize={props.autoSize === undefined || props.autoSize}
|
|
|
|
/>
|
2022-03-12 01:56:32 +00:00
|
|
|
{slots.enterIcon ? (
|
|
|
|
slots.enterIcon({ className: `${props.prefixCls}-edit-content-confirm` })
|
|
|
|
) : (
|
|
|
|
<EnterOutlined class={`${props.prefixCls}-edit-content-confirm`} />
|
|
|
|
)}
|
2021-03-16 04:54:22 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export default Editable;
|