Skip to content

Commit f58aa96

Browse files
authored
Merge pull request #44 from quicoto/set-custom-attribute-value
feat: set custom attribute and the value on setting value to proxy
2 parents 1d73c5d + 3487e4b commit f58aa96

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,13 @@ export default (config = {}) => {
172172
if (typeof $elements === `undefined` || value === null) return;
173173

174174
$elements.forEach(($element) => {
175-
if (hasCustomValue($element)) return;
175+
if (hasCustomValue($element)) {
176+
const attr = propertyToGet($element);
177+
178+
$element.setAttribute(attr, value);
179+
180+
return;
181+
}
176182

177183
if ($element.tagName === `INPUT`) {
178184
let checked = value !== `undefined` && value === ``;

src/tests/index.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,4 +651,25 @@ describe(`twoWayDataBinding`, () => {
651651

652652
expect(proxy.country).toEqual(`ES`);
653653
});
654+
655+
it(`Update element via proxy with custom value attribute [data-twowayvalue]`, () => {
656+
const {
657+
container,
658+
bindName
659+
} = render(
660+
`<input data-model="country" data-bind="country" data-twowayvalue="ES" type="text" value="Spain (ES)" />`,
661+
`data-bind`
662+
);
663+
664+
const proxy = twoWayDataBinding({
665+
$context: container,
666+
attributesCustomValue: [`data-twowayvalue`]
667+
});
668+
const $input = bindName(`country`, `data-model`);
669+
670+
proxy.country = `Germany (DE)`;
671+
672+
expect($input.getAttribute(`data-twowayvalue`)).toEqual(`Germany (DE)`);
673+
expect($input.value).toEqual(`Spain (ES)`);
674+
});
654675
});

0 commit comments

Comments
 (0)