Skip to content

Commit

Permalink
fix(sheets-data-validation): data validation daily bugifx (#2126)
Browse files Browse the repository at this point in the history
* fix: plain text dropdown click event

* feat: support render offset

* fix: #2121

* fix: close #2120

* fix: #2114

* fix: #2112

* feat: update

* feat: update

* feat: skip filter render
  • Loading branch information
weird94 authored Apr 30, 2024
1 parent 49c1a70 commit 3b45f89
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 51 deletions.
5 changes: 3 additions & 2 deletions examples/src/data/sheets/demo/default-workbook-data-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ const dataValidation = [
startColumn: 3,
endColumn: 5,
}],
operator: DataValidationOperator.GREATER_THAN,
formula1: '100',
operator: DataValidationOperator.NOT_BETWEEN,
formula1: '2024/04/10',
formula2: '2024/10/10',
errorStyle: DataValidationErrorStyle.STOP,
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class Custom extends SheetExtension {
};

// current cell is hidden
if (!worksheet.getColVisible(col) || !worksheet.getRowRawVisible(row)) {
if (!worksheet.getColVisible(col) || !worksheet.getRowVisible(row)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class Marker extends SheetExtension {

override uKey: string = UNIQUE_KEY;

// eslint-disable-next-line max-lines-per-function
override draw(ctx: UniverRenderingContext, parentScale: IScale, skeleton: SpreadsheetSkeleton, diffRanges?: IRange[] | undefined): void {
const { worksheet, rowColumnSegment } = skeleton;
if (!worksheet) {
Expand All @@ -42,6 +43,7 @@ export class Marker extends SheetExtension {

const mergeCellRendered = new Set<string>();

// eslint-disable-next-line max-lines-per-function
Range.foreach(rowColumnSegment, (row, col) => {
let cellData = worksheet.getCell(row, col);
const cellInfo = this.getCellIndex(
Expand Down Expand Up @@ -84,7 +86,7 @@ export class Marker extends SheetExtension {
}

// current cell is hidden
if (!worksheet.getColVisible(col) || !worksheet.getRowRawVisible(row)) {
if (!worksheet.getColVisible(col) || !worksheet.getRowVisible(row)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class DataValidationAlertController extends Disposable {
this._cellAlertManagerService.showAlert({
type: CellAlertType.ERROR,
title: this._localeService.t('dataValidation.error.title'),
message: validator?.generateRuleErrorMessage(rule),
message: validator?.getRuleFinalError(rule),
location: cellPos.location,
width: 200,
height: 74,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class DataValidationRejectInputController extends Disposable {
},
id: 'reject-input-dialog',
children: {
title: validator.generateRuleErrorMessage(rule),
title: validator.getRuleFinalError(rule),
},
footer: {
title: React.createElement(
Expand All @@ -99,4 +99,30 @@ export class DataValidationRejectInputController extends Disposable {
}
);
}

showReject(title: string) {
this._dialogService.open({
width: 368,
title: {
title: this._localeService.t('dataValidation.alert.title'),
},
id: 'reject-input-dialog',
children: {
title,
},
footer: {
title: React.createElement(
Button,
{
type: 'primary',
onClick: () => this._dialogService.close('reject-input-dialog'),
},
this._localeService.t('dataValidation.alert.ok')
),
},
onClose: () => {
this._dialogService.close('reject-input-dialog');
},
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class DateValidator extends BaseDataValidator<Dayjs> {
if (!date1 || !date2) {
return false;
}
const min = date1.isAfter(date2) ? date1 : date2;
const min = date1.isAfter(date2) ? date2 : date1;
const max = min === date1 ? date2 : date1;
return cellValue.isBefore(min) || cellValue.isAfter(max);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,9 @@ export class DecimalValidator extends BaseDataValidator<number> {
if (Number.isNaN(formula1) || Number.isNaN(formula2)) {
return true;
}

const start = Math.min(formula1, formula2);
const end = Math.max(formula1, formula2);
return cellInfo.value < start && cellInfo.value > end;
return cellInfo.value < start || cellInfo.value > end;
}

override async validatorIsGreaterThan(cellInfo: IValidatorCellInfo<number>, formula: IFormulaResult, _rule: IDataValidationRule) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class TextLengthValidator extends BaseDataValidator<number> {
const max = Math.max(formula1, formula2);
const min = Math.min(formula1, formula2);

return cellValue >= min && cellValue <= max;
return cellValue < min || cellValue > max;
}

override async validatorIsGreaterThan(cellInfo: IValidatorCellInfo<number>, formula: IFormulaResult, rule: IDataValidationRule): Promise<boolean> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class WholeValidator extends BaseDataValidator<number> {

const start = Math.min(formula1, formula2);
const end = Math.max(formula1, formula2);
return cellInfo.value < start && cellInfo.value > end;
return cellInfo.value < start || cellInfo.value > end;
}

override async validatorIsGreaterThan(cellInfo: IValidatorCellInfo<number>, formula: IFormulaResult, _rule: IDataValidationRule) {
Expand Down
51 changes: 33 additions & 18 deletions packages/sheets-data-validation/src/views/date-dropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@
*/

import React, { useState } from 'react';
import { ICommandService } from '@univerjs/core';
import { DataValidationErrorStyle, ICommandService } from '@univerjs/core';
import { useDependency } from '@wendellhu/redi/react-bindings';
import { DatePanel } from '@univerjs/design';
import { SetRangeValuesCommand } from '@univerjs/sheets';
import dayjs from 'dayjs';
import type { IDropdownComponentProps } from '../../services/dropdown-manager.service';
import type { DateValidator } from '../../validators';
import { getCellValueOrigin } from '../../utils/get-cell-data-origin';
import { DataValidationRejectInputController } from '../../controllers/dv-reject-input.controller';
import styles from './index.module.less';

export function DateDropdown(props: IDropdownComponentProps) {
const { location, hideFn } = props;
const { worksheet, row, col, unitId, subUnitId } = location;
const commandService = useDependency(ICommandService);
const rejectInputController = useDependency(DataValidationRejectInputController);
const [localDate, setLocalDate] = useState<dayjs.Dayjs>();
if (!worksheet) {
return null;
Expand All @@ -50,25 +52,38 @@ export function DateDropdown(props: IDropdownComponentProps) {
<div className={styles.dvDateDropdown}>
<DatePanel
pickerValue={localDate ?? date}
onSelect={(newValue) => {
onSelect={async (newValue) => {
const newValueStr = newValue.format('YYYY/MM/DD');
commandService.executeCommand(SetRangeValuesCommand.id, {
unitId,
subUnitId,
range: {
startColumn: col,
endColumn: col,
startRow: row,
endRow: row,
},
value: {
v: newValueStr,
p: null,
f: null,
si: null,
if (
rule.errorStyle !== DataValidationErrorStyle.STOP ||
(await validator.validator({
value: newValueStr,
unitId,
subUnitId,
row,
column: col,
}, rule))
) {
commandService.executeCommand(SetRangeValuesCommand.id, {
unitId,
subUnitId,
range: {
startColumn: col,
endColumn: col,
startRow: row,
endRow: row,
},
value: {
v: newValueStr,
p: null,
f: null,
si: null,

},
});
},
});
} else {
rejectInputController.showReject(validator.getRuleFinalError(rule));
}
hideFn();
}}
onPanelChange={(value) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ export function ListFormulaInput(props: IFormulaInputProps) {
};

useEffect(() => {
if (isFormulaStr === '1') {
return;
}
const labelSet = new Set<string>();
const finalList: { color: string; label: string }[] = [];
strList.map((item) => {
Expand All @@ -271,7 +274,7 @@ export function ListFormulaInput(props: IFormulaInputProps) {
formula1: serializeListOptions(finalList.map((item) => item.label)),
formula2: finalList.map((item) => item.color === DROP_DOWN_DEFAULT_COLOR ? '' : item.color).join(','),
});
}, [strList, onChange]);
}, [strList, onChange, isFormulaStr, formulaStrCopy, refColors]);

const updateFormula = useMemo(
() =>
Expand All @@ -296,7 +299,19 @@ export function ListFormulaInput(props: IFormulaInputProps) {
return (
<>
<FormLayout label={localeService.t('dataValidation.list.options')}>
<RadioGroup value={isFormulaStr} onChange={(v) => setIsFormulaStr(v as string)}>
<RadioGroup
value={isFormulaStr}
onChange={(v) => {
setIsFormulaStr(v as string);
setFormulaStr(formulaStrCopy);
if (v === '1') {
onChange({
formula1: formulaStrCopy === '=' ? '' : formulaStrCopy,
formula2: refColors.join(','),
});
}
}}
>
<Radio value="0">{localeService.t('dataValidation.list.customOptions')}</Radio>
<Radio value="1">{localeService.t('dataValidation.list.refOptions')}</Radio>
</RadioGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,27 @@ export class DropdownMultipleWidget implements IBaseDataValidationWidget {
// eslint-disable-next-line max-lines-per-function
drawWith(ctx: UniverRenderingContext2D, info: ICellRenderContext, skeleton: SpreadsheetSkeleton, spreadsheets: Spreadsheet): void {
const { primaryWithCoord, row, col, style, data, subUnitId } = info;
const cellBounding = primaryWithCoord.isMergedMainCell ? primaryWithCoord.mergeInfo : primaryWithCoord;
const validation = data.dataValidation;
const cellWidth = cellBounding.endX - cellBounding.startX;
const cellHeight = cellBounding.endY - cellBounding.startY;
const _cellBounding = primaryWithCoord.isMergedMainCell ? primaryWithCoord.mergeInfo : primaryWithCoord;

// @ts-ignore
const fontRenderExtension = data.fontRenderExtension as ISheetFontRenderExtension['fontRenderExtension'];
const { leftOffset = 0, rightOffset = 0, topOffset = 0, downOffset = 0 } = fontRenderExtension || {};

const validation = data.dataValidation;
const map = this._ensureMap(subUnitId);
const key = this._generateKey(row, col);

if (!validation) {
return;
}
const cellBounding = {
startX: _cellBounding.startX + leftOffset,
endX: _cellBounding.endX - rightOffset,
startY: _cellBounding.startY + topOffset,
endY: _cellBounding.endY - downOffset,
};
const cellWidth = cellBounding.endX - cellBounding.startX;
const cellHeight = cellBounding.endY - cellBounding.startY;
const { cl } = style || {};
const color = (typeof cl === 'object' ? cl?.rgb : cl) ?? '#000';
const fontStyle = getFontStyleString(style ?? undefined);
Expand Down Expand Up @@ -157,7 +167,16 @@ export class DropdownMultipleWidget implements IBaseDataValidationWidget {

calcCellAutoHeight(info: ICellRenderContext): number | undefined {
const { primaryWithCoord, style, data } = info;
const cellBounding = primaryWithCoord.isMergedMainCell ? primaryWithCoord.mergeInfo : primaryWithCoord;
// @ts-ignore
const fontRenderExtension = data.fontRenderExtension as ISheetFontRenderExtension['fontRenderExtension'];
const { leftOffset = 0, rightOffset = 0, topOffset = 0, downOffset = 0 } = fontRenderExtension || {};
const _cellBounding = primaryWithCoord.isMergedMainCell ? primaryWithCoord.mergeInfo : primaryWithCoord;
const cellBounding = {
startX: _cellBounding.startX + leftOffset,
endX: _cellBounding.endX - rightOffset,
startY: _cellBounding.startY + topOffset,
endY: _cellBounding.endY - downOffset,
};
const validation = data.dataValidation;
if (!validation) {
return undefined;
Expand Down
Loading

0 comments on commit 3b45f89

Please sign in to comment.