Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable values array to contain objects with metadata #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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