Skip to content

Commit

Permalink
Enable metadata to be attached to a handle
Browse files Browse the repository at this point in the history
  • Loading branch information
nlively committed Nov 6, 2019
1 parent 6a075ca commit 54b35ed
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ node_modules
npm-debug.log*
yarn-debug.log*
yarn-error.log*

.idea/
12 changes: 6 additions & 6 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"build/dist/react-compound-slider.js": {
"bundled": 78853,
"minified": 27782,
"gzipped": 8687
"bundled": 79012,
"minified": 27863,
"gzipped": 8712
},
"build/dist/react-compound-slider.min.js": {
"bundled": 12494,
"minified": 12472,
"gzipped": 4297
"bundled": 12571,
"minified": 12549,
"gzipped": 4325
}
}
2 changes: 1 addition & 1 deletion src/Slider/Slider.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface SliderProps {
mode?: number | SliderModeFunction;
step?: number;
domain?: ReadonlyArray<number>;
values: ReadonlyArray<number>;
values: ReadonlyArray<any>;
vertical?: boolean;
reversed?: boolean;
disabled?: boolean;
Expand Down
13 changes: 8 additions & 5 deletions src/Slider/Slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ const isBrowser =

const noop = () => {}

const compare = b => (m, d, i) => m && b[i] === d

const equal = (a, b) => {
return a === b || (a.length === b.length && a.reduce(compare(b), true))
return JSON.stringify(a) === JSON.stringify(b)
}

const getNextValue = (curr, step, domain, reversed) => {
Expand Down Expand Up @@ -472,8 +470,13 @@ class Slider extends PureComponent {
},
} = this

const mappedHandles = handles.map(({ key, val }) => {
return { id: key, value: val, percent: valueToPerc.getValue(val) }
const mappedHandles = handles.map(({ key, val, metadata }) => {
return {
id: key,
value: val,
percent: valueToPerc.getValue(val),
metadata,
}
})

const children = React.Children.map(this.props.children, child => {
Expand Down
8 changes: 5 additions & 3 deletions src/Slider/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ export function getHandles(values = [], reversed, valueToStep, warn) {
let changes = 0

const handles = values
.map(x => {
.map(item => {
const x = (typeof item === 'object') ? item.value : item
const metadata = (typeof item === 'object') ? item : {}
const val = valueToStep.getValue(x)

if (x !== val) {
Expand All @@ -76,9 +78,9 @@ export function getHandles(values = [], reversed, valueToStep, warn) {
)
}

return val
return { val, metadata };
})
.map((val, i) => ({ key: `$$-${i}`, val }))
.map(({ val, metadata }, i) => ({ key: `$$-${i}`, val, metadata }))
.sort(getSortByVal(reversed))

return { handles, changes }
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface SliderItem {
id: string;
value: number;
percent: number;
metadata: Object;
}

export interface EventData {
Expand Down

0 comments on commit 54b35ed

Please sign in to comment.