From 1151cfbbe5599613ab3c2c4173775cf54a1a4d77 Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 6 Jul 2019 22:28:06 -0400 Subject: [PATCH] feat(attributes-to-props): check for overloaded boolean values For HTML DOM attributes that can be either boolean or string, make sure to convert the value correctly for React. Add test to confirm `download` attribute is properly converted. --- lib/attributes-to-props.js | 10 +++++----- test/attributes-to-props.js | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/attributes-to-props.js b/lib/attributes-to-props.js index 3a604917..81fe3a2d 100644 --- a/lib/attributes-to-props.js +++ b/lib/attributes-to-props.js @@ -33,11 +33,11 @@ function attributesToProps(attributes) { // convert HTML attribute to React prop property = HTMLDOMPropertyConfig[attributeName.toLowerCase()]; if (property) { - if (property.hasBooleanValue) { - props[property.propertyName] = true; - } else { - props[property.propertyName] = attributeValue; - } + props[property.propertyName] = + property.hasBooleanValue || + (property.hasOverloadedBooleanValue && !attributeValue) + ? true + : attributeValue; continue; } diff --git a/test/attributes-to-props.js b/test/attributes-to-props.js index cf62005f..db5e7d71 100644 --- a/test/attributes-to-props.js +++ b/test/attributes-to-props.js @@ -109,6 +109,26 @@ describe('attributesToProps', () => { } ); }); + + it('converts overloaded boolean attributes', () => { + assert.deepEqual( + attributesToProps({ + download: '' + }), + { + download: true + } + ); + + assert.deepEqual( + attributesToProps({ + download: 'filename' + }), + { + download: 'filename' + } + ); + }); }); describe('SVG', () => {