Skip to content

Commit 5282261

Browse files
committed
wip shorthand collection keys
1 parent bc3b314 commit 5282261

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

docs/app/Examples/collections/Table/States/TableWarningShorthand.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import React from 'react'
22
import { Table } from 'semantic-ui-react'
33

4+
const tableData = [
5+
{ name: undefined, status: undefined, notes: undefined },
6+
{ name: 'Jimmy', status: 'Requires Action', notes: undefined },
7+
{ name: 'Jamie', status: undefined, notes: 'Hostile' },
8+
{ name: 'Jill', status: undefined, notes: undefined },
9+
]
10+
411
const headerRow = [
512
'Name',
613
'Status',
@@ -20,16 +27,14 @@ const renderBodyRow = ({ name, status, notes }) => ({
2027
],
2128
})
2229

23-
const tableData = [
24-
{ name: undefined, status: undefined, notes: undefined },
25-
{ name: 'Jimmy', status: 'Requires Action', notes: undefined },
26-
{ name: 'Jamie', status: undefined, notes: 'Hostile' },
27-
{ name: 'Jill', status: undefined, notes: undefined },
28-
]
29-
3030
const TableWarningShorthand = () => {
3131
return (
32-
<Table celled headerRow={headerRow} renderBodyRow={renderBodyRow} tableData={tableData} />
32+
<Table
33+
celled
34+
headerRow={headerRow}
35+
// renderBodyRow={renderBodyRow}
36+
tableData={tableData}
37+
/>
3338
)
3439
}
3540

src/lib/factories.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ import _ from 'lodash'
22
import cx from 'classnames'
33
import React, { cloneElement, isValidElement } from 'react'
44

5+
// Simplified and faster version of:
6+
// http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery
7+
const hashCode = function hashCode(str) {
8+
const { length } = str
9+
if (!str || length === 0) return 0
10+
let hash = 0
11+
for (let i = 0; i < length; i++) {
12+
hash = hash * 31 + str.charCodeAt(i) | 0
13+
}
14+
return hash
15+
}
16+
517
/**
618
* Merges props and classNames.
719
*
@@ -16,8 +28,14 @@ const mergePropsAndClassName = (defaultProps, props) => {
1628
newProps.className = cx(defaultProps.className, props.className) // eslint-disable-line react/prop-types
1729
}
1830

19-
if (!newProps.key && childKey) {
20-
newProps.key = _.isFunction(childKey) ? childKey(newProps) : childKey
31+
if (!newProps.key) {
32+
if (childKey) {
33+
newProps.key = _.isFunction(childKey) ? childKey(newProps) : childKey
34+
} else {
35+
const stringified = _.map(newProps, (val, key) => `${key}:${val}`).join('')
36+
console.log(stringified)
37+
newProps.key = hashCode(stringified)
38+
}
2139
}
2240

2341
return newProps
@@ -57,6 +75,10 @@ export function createShorthand(Component, mapValueToProps, val, defaultProps =
5775
defaultProps = _.isFunction(defaultProps) ? defaultProps(usersProps) : defaultProps
5876
const props = mergePropsAndClassName(defaultProps, usersProps)
5977

78+
if (Component._meta.name === 'TableCell') {
79+
console.log(props)
80+
}
81+
6082
// Clone ReactElements
6183
if (type === 'element') {
6284
return cloneElement(val, props)

test/specs/docs/examples-test.js

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ describe.only('examples', () => {
1212
console.error.restore()
1313
})
1414
exampleContext.keys().forEach(path => {
15+
if (!path.includes('TableWarningShorthand')) {
16+
return
17+
}
1518
it(`renders without errors: ${path}`, () => {
1619
mount(createElement(exampleContext(path).default))
1720

0 commit comments

Comments
 (0)