@oslokommune/punkt-react 20.2.0 → 20.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.
@@ -49,6 +49,8 @@ export type { TPktCounterPosition, IPktInputWrapperBase, IInputWrapperSpecProps,
49
49
  export type { TMessageboxSkin } from './skin';
50
50
  export { TAG_SKINS, TAG_STATES, TAG_TEXT_STYLES, TAG_DEFAULTS } from './tag';
51
51
  export type { TTagSkin, TTagState, TTagTextStyle, IPktTagBase, ITagSpecProps } from './tag';
52
+ export { TABS_DEFAULTS, TAB_ITEM_DEFAULTS } from './tabs';
53
+ export type { IPktTab, IPktTabTag, IPktTabsBase, IPktTabItemBase, ITabsSpecProps, ITabItemSpecProps, } from './tabs';
52
54
  export { MESSAGEBOX_SKINS, MESSAGEBOX_LIGHT_ONLY_SKINS, MESSAGEBOX_DEFAULTS } from './messagebox';
53
55
  export type { IPktMessageboxBase, IMessageboxSpecProps } from './messagebox';
54
56
  export { CALENDAR_DEFAULTS } from './calendar';
@@ -15,11 +15,12 @@ import { INPUT_WRAPPER_DEFAULTS as g } from "./input-wrapper.js";
15
15
  import { TEXTAREA_DEFAULTS as _ } from "./textarea.js";
16
16
  import { TEXTINPUT_DEFAULTS as v } from "./textinput.js";
17
17
  import { TAG_DEFAULTS as y } from "./tag.js";
18
- import { MESSAGEBOX_DEFAULTS as b, MESSAGEBOX_LIGHT_ONLY_SKINS as x } from "./messagebox.js";
18
+ import { TABS_DEFAULTS as b, TAB_ITEM_DEFAULTS as x } from "./tabs.js";
19
+ import { MESSAGEBOX_DEFAULTS as S, MESSAGEBOX_LIGHT_ONLY_SKINS as C } from "./messagebox.js";
19
20
  import "./calendar.js";
20
- import { DATEPICKER_DEFAULTS as S } from "./datepicker.js";
21
- import { PROGRESSBAR_DEFAULTS as C } from "./progressbar.js";
21
+ import { DATEPICKER_DEFAULTS as w } from "./datepicker.js";
22
+ import { PROGRESSBAR_DEFAULTS as T } from "./progressbar.js";
22
23
  import "./combobox.js";
23
- import { SELECT_DEFAULTS as w } from "./select.js";
24
- import { TABLE_DEFAULTS as T } from "./table.js";
25
- export { p as ACCORDION_LIGHT_ONLY_SKINS, u as ANIMATION_DEFAULT_PATH, m as BUTTON_DEFAULTS, i as CARD_DEFAULTS, a as CARD_LIGHT_ONLY_SKINS, o as CHECKBOX_DEFAULTS, l as CONSENT_DEFAULTS, S as DATEPICKER_DEFAULTS, n as DEFAULT_FOOTER_PERSONVERN_URL, r as DEFAULT_FOOTER_TILGJENGELIGHET_URL, e as DEFAULT_HEADER_FOOTER_URL, t as DEFAULT_HEADER_LOGO_LINK, d as ICON_DEFAULTS, g as INPUT_WRAPPER_DEFAULTS, h as LINKCARD_LIGHT_ONLY_SKINS, f as LOADER_DEFAULTS, b as MESSAGEBOX_DEFAULTS, x as MESSAGEBOX_LIGHT_ONLY_SKINS, c as OPTION_GROUP_DEFAULTS, C as PROGRESSBAR_DEFAULTS, s as RADIOBUTTON_DEFAULTS, w as SELECT_DEFAULTS, T as TABLE_DEFAULTS, y as TAG_DEFAULTS, _ as TEXTAREA_DEFAULTS, v as TEXTINPUT_DEFAULTS };
24
+ import { SELECT_DEFAULTS as E } from "./select.js";
25
+ import { TABLE_DEFAULTS as D } from "./table.js";
26
+ export { p as ACCORDION_LIGHT_ONLY_SKINS, u as ANIMATION_DEFAULT_PATH, m as BUTTON_DEFAULTS, i as CARD_DEFAULTS, a as CARD_LIGHT_ONLY_SKINS, o as CHECKBOX_DEFAULTS, l as CONSENT_DEFAULTS, w as DATEPICKER_DEFAULTS, n as DEFAULT_FOOTER_PERSONVERN_URL, r as DEFAULT_FOOTER_TILGJENGELIGHET_URL, e as DEFAULT_HEADER_FOOTER_URL, t as DEFAULT_HEADER_LOGO_LINK, d as ICON_DEFAULTS, g as INPUT_WRAPPER_DEFAULTS, h as LINKCARD_LIGHT_ONLY_SKINS, f as LOADER_DEFAULTS, S as MESSAGEBOX_DEFAULTS, C as MESSAGEBOX_LIGHT_ONLY_SKINS, c as OPTION_GROUP_DEFAULTS, T as PROGRESSBAR_DEFAULTS, s as RADIOBUTTON_DEFAULTS, E as SELECT_DEFAULTS, D as TABLE_DEFAULTS, b as TABS_DEFAULTS, x as TAB_ITEM_DEFAULTS, y as TAG_DEFAULTS, _ as TEXTAREA_DEFAULTS, v as TEXTINPUT_DEFAULTS };
@@ -0,0 +1,66 @@
1
+ /**
2
+ * tabs.ts
3
+ *
4
+ * Shared types and defaults for pkt-tabs and pkt-tab-item.
5
+ * Used by React, Elements, and typed component specs.
6
+ *
7
+ * @remarks React also takes a `tabs` array (`IPktTab[]`) instead of tab item
8
+ * children. Elements only takes `pkt-tab-item` children. Elements widens
9
+ * `ariaLabel` to `string | null`, like the native `ariaLabel` property.
10
+ */
11
+ import type { TTagSkin } from './tag';
12
+ /** The tag on a tab in React's `tabs` array. */
13
+ export interface IPktTabTag {
14
+ text: string;
15
+ skin?: TTagSkin;
16
+ }
17
+ /** One tab in React's `tabs` array. */
18
+ export interface IPktTab {
19
+ text: string;
20
+ href?: string;
21
+ action?: (index: number) => void;
22
+ disabled?: boolean;
23
+ icon?: string;
24
+ controls?: string;
25
+ tag?: IPktTabTag;
26
+ active?: boolean;
27
+ }
28
+ /** Tabs props shared by React, Elements, and typed specs. */
29
+ export interface IPktTabsBase {
30
+ arrowNav?: boolean;
31
+ disableArrowNav?: boolean;
32
+ separatorIconName?: string;
33
+ separatorIconPath?: string;
34
+ togglePanels?: boolean;
35
+ ariaLabel?: string;
36
+ ariaLabelledBy?: string;
37
+ }
38
+ /** Tab item props shared by React, Elements, and typed specs. */
39
+ export interface IPktTabItemBase {
40
+ active?: boolean;
41
+ disabled?: boolean;
42
+ href?: string;
43
+ icon?: string;
44
+ controls?: string;
45
+ tag?: string;
46
+ tagSkin?: TTagSkin;
47
+ index?: number;
48
+ }
49
+ /** Props documented in the tabs spec. */
50
+ export type ITabsSpecProps = Pick<IPktTabsBase, 'arrowNav' | 'disableArrowNav' | 'separatorIconName' | 'separatorIconPath' | 'togglePanels' | 'ariaLabelledBy' | 'ariaLabel'> & {
51
+ /** React only. Elements takes `pkt-tab-item` children. */
52
+ tabs?: IPktTab[];
53
+ };
54
+ /** Props documented in the tab item spec. */
55
+ export type ITabItemSpecProps = Pick<IPktTabItemBase, 'active' | 'disabled' | 'href' | 'icon' | 'tag' | 'tagSkin' | 'index' | 'controls'>;
56
+ export declare const TABS_DEFAULTS: {
57
+ readonly arrowNav: true;
58
+ readonly disableArrowNav: false;
59
+ readonly togglePanels: false;
60
+ };
61
+ export declare const TAB_ITEM_DEFAULTS: {
62
+ readonly active: false;
63
+ readonly disabled: false;
64
+ readonly tagSkin: 'blue';
65
+ readonly index: 0;
66
+ };
@@ -0,0 +1,12 @@
1
+ // ../../shared-types/tabs.ts
2
+ var e = {
3
+ arrowNav: !0,
4
+ disableArrowNav: !1,
5
+ togglePanels: !1
6
+ }, t = {
7
+ active: !1,
8
+ disabled: !1,
9
+ tagSkin: "blue",
10
+ index: 0
11
+ };
12
+ export { e as TABS_DEFAULTS, t as TAB_ITEM_DEFAULTS };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oslokommune/punkt-react",
3
- "version": "20.2.0",
3
+ "version": "20.2.1",
4
4
  "description": "React komponentbibliotek til Punkt, designsystem for Oslo kommune",
5
5
  "homepage": "https://punkt.oslo.kommune.no",
6
6
  "author": "Team Designsystem, Oslo kommune",
@@ -102,5 +102,5 @@
102
102
  "url": "https://github.com/oslokommune/punkt/issues"
103
103
  },
104
104
  "license": "MIT",
105
- "gitHead": "6e2e6b2c2801df3e024106db88fe4916f457b93c"
105
+ "gitHead": "fc42bfbbb587bfd13f8a5d0e94e042cfc0d19695"
106
106
  }
@@ -25,7 +25,8 @@ export type { IPktRadioButton } from './radio/RadioButton'
25
25
  export type { IPktRadioGroup } from './radiogroup/RadioGroup'
26
26
  export type { IPktStep } from './stepper/Step'
27
27
  export type { IPktStepper } from './stepper/Stepper'
28
- export type { IPktTab } from './tabs/Tabs'
28
+ export type { IPktTab, IPktTabs } from './tabs/Tabs'
29
+ export type { IPktTabItem } from './tabs/TabItem'
29
30
  export type { IPktTag } from './tag/Tag'
30
31
  export type { IPktTextarea } from './textarea/Textarea'
31
32
  export type { IPktTextinput } from './textinput/Textinput'
@@ -3,27 +3,33 @@
3
3
  import { forwardRef, type KeyboardEvent, type MouseEvent, type ReactNode, useEffect, useRef } from 'react'
4
4
 
5
5
  import { PktIcon } from '../icon/Icon'
6
- import type { TTagSkin } from 'shared-types'
6
+ import { TAB_ITEM_DEFAULTS, type IPktTabItemBase } from 'shared-types'
7
7
  import { syncTabPanel, tabIdFor } from 'shared-utils/tabs/panels'
8
8
 
9
9
  import { PktTag } from '../tag/Tag'
10
10
  import { useTabsContext } from './Tabs'
11
11
 
12
- export interface IPktTabItem {
12
+ export interface IPktTabItem extends IPktTabItemBase {
13
13
  children: ReactNode
14
- active?: boolean
15
- disabled?: boolean
16
- href?: string
17
14
  onClick?: (event: MouseEvent) => void
18
- icon?: string
19
- controls?: string
20
- tag?: string
21
- tagSkin?: TTagSkin
22
- index?: number
23
15
  }
24
16
 
25
17
  export const PktTabItem = forwardRef<HTMLAnchorElement | HTMLButtonElement, IPktTabItem>(
26
- ({ children, active, disabled = false, href, onClick, icon, controls, tag, tagSkin = 'blue', index = 0 }, ref) => {
18
+ (
19
+ {
20
+ children,
21
+ active = TAB_ITEM_DEFAULTS.active,
22
+ disabled = TAB_ITEM_DEFAULTS.disabled,
23
+ href,
24
+ onClick,
25
+ icon,
26
+ controls,
27
+ tag,
28
+ tagSkin = TAB_ITEM_DEFAULTS.tagSkin,
29
+ index = TAB_ITEM_DEFAULTS.index,
30
+ },
31
+ ref,
32
+ ) => {
27
33
  const {
28
34
  arrowNav,
29
35
  togglePanels,
@@ -86,7 +92,7 @@ export const PktTabItem = forwardRef<HTMLAnchorElement | HTMLButtonElement, IPkt
86
92
  tabIndex: disabled ? -1 : isFocusable || !arrowNav ? undefined : -1,
87
93
  ref: (el: HTMLAnchorElement | HTMLButtonElement | null) => {
88
94
  elementRef.current = el
89
- registerTabRef(index, el, disabled)
95
+ registerTabRef(index, el)
90
96
  if (typeof ref === 'function') {
91
97
  ref(el)
92
98
  } else if (ref) {
@@ -288,6 +288,28 @@ describe('PktTabItem children format', () => {
288
288
  expect(getByText('First Tab')).toHaveFocus()
289
289
  })
290
290
 
291
+ it('reaches every tab when a conditional tab is hidden', () => {
292
+ const renderWith = (showMiddle: boolean) => (
293
+ <PktTabs>
294
+ <PktTabItem index={0} active>
295
+ First Tab
296
+ </PktTabItem>
297
+ {showMiddle && <PktTabItem index={1}>Second Tab</PktTabItem>}
298
+ <PktTabItem index={2}>Third Tab</PktTabItem>
299
+ </PktTabs>
300
+ )
301
+ const { getByText, rerender } = render(renderWith(true))
302
+ rerender(renderWith(false))
303
+
304
+ const firstTab = getByText('First Tab')
305
+ firstTab.focus()
306
+ fireEvent.keyDown(firstTab, { key: 'ArrowRight' })
307
+ expect(getByText('Third Tab')).toHaveFocus()
308
+
309
+ fireEvent.keyDown(getByText('Third Tab'), { key: 'ArrowRight' })
310
+ expect(firstTab).toHaveFocus()
311
+ })
312
+
291
313
  it('wraps to the last tab with ArrowLeft on the first tab', () => {
292
314
  const { getByText } = render(
293
315
  <PktTabs>
@@ -10,12 +10,11 @@ import {
10
10
  Ref,
11
11
  useCallback,
12
12
  useContext,
13
- useEffect,
14
13
  useRef,
15
14
  useState,
16
15
  } from 'react'
17
16
 
18
- import type { TTagSkin } from 'shared-types'
17
+ import { TABS_DEFAULTS, type IPktTab, type IPktTabsBase } from 'shared-types'
19
18
  import { tabIndexForKey } from 'shared-utils/tabs/keyboard'
20
19
 
21
20
  import { PktIcon } from '../icon/Icon'
@@ -32,7 +31,7 @@ interface ITabsContext {
32
31
  setSelectedIndex: (index: number) => void
33
32
  fallbackFocusIndex: number | null
34
33
  reportTab: (index: number, state: TTabState | null) => void
35
- registerTabRef: (index: number, el: HTMLAnchorElement | HTMLButtonElement | null, disabled?: boolean) => void
34
+ registerTabRef: (index: number, el: HTMLAnchorElement | HTMLButtonElement | null) => void
36
35
  handleKeyPress: (index: number, event: KeyboardEvent<HTMLAnchorElement | HTMLButtonElement>) => void
37
36
  selectTab: (index: number) => void
38
37
  }
@@ -47,28 +46,9 @@ export const useTabsContext = () => {
47
46
  return context
48
47
  }
49
48
 
50
- export interface IPktTab {
51
- text: string
52
- href?: string
53
- action?: (index: number) => void
54
- disabled?: boolean
55
- icon?: string
56
- controls?: string
57
- tag?: {
58
- text: string
59
- skin: TTagSkin | undefined
60
- }
61
- active?: boolean
62
- }
49
+ export type { IPktTab }
63
50
 
64
- export interface IPktTabs {
65
- arrowNav?: boolean
66
- disableArrowNav?: boolean
67
- separatorIconName?: string
68
- separatorIconPath?: string
69
- togglePanels?: boolean
70
- ariaLabel?: string
71
- ariaLabelledBy?: string
51
+ export interface IPktTabs extends IPktTabsBase {
72
52
  tabs?: IPktTab[]
73
53
  onTabSelected?: (index: number) => void
74
54
  children?: ReactNode
@@ -77,11 +57,11 @@ export interface IPktTabs {
77
57
  export const PktTabs = forwardRef(
78
58
  (
79
59
  {
80
- arrowNav = true,
81
- disableArrowNav = false,
60
+ arrowNav = TABS_DEFAULTS.arrowNav,
61
+ disableArrowNav = TABS_DEFAULTS.disableArrowNav,
82
62
  separatorIconName,
83
63
  separatorIconPath,
84
- togglePanels = false,
64
+ togglePanels = TABS_DEFAULTS.togglePanels,
85
65
  ariaLabel,
86
66
  ariaLabelledBy,
87
67
  tabs,
@@ -91,7 +71,6 @@ export const PktTabs = forwardRef(
91
71
  ref: Ref<HTMLDivElement>,
92
72
  ): JSX.Element => {
93
73
  const tabElementsRef = useRef<Array<HTMLAnchorElement | HTMLButtonElement | null>>([])
94
- const disabledMapRef = useRef<Record<number, boolean>>({})
95
74
  const [selectedIndex, setSelectedIndex] = useState<number | null>(null)
96
75
  const [tabStates, setTabStates] = useState<Record<number, TTabState> | null>(null)
97
76
 
@@ -121,21 +100,10 @@ export const PktTabs = forwardRef(
121
100
  // eslint-disable-next-line @eslint-react/no-children-to-array
122
101
  const childItems = Children.toArray(children)
123
102
  const hasChildren = childItems.length > 0
124
- const tabCount = hasChildren ? childItems.length : tabs?.length || 0
103
+ const tabCount = tabStates ? Math.max(0, ...Object.keys(tabStates).map((key) => Number(key) + 1)) : 0
125
104
  const hasSeparator = !!(separatorIconName || separatorIconPath)
126
105
 
127
- useEffect(() => {
128
- tabElementsRef.current = tabElementsRef.current.slice(0, tabCount)
129
- Object.keys(disabledMapRef.current).forEach((key) => {
130
- const index = Number(key)
131
- if (index >= tabCount) delete disabledMapRef.current[index]
132
- })
133
- }, [tabCount])
134
-
135
- const isTabDisabled = (index: number): boolean => {
136
- if (tabs) return !!tabs[index]?.disabled
137
- return !!disabledMapRef.current[index]
138
- }
106
+ const isTabDisabled = (index: number): boolean => !tabStates?.[index] || tabStates[index].disabled
139
107
 
140
108
  const selectTab = (index: number): void => {
141
109
  if (isTabDisabled(index)) return
@@ -166,9 +134,8 @@ export const PktTabs = forwardRef(
166
134
  }
167
135
  }
168
136
 
169
- const registerTabRef = (index: number, el: HTMLAnchorElement | HTMLButtonElement | null, disabled = false) => {
137
+ const registerTabRef = (index: number, el: HTMLAnchorElement | HTMLButtonElement | null) => {
170
138
  tabElementsRef.current[index] = el
171
- disabledMapRef.current[index] = disabled
172
139
  }
173
140
 
174
141
  const renderSeparator = (key: string) => (