Skip to content

Commit 0af9f61

Browse files
committed
[untested] Props will be handed to <Element>
1 parent fe227f9 commit 0af9f61

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

source/helpers.jsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,14 @@ export function addToDate(date, add) {
1616
export function subtractFromDate(date, subtract) {
1717
return Object.keys(subtract).reduce((updatedDate, key) => updatedDate.subtract(subtract[key], key), date)
1818
}
19+
20+
// from https://github.com/headzoo/react-moment/blob/240ee62cb969f341e9e081ca6fa94d85312b8ede/src/objects.js#L26-L34
21+
export function objectKeyFilter(obj1, obj2) {
22+
const obj2Keys = Object.keys(obj2)
23+
const newProps = Object.assign({}, obj1)
24+
Object.keys(newProps)
25+
.filter(key => obj2Keys.indexOf(key) !== -1)
26+
.forEach(key => delete newProps[key])
27+
28+
return newProps
29+
}

source/index.jsx

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,30 @@ import {
77
addToDate,
88
generateInitialDate,
99
subtractFromDate,
10+
objectKeyFilter
1011
} from "./helpers"
1112

13+
const propTypes = {
14+
element: PropTypes.any,
15+
date: PropTypes.oneOfType([
16+
PropTypes.string,
17+
PropTypes.number,
18+
PropTypes.array,
19+
PropTypes.object,
20+
]),
21+
format: PropTypes.string,
22+
toJSON: PropTypes.bool,
23+
toISOString: PropTypes.bool,
24+
asString: PropTypes.bool,
25+
unixSeconds: PropTypes.bool,
26+
unixMilliseconds: PropTypes.bool,
27+
daysInMonth: PropTypes.bool,
28+
displayIsValid: PropTypes.bool,
29+
add: PropTypes.object,
30+
subtract: PropTypes.object,
31+
children: PropTypes.string,
32+
}
33+
1234
const DayJS = (props) => {
1335
const [state, setState] = useState({
1436
value: "",
@@ -106,30 +128,15 @@ const DayJS = (props) => {
106128
update(props)
107129
}, [])
108130

109-
const Element = props.element
110-
return <Element>{state.value}</Element>
131+
const elementProps = objectKeyFilter(props, propTypes)
132+
return React.createElement(
133+
props.element,
134+
elementProps,
135+
state.value
136+
)
111137
}
112138

113-
DayJS.propTypes = {
114-
element: PropTypes.any,
115-
date: PropTypes.oneOfType([
116-
PropTypes.string,
117-
PropTypes.number,
118-
PropTypes.array,
119-
PropTypes.object,
120-
]),
121-
format: PropTypes.string,
122-
toJSON: PropTypes.bool,
123-
toISOString: PropTypes.bool,
124-
asString: PropTypes.bool,
125-
unixSeconds: PropTypes.bool,
126-
unixMilliseconds: PropTypes.bool,
127-
daysInMonth: PropTypes.bool,
128-
displayIsValid: PropTypes.bool,
129-
add: PropTypes.object,
130-
subtract: PropTypes.object,
131-
children: PropTypes.string,
132-
}
139+
DayJS.propTypes = propTypes
133140

134141
DayJS.defaultProps = {
135142
element: "time",

0 commit comments

Comments
 (0)