Skip to content

Commit

Permalink
fix(Slider): make it optional to provide an updated prop value (#1537)
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker authored Aug 31, 2022
1 parent 2f3b82e commit ff1f3b7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ showTabs: true
## Description

The Slider component provides a visual indication of adjustable value. A value can be adjusted (increased or decreased) by moving the drag handle along a track (usually horizontal or vertical). Remember to inform users that they can also adjust the value directly in the value input field (if it exists).

### Define a `min` and `max` value

Keep in mind, you should most probably define your `min` and `max` value, because they are tied closely to your given value property.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ showTabs: true

| Properties | Description |
| ------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `value` | _(required)_ the `value` of the slider as a number. If an array with numbers is provided, each number will represent a thumb button (the `+` and `-` button will be hidden on multible thumbs). |
| `value` | _(required)_ the `value` of the slider as a number or an array. If an array with numbers is provided, each number will represent a thumb button (the `+` and `-` button will be hidden on multible thumbs). |
| `min` | _(optional)_ the minimum value. Defaults to `0`. |
| `max` | _(optional)_ the maximum value. Defaults to `100` |
| `step` | _(optional)_ the steps the slider takes on changing the value. Defaults to `null` |
Expand Down
8 changes: 6 additions & 2 deletions packages/dnb-eufemia/src/components/slider/SliderProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ export function SliderProvider(localProps: SliderProps) {
} = allProps

const [value, setValue] = React.useState<ValueTypes>(_value)
const [externValue, updateExternValue] =
React.useState<ValueTypes>(_value)
const realtimeValue = React.useRef<ValueTypes>(_value)
const [thumbState, setThumbState] =
React.useState<ThumbStateEnums>('initial')
Expand Down Expand Up @@ -215,14 +217,16 @@ export function SliderProvider(localProps: SliderProps) {
React.useEffect(() => {
if (isMulti) {
const hasChanged = (_value as Array<number>).some((val, i) => {
return val !== value[i]
return val !== externValue[i]
})

if (hasChanged) {
updateValue(_value)
updateExternValue(_value)
}
} else {
} else if (_value !== externValue) {
updateValue(_value)
updateExternValue(_value)
}

// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ describe('Slider component', () => {
return <Slider {...props} value={value} onChange={onChangehandler} />
}

const getThumbElements = (index: number) =>
document.querySelectorAll('.dnb-slider__thumb')[index] as HTMLElement

const getRangeElement = (index: number) =>
document.querySelectorAll('[type="range"]')[
index
Expand All @@ -266,6 +269,38 @@ describe('Slider component', () => {
resetMouseSimulation()
})

it('will net need on external prop changes', () => {
const WrongUsage = () => {
const [min, setMinVal] = React.useState(0) //eslint-disable-line
const [max, setMaxVal] = React.useState(200) //eslint-disable-line

return (
<Slider
max={200}
value={[0, 200]} // <-- Here we do not update the value
onChange={({ value }) => {
setMinVal(value[0])
setMaxVal(value[1])
}}
/>
)
}

render(<WrongUsage />)

simulateMouseMove({ pageX: 20, width: 100, height: 10 })
expect(getThumbElements(0).getAttribute('style')).toBe(
'z-index: 4; left: 20%;'
)

simulateMouseMove({ pageX: 80, width: 100, height: 10 })
expect(getThumbElements(1).getAttribute('style')).toBe(
'z-index: 4; left: 80%;'
)

resetMouseSimulation()
})

it('tracks mousemove on track', () => {
const onChange = jest.fn()

Expand Down Expand Up @@ -365,11 +400,6 @@ describe('Slider component', () => {
/>
)

const getThumbElements = (index: number) =>
document.querySelectorAll('.dnb-slider__thumb')[
index
] as HTMLElement

const secondThumb = document.querySelectorAll(
'.dnb-slider__button-helper'
)[1]
Expand Down

0 comments on commit ff1f3b7

Please sign in to comment.