@oslokommune/punkt-react 15.2.0 → 15.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oslokommune/punkt-react",
3
- "version": "15.2.0",
3
+ "version": "15.2.1",
4
4
  "description": "React komponentbibliotek til Punkt, et designsystem laget av Oslo Origo",
5
5
  "homepage": "https://punkt.oslo.kommune.no",
6
6
  "author": "Team Designsystem, Oslo Origo",
@@ -109,5 +109,5 @@
109
109
  "url": "https://github.com/oslokommune/punkt/issues"
110
110
  },
111
111
  "license": "MIT",
112
- "gitHead": "67c9a3c4ede793c0e6bf1e4611ae29d00cf072ee"
112
+ "gitHead": "752427d52a7f6ace1d025a726cf9199d2aeaa240"
113
113
  }
@@ -1,5 +1,6 @@
1
1
  import '@testing-library/jest-dom'
2
2
 
3
+ import { type ChangeEvent, useState } from 'react'
3
4
  import { fireEvent, render, waitFor } from '@testing-library/react'
4
5
 
5
6
  import { IPktDatepicker, PktDatepicker } from './Datepicker'
@@ -196,7 +197,7 @@ describe('PktDatepicker', () => {
196
197
  expect(separator).not.toBeInTheDocument()
197
198
  })
198
199
 
199
- test('does not dispatch onValueChange for incomplete range (only from)', () => {
200
+ test('dispatches onValueChange for partial range (only from)', () => {
200
201
  const handleValueChange = jest.fn()
201
202
  const { container } = createDatepickerTest({
202
203
  range: true,
@@ -206,7 +207,7 @@ describe('PktDatepicker', () => {
206
207
  const inputs = container.querySelectorAll('input[type="date"]') as NodeListOf<HTMLInputElement>
207
208
  fireEvent.change(inputs[0], { target: { value: '2024-06-15' } })
208
209
 
209
- expect(handleValueChange).not.toHaveBeenCalled()
210
+ expect(handleValueChange).toHaveBeenCalledWith(['2024-06-15'])
210
211
  })
211
212
 
212
213
  test('dispatches onValueChange when both range dates are present', () => {
@@ -259,6 +260,41 @@ describe('PktDatepicker', () => {
259
260
  const toInput = container.querySelector(`#${datePickerId}-to`) as HTMLInputElement
260
261
  expect(toInput).toHaveAttribute('aria-label', `${label} Til`)
261
262
  })
263
+
264
+ test('controlled range with onChange updates value via useState', async () => {
265
+ function ControlledRange() {
266
+ const [val, setVal] = useState('2024-06-15,2024-06-25')
267
+ return (
268
+ <>
269
+ <PktDatepicker
270
+ id="range-controlled"
271
+ label="Range"
272
+ range
273
+ value={val}
274
+ onChange={(e: ChangeEvent<HTMLInputElement>) => setVal(e.target.value)}
275
+ />
276
+ <span data-testid="output">{val}</span>
277
+ </>
278
+ )
279
+ }
280
+
281
+ const { container, getByTestId } = render(<ControlledRange />)
282
+ const inputs = container.querySelectorAll('input[type="date"]') as NodeListOf<HTMLInputElement>
283
+
284
+ // Change the "from" date
285
+ fireEvent.change(inputs[0], { target: { value: '2024-06-10' } })
286
+
287
+ await waitFor(() => {
288
+ expect(getByTestId('output').textContent).toBe('2024-06-10,2024-06-25')
289
+ })
290
+
291
+ // Change the "to" date
292
+ fireEvent.change(inputs[1], { target: { value: '2024-06-30' } })
293
+
294
+ await waitFor(() => {
295
+ expect(getByTestId('output').textContent).toBe('2024-06-10,2024-06-30')
296
+ })
297
+ })
262
298
  })
263
299
 
264
300
  describe('Single date selection', () => {
@@ -242,10 +242,8 @@ export function useDatepickerState(
242
242
  const processed = processDateSelection(selected, multiple, range)
243
243
  const newValues = valueToArray(processed)
244
244
 
245
- // In range mode, only fire callbacks when both dates are selected
246
- const shouldNotify = !range || newValues.length === 2
247
- updateValues(newValues, shouldNotify)
248
- if (shouldNotify) dispatchChange(newValues)
245
+ updateValues(newValues)
246
+ dispatchChange(newValues)
249
247
 
250
248
  if (!multiple && !range) {
251
249
  hideCalendar()
@@ -277,9 +275,8 @@ export function useDatepickerState(
277
275
  const fromVal = e.target.value
278
276
  const toVal = values[1] ?? ''
279
277
  const newValues = [fromVal, toVal].filter(Boolean)
280
- const shouldNotify = newValues.length === 2
281
- updateValues(newValues, shouldNotify)
282
- if (shouldNotify) dispatchChange(newValues)
278
+ updateValues(newValues)
279
+ dispatchChange(newValues)
283
280
  },
284
281
  [values, updateValues, dispatchChange],
285
282
  )
@@ -289,9 +286,8 @@ export function useDatepickerState(
289
286
  const fromVal = values[0] ?? ''
290
287
  const toVal = e.target.value
291
288
  const newValues = [fromVal, toVal].filter(Boolean)
292
- const shouldNotify = newValues.length === 2
293
- updateValues(newValues, shouldNotify)
294
- if (shouldNotify) dispatchChange(newValues)
289
+ updateValues(newValues)
290
+ dispatchChange(newValues)
295
291
  },
296
292
  [values, updateValues, dispatchChange],
297
293
  )