Skip to content

Commit a94e161

Browse files
authored
Merge pull request #4812 from easyops-cn/steve/v3-data-attrs
Steve/v3-data-attrs
2 parents 14446c6 + 0510f06 commit a94e161

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

packages/runtime/src/internal/compute/setRealProperties.spec.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { setValueForStyle } from "./setRealProperties.js";
1+
import { setRealProperties, setValueForStyle } from "./setRealProperties.js";
22

33
describe("setValueForStyle", () => {
44
let mockStyle: CSSStyleDeclaration;
@@ -36,3 +36,18 @@ describe("setValueForStyle", () => {
3636
expect((mockStyle as any).margin).toBe("10px");
3737
});
3838
});
39+
40+
describe("setRealProperties", () => {
41+
test("should set plain data attributes on the element", () => {
42+
const brick = document.createElement("div");
43+
const realProps = {
44+
"data-test": "value1",
45+
"data-info": "value2",
46+
};
47+
48+
setRealProperties(brick, realProps);
49+
50+
expect(brick.getAttribute("data-test")).toBe("value1");
51+
expect(brick.getAttribute("data-info")).toBe("value2");
52+
});
53+
});

packages/runtime/src/internal/compute/setRealProperties.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ export function setRealProperties(
2525
// `innerHTML` is dangerous, use `textContent` instead.
2626
throw new Error(`set \`${propName}\` is prohibited`);
2727
default:
28-
(brick as unknown as Record<string, unknown>)[propName] = propValue;
28+
if (propName.startsWith("data-")) {
29+
brick.setAttribute(propName, String(propValue));
30+
} else {
31+
(brick as unknown as Record<string, unknown>)[propName] = propValue;
32+
}
2933
}
3034
}
3135
}

0 commit comments

Comments
 (0)