Skip to content

Commit 56358bb

Browse files
authored
Props will be handed to <Element> (#24)
* [untested] Props will be handed to <Element> * Added tests for hand-down of props to element Co-authored-by: Michael Lohmann <[email protected]>
1 parent a4ce353 commit 56358bb

File tree

3 files changed

+55
-22
lines changed

3 files changed

+55
-22
lines changed

source/helpers.jsx

+11
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

+29-22
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",

test/index.test.jsx

+15
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ describe("react-dayjs element", () => {
5353
const { container } = render(<DayJS date={ DATE_STRING } element="span"/>)
5454
expect(container.firstChild.tagName).toEqual("SPAN")
5555
})
56+
57+
it("className should be handed to element", () => {
58+
const { container } = render(<DayJS
59+
date={ DATE_STRING }
60+
className="myClassName"/>)
61+
expect(container.firstChild.className).toEqual("myClassName")
62+
})
63+
64+
it("style should be handed to element", () => {
65+
const { container } = render(<DayJS
66+
date={ DATE_STRING }
67+
style={ { fontSize: 30 } }
68+
element="h1"/>)
69+
expect(container.firstChild.style._values).toEqual({ "font-size": "30px" })
70+
})
5671
})
5772

5873
describe("react-dayjs manipulation", () => {

0 commit comments

Comments
 (0)