Skip to content

Commit

Permalink
Flip prop-attrs definition
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Dec 17, 2019
1 parent 5359bd7 commit 8dc9452
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
15 changes: 7 additions & 8 deletions extensions/amp-date-display/0.2/amp-date-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,13 @@ const AmpDateDisplay = PreactBaseElement(AmpDateDisplayComponent, {
'templates': win => Services.templatesFor(win),
},

attrs: {
'display-in': {prop: 'displayIn'},
'offset-seconds': {prop: 'offsetSeconds', type: 'number'},
'locale': {prop: 'locale'},
'datetime': {prop: 'datetime'},
'timestamp-ms': {prop: 'timestampMilliseconds', type: 'number'},
'timestamp-seconds': {prop: 'timestampSeconds', type: 'number'},
'template': {prop: 'displayIn'},
props: {
'displayIn': {attr: 'display-in'},
'offsetSeconds': {attr: 'offset-seconds', type: 'number'},
'locale': {attr: 'locale'},
'datetime': {attr: 'datetime'},
'timestampMs': {attr: 'timestamp-milliseconds', type: 'number'},
'timestampSeconds': {attr: 'timestamp-seconds', type: 'number'},
},

/** @override */
Expand Down
20 changes: 9 additions & 11 deletions src/preact-base-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ import {withAmpContext} from './preact/context';

/**
* @typedef {{
* prop: string,
* attr: string,
* type: (string|undefined),
* default: *,
* type: (string|undefined)
* }}
*/
export let AmpElementAttr;
export let AmpElementProp;

/**
* @typedef {{
* className: (string|undefined),
* attrs: (!Object<string, !AmpElementAttr>|undefined),
* props: (!Object<string, !AmpElementProp>|undefined),
* }}
*/
export let AmpElementOptions;
Expand Down Expand Up @@ -208,15 +208,13 @@ function collectProps(element, opts, win, ampdoc) {
props['className'] = opts.className;
}

// Attributes.
const defs = opts.attrs || {};
// Props.
const defs = opts.props || {};
for (const name in defs) {
const def = defs[name];
const value = element.getAttribute(name);
const value = element.getAttribute(def.attr);
if (value == null) {
if (def.default != null) {
props[def.prop] = def.default;
}
props[name] = def.default;
} else {
const v =
def.type == 'number'
Expand All @@ -226,7 +224,7 @@ function collectProps(element, opts, win, ampdoc) {
// React and AMP? Currently modeled as a Ref.
{current: element.getRootNode().getElementById(value)}
: value;
props[def.prop] = v;
props[name] = v;
}
}

Expand Down

0 comments on commit 8dc9452

Please sign in to comment.