-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(ui-common): prevent null values for string cells in HandsonTable component #1678
Conversation
Bug Fixes
ContributorsCommit-Lint commandsYou can trigger Commit-Lint actions by commenting on this PR:
|
02728f8
to
0432b24
Compare
0432b24
to
70684f0
Compare
@@ -48,7 +48,7 @@ function Table(props: TableProps) { | |||
const handleAfterChange: HandsontableProps["afterChange"] = | |||
function afterChange(this: unknown, changes, ...rest): void { | |||
changes?.forEach(([row, column, _, nextValue]) => { | |||
setValue(`${data[row].id}.${column}`, nextValue); | |||
setValue(`${data[row].id}.${column}`, nextValue ?? ""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. Like I said, nextValue
is not necessary a string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct me if I am wrong but null
value is possible only on strings. when you press delete on booleans true
or false
only is possible. on numbers inputs this behavior is disabled.
So I dont see in what case we can have an other type than string that is null
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EDIT:
afterChange
event in Handsontable provides the previous value of the cell prevValue
, in addition to nextValue
. We can leverage this to determine if the cell was originally a string
. If it was, and if nextValue
is null
, we can then replace it with an empty string. This ensures that we only replace null with "" for cells that were originally strings.
const handleAfterChange: HandsontableProps["afterChange"] =
function afterChange(this: unknown, changes, ...rest): void {
changes?.forEach(([row, column, prevValue, nextValue]) => {
if (typeof prevValue === "string" && nextValue === null) {
setValue(`${data[row].id}.${column}`, "");
} else {
setValue(`${data[row].id}.${column}`, nextValue);
}
});
restProps.afterChange?.call(this, changes, ...rest);
};
Tested, it works as expected, I dont see a better solution if this does not suits you I let you provide a better one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use beforeChange
instead, because is called before re-render.
const handleBeforeChange: HandsontableProps["beforeChange"] =
function beforeChange(this: unknown, changes, ...rest): void {
changes.forEach(([row, prop, oldValue, newValue]) => {
if (restProps.allowEmpty && typeof oldValue === "string" && newValue === null) {
// You must mutate the value
changes[row][3] = '';
}
});
restProps.beforeChange?.call(this, changes, ...rest);
}
Add it to the HandsonTable component.
70684f0
to
19ce885
Compare
19ce885
to
96078ed
Compare
96078ed
to
07f3566
Compare
07f3566
to
1c32d90
Compare
1c32d90
to
07f3566
Compare
07f3566
to
e9eb98c
Compare
fix #1663
-This Pull Request addresses an issue in the TableMode, when you press the delete button, the default behavior is to set the cell value to
null
. This behavior is now modified so that the cell's value is set to an empty string (""
) instead.Note: only string inputs are concerned