| | |
| | | import { CheckOutlined, CloseOutlined, FormOutlined } from '@ant-design/icons-vue'; |
| | | import { CellComponent } from './CellComponent'; |
| | | |
| | | import { useDesign } from '/@/hooks/web/useDesign'; |
| | | import { useDesign } from '@/hooks/web/useDesign'; |
| | | import { useTableContext } from '../../hooks/useTableContext'; |
| | | |
| | | import clickOutside from '/@/directives/clickOutside'; |
| | | import clickOutside from '@/directives/clickOutside'; |
| | | |
| | | import { propTypes } from '/@/utils/propTypes'; |
| | | import { isArray, isBoolean, isFunction, isNumber, isString } from '/@/utils/is'; |
| | | import { propTypes } from '@/utils/propTypes'; |
| | | import { isArray, isBoolean, isFunction, isNumber, isString } from '@/utils/is'; |
| | | import { createPlaceholderMessage } from './helper'; |
| | | import { pick, set } from 'lodash-es'; |
| | | import { treeToList } from '/@/utils/helper/treeHelper'; |
| | | import { treeToList } from '@/utils/helper/treeHelper'; |
| | | import { Spin } from 'ant-design-vue'; |
| | | import { parseRowKey } from '../../helper'; |
| | | |
| | | export default defineComponent({ |
| | | name: 'EditableCell', |
| | |
| | | return ['Checkbox', 'Switch'].includes(component); |
| | | }); |
| | | |
| | | const getDisable = computed(() => { |
| | | const { editDynamicDisabled } = props.column; |
| | | let disabled = false; |
| | | if (isBoolean(editDynamicDisabled)) { |
| | | disabled = editDynamicDisabled; |
| | | } |
| | | if (isFunction(editDynamicDisabled)) { |
| | | const { record } = props; |
| | | disabled = editDynamicDisabled({ record, currentValue: currentValueRef.value }); |
| | | } |
| | | return disabled; |
| | | }); |
| | | |
| | | const getComponentProps = computed(() => { |
| | | const isCheckValue = unref(getIsCheckComp); |
| | | let compProps = props.column?.editComponentProps ?? ({} as any); |
| | | const { checkedValue, unCheckedValue } = compProps; |
| | | |
| | | const valueField = isCheckValue ? 'checked' : 'value'; |
| | | const val = unref(currentValueRef); |
| | | |
| | | const value = isCheckValue ? (isNumber(val) && isBoolean(val) ? val : !!val) : val; |
| | | let value = val; |
| | | if (isCheckValue) { |
| | | if (typeof checkedValue !== 'undefined') { |
| | | value = val === checkedValue ? checkedValue : unCheckedValue; |
| | | } else if (typeof unCheckedValue !== 'undefined') { |
| | | value = val === unCheckedValue ? unCheckedValue : checkedValue; |
| | | } else { |
| | | value = isNumber(val) || isBoolean(val) ? val : !!val; |
| | | } |
| | | } |
| | | |
| | | let compProps = props.column?.editComponentProps ?? ({} as any); |
| | | const { record, column, index } = props; |
| | | |
| | | if (isFunction(compProps)) { |
| | |
| | | const dataKey = (dataIndex || key) as string; |
| | | set(record, dataKey, value); |
| | | } |
| | | const getDisable = computed(() => { |
| | | const { editDynamicDisabled } = props.column; |
| | | let disabled = false; |
| | | if (isBoolean(editDynamicDisabled)) { |
| | | disabled = editDynamicDisabled; |
| | | } |
| | | if (isFunction(editDynamicDisabled)) { |
| | | const { record } = props; |
| | | disabled = editDynamicDisabled({ record }); |
| | | } |
| | | return disabled; |
| | | }); |
| | | |
| | | const getValues = computed(() => { |
| | | const { editValueMap } = props.column; |
| | | |
| | |
| | | return option?.label ?? value; |
| | | }); |
| | | |
| | | const getRowEditable = computed(() => { |
| | | const { editable } = props.record || {}; |
| | | return !!editable; |
| | | }); |
| | | |
| | | const getWrapperStyle = computed((): CSSProperties => { |
| | | if (unref(getIsCheckComp) || unref(getRowEditable)) { |
| | | return {}; |
| | |
| | | return `edit-cell-align-${align}`; |
| | | }); |
| | | |
| | | const getRowEditable = computed(() => { |
| | | const { editable } = props.record || {}; |
| | | return !!editable; |
| | | }); |
| | | |
| | | watchEffect(() => { |
| | | // defaultValueRef.value = props.value; |
| | | currentValueRef.value = props.value; |
| | |
| | | } |
| | | }); |
| | | |
| | | function handleEdit() { |
| | | if (unref(getRowEditable) || unref(props.column?.editRow)) return; |
| | | function handleEdit(e) { |
| | | e.stopPropagation(); |
| | | if (unref(getRowEditable) || unref(props.column?.editRow) || unref(getDisable)) return; |
| | | ruleMessage.value = ''; |
| | | isEdit.value = true; |
| | | nextTick(() => { |
| | |
| | | }); |
| | | } |
| | | |
| | | async function handleChange(e: any) { |
| | | async function handleChange(e: any, ...rest: any[]) { |
| | | const component = unref(getComponent); |
| | | if (!e) { |
| | | currentValueRef.value = e; |
| | |
| | | currentValueRef.value = e; |
| | | } |
| | | const onChange = unref(getComponentProps)?.onChangeTemp; |
| | | if (onChange && isFunction(onChange)) onChange(...arguments); |
| | | if (onChange && isFunction(onChange)) onChange(e, ...rest); |
| | | |
| | | table.emit?.('edit-change', { |
| | | column: props.column, |
| | |
| | | if (!record.editable) { |
| | | const { getBindValues } = table; |
| | | |
| | | const { beforeEditSubmit, columns } = unref(getBindValues); |
| | | const { beforeEditSubmit, columns, rowKey } = unref(getBindValues); |
| | | |
| | | const rowKeyParsed = parseRowKey(rowKey, record); |
| | | |
| | | if (beforeEditSubmit && isFunction(beforeEditSubmit)) { |
| | | spinning.value = true; |
| | | const keys: string[] = columns |
| | | .map((_column) => _column.dataIndex) |
| | | .filter((field) => !!field) as string[]; |
| | | |
| | | let result: any = true; |
| | | try { |
| | | result = await beforeEditSubmit({ |
| | | record: pick(record, keys), |
| | | record: pick(record, [rowKeyParsed, ...keys]), |
| | | index, |
| | | key: dataKey as string, |
| | | value, |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | set(record, dataKey, value); |
| | | defaultValueRef.value = value; |
| | | //const record = await table.updateTableData(index, dataKey, value); |
| | | needEmit && table.emit?.('edit-end', { record, index, key: dataKey, value }); |
| | | isEdit.value = false; |
| | |
| | | if (!props.record.editValueRefs) props.record.editValueRefs = {}; |
| | | props.record.editValueRefs[props.column.dataIndex as any] = currentValueRef; |
| | | } |
| | | /* eslint-disable */ |
| | | props.record.onCancelEdit = () => { |
| | | isArray(props.record?.cancelCbs) && props.record?.cancelCbs.forEach((fn) => fn()); |
| | | }; |
| | | /* eslint-disable */ |
| | | props.record.onSubmitEdit = async () => { |
| | | if (isArray(props.record?.submitCbs)) { |
| | | if (!props.record?.onValid?.()) return; |
| | |
| | | handleEnter, |
| | | handleSubmitClick, |
| | | spinning, |
| | | getDisable, |
| | | }; |
| | | }, |
| | | render() { |
| | |
| | | record: this.record as Recordable, |
| | | column: this.column, |
| | | index: this.index, |
| | | currentValue: this.currentValueRef, |
| | | }) |
| | | : this.getValues ?? '\u00A0'} |
| | | </div> |
| | | {!this.column.editRow && <FormOutlined class={`${this.prefixCls}__normal-icon`} />} |
| | | {!this.column.editRow && !this.getDisable && ( |
| | | <FormOutlined class={`${this.prefixCls}__normal-icon`} /> |
| | | )} |
| | | </div> |
| | | {this.isEdit && ( |
| | | <Spin spinning={this.spinning}> |
| | | <div class={`${this.prefixCls}__wrapper`} v-click-outside={this.onClickOutside}> |
| | | <Spin spinning={this.spinning} onClick={(e) => e.stopPropagation()}> |
| | | <div |
| | | class={`${this.prefixCls}__wrapper`} |
| | | v-click-outside={this.onClickOutside} |
| | | onClick={(e) => e.stopPropagation()} |
| | | > |
| | | <CellComponent |
| | | {...this.getComponentProps} |
| | | component={this.getComponent} |
| | |
| | | } |
| | | .@{prefix-cls} { |
| | | position: relative; |
| | | min-height: 24px; // 设置高度让其始终可被hover |
| | | |
| | | &__wrapper { |
| | | display: flex; |