@navikt/ds-react 0.14.4 → 0.14.8
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/cjs/form/Select.js +1 -1
- package/cjs/form/Switch.js +15 -4
- package/cjs/help-text/HelpText.js +2 -2
- package/cjs/index.js +1 -0
- package/cjs/link-panel/LinkPanel.js +1 -1
- package/cjs/popover/Popover.js +2 -1
- package/cjs/step-indicator/Step.js +62 -0
- package/cjs/step-indicator/StepIndicator.js +76 -0
- package/cjs/step-indicator/index.js +19 -0
- package/cjs/step-indicator/package.json +6 -0
- package/esm/form/Select.js +1 -1
- package/esm/form/Select.js.map +1 -1
- package/esm/form/Switch.d.ts +4 -0
- package/esm/form/Switch.js +16 -5
- package/esm/form/Switch.js.map +1 -1
- package/esm/help-text/HelpText.d.ts +2 -7
- package/esm/help-text/HelpText.js +2 -2
- package/esm/help-text/HelpText.js.map +1 -1
- package/esm/index.d.ts +1 -0
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -1
- package/esm/link-panel/LinkPanel.js +1 -1
- package/esm/link-panel/LinkPanel.js.map +1 -1
- package/esm/popover/Popover.d.ts +6 -0
- package/esm/popover/Popover.js +2 -1
- package/esm/popover/Popover.js.map +1 -1
- package/esm/step-indicator/Step.d.ts +20 -0
- package/esm/step-indicator/Step.js +39 -0
- package/esm/step-indicator/Step.js.map +1 -0
- package/esm/step-indicator/StepIndicator.d.ts +41 -0
- package/esm/step-indicator/StepIndicator.js +52 -0
- package/esm/step-indicator/StepIndicator.js.map +1 -0
- package/esm/step-indicator/index.d.ts +2 -0
- package/esm/step-indicator/index.js +3 -0
- package/esm/step-indicator/index.js.map +1 -0
- package/package.json +2 -2
- package/src/form/Select.tsx +1 -1
- package/src/form/Switch.tsx +65 -6
- package/src/form/stories/switch.stories.mdx +73 -3
- package/src/form/stories/switch.stories.tsx +27 -1
- package/src/help-text/HelpText.tsx +12 -9
- package/src/index.ts +1 -0
- package/src/link-panel/LinkPanel.tsx +1 -4
- package/src/popover/Popover.tsx +8 -0
- package/src/step-indicator/Step.tsx +79 -0
- package/src/step-indicator/StepIndicator.tsx +145 -0
- package/src/step-indicator/index.ts +2 -0
- package/src/step-indicator/stories/Example.tsx +23 -0
- package/src/step-indicator/stories/step-indicator.stories.mdx +122 -0
- package/src/step-indicator/stories/step-indicator.stories.tsx +104 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import cl from "classnames";
|
|
2
|
+
import React, {
|
|
3
|
+
createContext,
|
|
4
|
+
forwardRef,
|
|
5
|
+
useCallback,
|
|
6
|
+
useEffect,
|
|
7
|
+
useRef,
|
|
8
|
+
useState,
|
|
9
|
+
} from "react";
|
|
10
|
+
import mergeRefs from "react-merge-refs";
|
|
11
|
+
import Step, { StepIndicatorStepProps, StepIndicatorStepType } from "./Step";
|
|
12
|
+
|
|
13
|
+
export interface StepIndicatorProps
|
|
14
|
+
extends React.HTMLAttributes<HTMLOListElement> {
|
|
15
|
+
/**
|
|
16
|
+
* <StepIndicator.Step /> elements
|
|
17
|
+
*/
|
|
18
|
+
children: React.ReactNode;
|
|
19
|
+
/**
|
|
20
|
+
* Adds classname to wrapper
|
|
21
|
+
*/
|
|
22
|
+
className?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Current active step index
|
|
25
|
+
*/
|
|
26
|
+
activeStep: number;
|
|
27
|
+
/**
|
|
28
|
+
* Callback for clicked step index
|
|
29
|
+
*/
|
|
30
|
+
onStepChange?: (step: number) => void;
|
|
31
|
+
/**
|
|
32
|
+
* Hides labels for each step if true
|
|
33
|
+
* @default false
|
|
34
|
+
*/
|
|
35
|
+
hideLabels?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* enables `hideLabels` internally when steps start to overflow.
|
|
38
|
+
* @note declaring `hideLabels` overwrites this functionality
|
|
39
|
+
*/
|
|
40
|
+
responsive?: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface StepIndicatorComponent
|
|
44
|
+
extends React.ForwardRefExoticComponent<
|
|
45
|
+
StepIndicatorProps & React.RefAttributes<HTMLOListElement>
|
|
46
|
+
> {
|
|
47
|
+
Step: StepIndicatorStepType;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface StepContextProps {
|
|
51
|
+
activeStep: number;
|
|
52
|
+
onStepChange: (step: number) => void;
|
|
53
|
+
hideLabels: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const StepContext = createContext<StepContextProps | null>(null);
|
|
57
|
+
|
|
58
|
+
const StepIndicator: StepIndicatorComponent = forwardRef<
|
|
59
|
+
HTMLOListElement,
|
|
60
|
+
StepIndicatorProps
|
|
61
|
+
>(
|
|
62
|
+
(
|
|
63
|
+
{
|
|
64
|
+
children,
|
|
65
|
+
className,
|
|
66
|
+
activeStep,
|
|
67
|
+
hideLabels,
|
|
68
|
+
onStepChange = () => {},
|
|
69
|
+
responsive,
|
|
70
|
+
...rest
|
|
71
|
+
},
|
|
72
|
+
ref
|
|
73
|
+
) => {
|
|
74
|
+
const wrapperRef = useRef<HTMLOListElement | null>(null);
|
|
75
|
+
const mergedRef = mergeRefs([wrapperRef, ref]);
|
|
76
|
+
|
|
77
|
+
const [showLabels, setShowLabels] = useState(true);
|
|
78
|
+
|
|
79
|
+
const removeLabels = hideLabels ?? (!!responsive && !showLabels);
|
|
80
|
+
|
|
81
|
+
const stepsWithIndex = React.Children.map(children, (step, index) => {
|
|
82
|
+
return React.isValidElement<StepIndicatorStepProps>(step) ? (
|
|
83
|
+
<li
|
|
84
|
+
className={cl("navds-step-indicator__step-wrapper", {
|
|
85
|
+
"navds-step-indicator__step-wrapper--hidelabel": removeLabels,
|
|
86
|
+
})}
|
|
87
|
+
key={index}
|
|
88
|
+
aria-current={index === activeStep && "step"}
|
|
89
|
+
>
|
|
90
|
+
{React.cloneElement(step, {
|
|
91
|
+
...step.props,
|
|
92
|
+
index,
|
|
93
|
+
})}
|
|
94
|
+
</li>
|
|
95
|
+
) : (
|
|
96
|
+
step
|
|
97
|
+
);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const canShowLabels = useCallback(() => {
|
|
101
|
+
const remSize = parseFloat(
|
|
102
|
+
String(getComputedStyle(document.documentElement).fontSize)
|
|
103
|
+
);
|
|
104
|
+
const childrenLength = React.Children.toArray(children).filter((child) =>
|
|
105
|
+
React.isValidElement(child)
|
|
106
|
+
).length;
|
|
107
|
+
|
|
108
|
+
wrapperRef.current &&
|
|
109
|
+
setShowLabels(
|
|
110
|
+
wrapperRef.current?.getBoundingClientRect().width >=
|
|
111
|
+
remSize * 10 * childrenLength
|
|
112
|
+
);
|
|
113
|
+
}, [children]);
|
|
114
|
+
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
window.addEventListener("resize", canShowLabels);
|
|
117
|
+
canShowLabels();
|
|
118
|
+
return () => {
|
|
119
|
+
window.removeEventListener("resize", canShowLabels);
|
|
120
|
+
};
|
|
121
|
+
}, [canShowLabels]);
|
|
122
|
+
|
|
123
|
+
return (
|
|
124
|
+
<ol
|
|
125
|
+
ref={mergedRef}
|
|
126
|
+
className={cl(`navds-step-indicator`, className)}
|
|
127
|
+
{...rest}
|
|
128
|
+
>
|
|
129
|
+
<StepContext.Provider
|
|
130
|
+
value={{
|
|
131
|
+
activeStep,
|
|
132
|
+
onStepChange,
|
|
133
|
+
hideLabels: removeLabels,
|
|
134
|
+
}}
|
|
135
|
+
>
|
|
136
|
+
{stepsWithIndex}
|
|
137
|
+
</StepContext.Provider>
|
|
138
|
+
</ol>
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
) as StepIndicatorComponent;
|
|
142
|
+
|
|
143
|
+
StepIndicator.Step = Step;
|
|
144
|
+
|
|
145
|
+
export default StepIndicator;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import React, { useState } from "react";
|
|
2
|
+
import { StepIndicator } from "..";
|
|
3
|
+
|
|
4
|
+
export const Example = ({ ...props }) => {
|
|
5
|
+
const [active, setactive] = useState(1);
|
|
6
|
+
|
|
7
|
+
const { disabled, ...rest } = props;
|
|
8
|
+
|
|
9
|
+
return (
|
|
10
|
+
<StepIndicator {...rest} activeStep={active} onStepChange={setactive}>
|
|
11
|
+
<StepIndicator.Step>Start</StepIndicator.Step>
|
|
12
|
+
<StepIndicator.Step>
|
|
13
|
+
Sunt deserunt qui sit sunt culpa nisi
|
|
14
|
+
</StepIndicator.Step>
|
|
15
|
+
<StepIndicator.Step disabled={disabled}>
|
|
16
|
+
Nulla nisi pariatur nulla cupidatat elit.
|
|
17
|
+
</StepIndicator.Step>
|
|
18
|
+
<StepIndicator.Step>
|
|
19
|
+
Nulla laborum proident consequat laborum elit et dolore ut sunt.
|
|
20
|
+
</StepIndicator.Step>
|
|
21
|
+
</StepIndicator>
|
|
22
|
+
);
|
|
23
|
+
};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { Meta, Canvas } from "@storybook/addon-docs";
|
|
2
|
+
import { StepIndicator } from "..";
|
|
3
|
+
import { Example } from "./Example";
|
|
4
|
+
|
|
5
|
+
<Meta title="ds-react/step-indicator/intro" />
|
|
6
|
+
|
|
7
|
+
# Hvordan ta i bruk StepIndicator
|
|
8
|
+
|
|
9
|
+
Vi flytter over Stegindikator til `ds-react` uten noen veldig store endringer i design.
|
|
10
|
+
Du må nå selv styre state med `activeStep` og `onStepChange`.
|
|
11
|
+
|
|
12
|
+
```jsx
|
|
13
|
+
<StepIndicator activeStep={activeStep} onStepChange={(x) => setActiveStep(x)}>
|
|
14
|
+
<StepIndicator.Step>Start</StepIndicator.Step>
|
|
15
|
+
<StepIndicator.Step>Sunt deserunt qui sit sunt culpa nisi</StepIndicator.Step>
|
|
16
|
+
<StepIndicator.Step>
|
|
17
|
+
Nulla nisi pariatur nulla cupidatat elit.
|
|
18
|
+
</StepIndicator.Step>
|
|
19
|
+
<StepIndicator.Step>
|
|
20
|
+
Nulla laborum proident consequat laborum elit et dolore ut sunt.
|
|
21
|
+
</StepIndicator.Step>
|
|
22
|
+
</StepIndicator>
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
<Canvas>
|
|
26
|
+
<Example />
|
|
27
|
+
</Canvas>
|
|
28
|
+
|
|
29
|
+
## responsive
|
|
30
|
+
|
|
31
|
+
Bruk `responsive`-prop for å skjule labels med en gang StepIndicator begynner å overlfowe.
|
|
32
|
+
|
|
33
|
+
```jsx
|
|
34
|
+
<StepIndicator
|
|
35
|
+
responsive
|
|
36
|
+
activeStep={activeStep}
|
|
37
|
+
onStepChange={(x) => setActiveStep(x)}
|
|
38
|
+
>
|
|
39
|
+
<StepIndicator.Step>Start</StepIndicator.Step>
|
|
40
|
+
<StepIndicator.Step>Sunt deserunt qui sit sunt culpa nisi</StepIndicator.Step>
|
|
41
|
+
<StepIndicator.Step>
|
|
42
|
+
Nulla nisi pariatur nulla cupidatat elit.
|
|
43
|
+
</StepIndicator.Step>
|
|
44
|
+
<StepIndicator.Step>
|
|
45
|
+
Nulla laborum proident consequat laborum elit et dolore ut sunt.
|
|
46
|
+
</StepIndicator.Step>
|
|
47
|
+
</StepIndicator>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
<Canvas>
|
|
51
|
+
<Example responsive />
|
|
52
|
+
</Canvas>
|
|
53
|
+
|
|
54
|
+
## hideLabels
|
|
55
|
+
|
|
56
|
+
Du kan gjemme labels fra hvert steg med `hideLabels`-prop
|
|
57
|
+
|
|
58
|
+
```jsx
|
|
59
|
+
<StepIndicator
|
|
60
|
+
hideLabels
|
|
61
|
+
activeStep={activeStep}
|
|
62
|
+
onStepChange={(x) => setActiveStep(x)}
|
|
63
|
+
>
|
|
64
|
+
<StepIndicator.Step>Start</StepIndicator.Step>
|
|
65
|
+
<StepIndicator.Step>Sunt deserunt qui sit sunt culpa nisi</StepIndicator.Step>
|
|
66
|
+
<StepIndicator.Step>
|
|
67
|
+
Nulla nisi pariatur nulla cupidatat elit.
|
|
68
|
+
</StepIndicator.Step>
|
|
69
|
+
<StepIndicator.Step>
|
|
70
|
+
Nulla laborum proident consequat laborum elit et dolore ut sunt.
|
|
71
|
+
</StepIndicator.Step>
|
|
72
|
+
</StepIndicator>
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
<Canvas>
|
|
76
|
+
<Example hideLabels />
|
|
77
|
+
</Canvas>
|
|
78
|
+
|
|
79
|
+
## disabled
|
|
80
|
+
|
|
81
|
+
```jsx
|
|
82
|
+
<StepIndicator activeStep={activeStep} onStepChange={(x) => setActiveStep(x)}>
|
|
83
|
+
<StepIndicator.Step>Start</StepIndicator.Step>
|
|
84
|
+
<StepIndicator.Step disabled>
|
|
85
|
+
Sunt deserunt qui sit sunt culpa nisi
|
|
86
|
+
</StepIndicator.Step>
|
|
87
|
+
<StepIndicator.Step disabled>
|
|
88
|
+
Nulla nisi pariatur nulla cupidatat elit.
|
|
89
|
+
</StepIndicator.Step>
|
|
90
|
+
<StepIndicator.Step>
|
|
91
|
+
Nulla laborum proident consequat laborum elit et dolore ut sunt.
|
|
92
|
+
</StepIndicator.Step>
|
|
93
|
+
</StepIndicator>
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
<Canvas>
|
|
97
|
+
<Example disabled />
|
|
98
|
+
</Canvas>
|
|
99
|
+
|
|
100
|
+
## Override tags
|
|
101
|
+
|
|
102
|
+
`StepIndicator.Step` er implementert med Overridable-component noe som gjør at du kan endre taggen til
|
|
103
|
+
det du selv ønsker ved å bruke `as`-prop
|
|
104
|
+
|
|
105
|
+
```jsx
|
|
106
|
+
<StepIndicator activeStep={activeStep} onStepChange={(x) => setActiveStep(x)}>
|
|
107
|
+
{/* Dette er default */}
|
|
108
|
+
<StepIndicator.Step as="button">Start</StepIndicator.Step>
|
|
109
|
+
<StepIndicator.Step as="a" href="#">
|
|
110
|
+
Sunt deserunt qui sit sunt culpa nisi
|
|
111
|
+
</StepIndicator.Step>
|
|
112
|
+
</StepIndicator>
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
<Canvas>
|
|
116
|
+
<StepIndicator activeStep={1} onStepChange={console.log}>
|
|
117
|
+
<StepIndicator.Step as="button">{`<button>`}</StepIndicator.Step>
|
|
118
|
+
<StepIndicator.Step as="a" href="#">
|
|
119
|
+
{`<a>`}
|
|
120
|
+
</StepIndicator.Step>
|
|
121
|
+
</StepIndicator>
|
|
122
|
+
</Canvas>
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import React, { useState } from "react";
|
|
2
|
+
import StepIndicator from "../StepIndicator";
|
|
3
|
+
import { Meta } from "@storybook/react/types-6-0";
|
|
4
|
+
import { Link, HashRouter as Router, useLocation } from "react-router-dom";
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
title: "ds-react/step-indicator",
|
|
8
|
+
component: StepIndicator,
|
|
9
|
+
} as Meta;
|
|
10
|
+
|
|
11
|
+
export const All = () => {
|
|
12
|
+
const [activeStep, setActiveStep] = useState(1);
|
|
13
|
+
return (
|
|
14
|
+
<div>
|
|
15
|
+
<StepIndicator activeStep={1} onStepChange={console.log}>
|
|
16
|
+
<StepIndicator.Step>Steg nr 1</StepIndicator.Step>
|
|
17
|
+
<StepIndicator.Step>Laborum velit eu magna esse</StepIndicator.Step>
|
|
18
|
+
<StepIndicator.Step>test</StepIndicator.Step>
|
|
19
|
+
</StepIndicator>
|
|
20
|
+
<br />
|
|
21
|
+
|
|
22
|
+
<StepIndicator activeStep={1} onStepChange={console.log}>
|
|
23
|
+
<StepIndicator.Step href="#" as="a">
|
|
24
|
+
1
|
|
25
|
+
</StepIndicator.Step>
|
|
26
|
+
<StepIndicator.Step href="#" as="a">
|
|
27
|
+
2
|
|
28
|
+
</StepIndicator.Step>
|
|
29
|
+
<StepIndicator.Step href="#" as="a">
|
|
30
|
+
3
|
|
31
|
+
</StepIndicator.Step>
|
|
32
|
+
</StepIndicator>
|
|
33
|
+
<br />
|
|
34
|
+
<StepIndicator activeStep={activeStep} onStepChange={setActiveStep}>
|
|
35
|
+
<StepIndicator.Step disabled>
|
|
36
|
+
Pariatur pariatur adipisicing reprehenderit ad occaecat reprehenderit
|
|
37
|
+
ut dolore.
|
|
38
|
+
</StepIndicator.Step>
|
|
39
|
+
<StepIndicator.Step>Laborum velit eu magna esse</StepIndicator.Step>
|
|
40
|
+
<StepIndicator.Step disabled>
|
|
41
|
+
Cupidatat Lorem do nostrud ut eu.
|
|
42
|
+
</StepIndicator.Step>
|
|
43
|
+
<StepIndicator.Step>test</StepIndicator.Step>
|
|
44
|
+
<StepIndicator.Step>
|
|
45
|
+
Voluptate pariatur ut est voluptate elit officia excepteur laborum.
|
|
46
|
+
</StepIndicator.Step>
|
|
47
|
+
</StepIndicator>
|
|
48
|
+
<br />
|
|
49
|
+
<StepIndicator
|
|
50
|
+
activeStep={activeStep}
|
|
51
|
+
onStepChange={setActiveStep}
|
|
52
|
+
hideLabels
|
|
53
|
+
>
|
|
54
|
+
<StepIndicator.Step>
|
|
55
|
+
Pariatur pariatur adipisicing reprehenderit ad occaecat reprehenderit
|
|
56
|
+
ut dolore.
|
|
57
|
+
</StepIndicator.Step>
|
|
58
|
+
<StepIndicator.Step>Laborum velit eu magna esse</StepIndicator.Step>
|
|
59
|
+
<StepIndicator.Step disabled>
|
|
60
|
+
Cupidatat Lorem do nostrud ut eu.
|
|
61
|
+
</StepIndicator.Step>
|
|
62
|
+
<StepIndicator.Step>test</StepIndicator.Step>
|
|
63
|
+
<StepIndicator.Step>
|
|
64
|
+
Voluptate pariatur ut est voluptate elit officia excepteur laborum.
|
|
65
|
+
</StepIndicator.Step>
|
|
66
|
+
</StepIndicator>
|
|
67
|
+
</div>
|
|
68
|
+
);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const ReactRouter = () => {
|
|
72
|
+
const { pathname } = useLocation();
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<StepIndicator
|
|
76
|
+
activeStep={
|
|
77
|
+
{
|
|
78
|
+
"/": 0,
|
|
79
|
+
"/first": 0,
|
|
80
|
+
"/second": 1,
|
|
81
|
+
"/third": 2,
|
|
82
|
+
}[pathname]
|
|
83
|
+
}
|
|
84
|
+
>
|
|
85
|
+
<StepIndicator.Step as={Link} to="/first">
|
|
86
|
+
Steg nr 1
|
|
87
|
+
</StepIndicator.Step>
|
|
88
|
+
<StepIndicator.Step as={Link} to="/second">
|
|
89
|
+
Laborum velit eu magna esse
|
|
90
|
+
</StepIndicator.Step>
|
|
91
|
+
<StepIndicator.Step as={Link} to="/third">
|
|
92
|
+
test
|
|
93
|
+
</StepIndicator.Step>
|
|
94
|
+
</StepIndicator>
|
|
95
|
+
);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
ReactRouter.decorators = [
|
|
99
|
+
(Story) => (
|
|
100
|
+
<Router>
|
|
101
|
+
<Story />
|
|
102
|
+
</Router>
|
|
103
|
+
),
|
|
104
|
+
];
|