@navikt/ds-react 0.16.19 → 0.17.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/cjs/accordion/Accordion.js +5 -1
- package/cjs/accordion/AccordionContent.js +5 -1
- package/cjs/accordion/AccordionHeader.js +5 -1
- package/cjs/accordion/AccordionItem.js +5 -1
- package/cjs/accordion/index.js +5 -1
- package/cjs/alert/Alert.js +5 -1
- package/cjs/alert/index.js +5 -1
- package/cjs/button/Button.js +5 -1
- package/cjs/button/index.js +5 -1
- package/cjs/card/MicroCard.js +5 -1
- package/cjs/card/index.js +5 -1
- package/cjs/form/ConfirmationPanel.js +5 -1
- package/cjs/form/Fieldset/Fieldset.js +5 -1
- package/cjs/form/Select.js +5 -1
- package/cjs/form/Switch.js +5 -1
- package/cjs/form/TextField.js +5 -1
- package/cjs/form/Textarea.js +5 -1
- package/cjs/form/checkbox/Checkbox.js +5 -1
- package/cjs/form/checkbox/CheckboxGroup.js +5 -1
- package/cjs/form/error-summary/ErrorSummary.js +5 -1
- package/cjs/form/error-summary/ErrorSummaryItem.js +5 -1
- package/cjs/form/error-summary/index.js +5 -1
- package/cjs/form/index.js +3 -3
- package/cjs/form/radio/Radio.js +5 -1
- package/cjs/form/radio/RadioGroup.js +5 -1
- package/cjs/form/search/Search.js +102 -0
- package/cjs/form/{search-field/SearchFieldButton.js → search/SearchButton.js} +22 -11
- package/cjs/form/search/index.js +8 -0
- package/cjs/form/search/package.json +6 -0
- package/cjs/form/search/useSearch.js +31 -0
- package/cjs/grid/Cell.js +5 -1
- package/cjs/grid/ContentContainer.js +5 -1
- package/cjs/grid/Grid.js +5 -1
- package/cjs/grid/index.js +5 -1
- package/cjs/guide-panel/Guide.js +5 -1
- package/cjs/guide-panel/GuidePanel.js +5 -1
- package/cjs/guide-panel/index.js +5 -1
- package/cjs/help-text/HelpText.js +5 -1
- package/cjs/help-text/index.js +5 -1
- package/cjs/index.js +5 -1
- package/cjs/link/Link.js +5 -1
- package/cjs/link/index.js +5 -1
- package/cjs/link-panel/LinkPanel.js +5 -1
- package/cjs/link-panel/LinkPanelDescription.js +5 -1
- package/cjs/link-panel/LinkPanelTitle.js +5 -1
- package/cjs/link-panel/index.js +5 -1
- package/cjs/loader/Loader.js +5 -1
- package/cjs/loader/index.js +5 -1
- package/cjs/menu/Menu.js +5 -1
- package/cjs/menu/MenuCollapse.js +5 -1
- package/cjs/menu/MenuItem.js +5 -1
- package/cjs/modal/Modal.js +7 -3
- package/cjs/modal/ModalContent.js +5 -1
- package/cjs/modal/index.js +5 -1
- package/cjs/page-header/PageHeader.js +5 -1
- package/cjs/page-header/index.js +5 -1
- package/cjs/pagination/index.js +5 -1
- package/cjs/panel/Panel.js +5 -1
- package/cjs/panel/index.js +5 -1
- package/cjs/popover/Popover.js +5 -1
- package/cjs/popover/PopoverContent.js +5 -1
- package/cjs/popover/index.js +5 -1
- package/cjs/speech-bubble/Bubble.js +5 -1
- package/cjs/speech-bubble/SpeechBubble.js +5 -1
- package/cjs/speech-bubble/index.js +5 -1
- package/cjs/step-indicator/Step.js +5 -1
- package/cjs/step-indicator/StepIndicator.js +5 -1
- package/cjs/step-indicator/index.js +5 -1
- package/cjs/table/Body.js +5 -1
- package/cjs/table/ColumnHeader.js +5 -1
- package/cjs/table/DataCell.js +5 -1
- package/cjs/table/Header.js +5 -1
- package/cjs/table/HeaderCell.js +5 -1
- package/cjs/table/Row.js +5 -1
- package/cjs/table/Table.js +5 -1
- package/cjs/table/index.js +5 -1
- package/cjs/tag/Tag.js +5 -1
- package/cjs/tag/index.js +5 -1
- package/cjs/toggle-group/ToggleGroup.js +5 -1
- package/cjs/toggle-group/ToggleItem.js +5 -1
- package/cjs/typography/BodyLong.js +5 -1
- package/cjs/typography/BodyShort.js +5 -1
- package/cjs/typography/Detail.js +5 -1
- package/cjs/typography/Heading.js +5 -1
- package/cjs/typography/Ingress.js +5 -1
- package/cjs/typography/Label.js +5 -1
- package/cjs/util/index.js +20 -2
- package/esm/form/index.d.ts +1 -1
- package/esm/form/index.js +1 -1
- package/esm/form/index.js.map +1 -1
- package/esm/form/search/Search.d.ts +62 -0
- package/esm/form/search/Search.js +74 -0
- package/esm/form/search/Search.js.map +1 -0
- package/esm/form/search/SearchButton.d.ts +11 -0
- package/esm/form/search/SearchButton.js +34 -0
- package/esm/form/search/SearchButton.js.map +1 -0
- package/esm/form/search/index.d.ts +1 -0
- package/esm/form/search/index.js +2 -0
- package/esm/form/search/index.js.map +1 -0
- package/esm/form/search/useSearch.d.ts +10 -0
- package/esm/form/search/useSearch.js +25 -0
- package/esm/form/search/useSearch.js.map +1 -0
- package/esm/help-text/HelpText.js.map +1 -1
- package/esm/modal/Modal.d.ts +9 -0
- package/esm/modal/Modal.js +2 -2
- package/esm/modal/Modal.js.map +1 -1
- package/esm/table/ColumnHeader.js.map +1 -1
- package/esm/util/index.d.ts +5 -0
- package/esm/util/index.js +13 -0
- package/esm/util/index.js.map +1 -1
- package/package.json +2 -2
- package/src/form/index.ts +1 -1
- package/src/form/search/Search.tsx +235 -0
- package/src/form/search/SearchButton.tsx +51 -0
- package/src/form/search/index.ts +1 -0
- package/src/form/search/search.stories.tsx +91 -0
- package/src/form/search/useSearch.ts +31 -0
- package/src/modal/Modal.tsx +11 -0
- package/src/modal/{stories/modal.stories.tsx → modal.stories.tsx} +9 -5
- package/src/util/index.ts +33 -0
- package/cjs/form/search-field/SearchField.js +0 -69
- package/cjs/form/search-field/SearchFieldClearButton.js +0 -50
- package/cjs/form/search-field/SearchFieldInput.js +0 -49
- package/cjs/form/search-field/index.js +0 -19
- package/cjs/form/search-field/package.json +0 -6
- package/esm/form/search-field/SearchField.d.ts +0 -36
- package/esm/form/search-field/SearchField.js +0 -45
- package/esm/form/search-field/SearchField.js.map +0 -1
- package/esm/form/search-field/SearchFieldButton.d.ts +0 -17
- package/esm/form/search-field/SearchFieldButton.js +0 -27
- package/esm/form/search-field/SearchFieldButton.js.map +0 -1
- package/esm/form/search-field/SearchFieldClearButton.d.ts +0 -17
- package/esm/form/search-field/SearchFieldClearButton.js +0 -27
- package/esm/form/search-field/SearchFieldClearButton.js.map +0 -1
- package/esm/form/search-field/SearchFieldInput.d.ts +0 -6
- package/esm/form/search-field/SearchFieldInput.js +0 -26
- package/esm/form/search-field/SearchFieldInput.js.map +0 -1
- package/esm/form/search-field/index.d.ts +0 -2
- package/esm/form/search-field/index.js +0 -3
- package/esm/form/search-field/index.js.map +0 -1
- package/src/form/search-field/SearchField.tsx +0 -132
- package/src/form/search-field/SearchFieldButton.tsx +0 -47
- package/src/form/search-field/SearchFieldClearButton.tsx +0 -49
- package/src/form/search-field/SearchFieldInput.tsx +0 -42
- package/src/form/search-field/index.ts +0 -2
- package/src/form/search-field/stories/search-field-example.tsx +0 -25
- package/src/form/search-field/stories/search-field.stories.mdx +0 -156
- package/src/form/search-field/stories/search-field.stories.tsx +0 -199
- package/src/modal/stories/modal.stories.mdx +0 -32
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { Close } from "@navikt/ds-icons";
|
|
2
|
+
import cl from "classnames";
|
|
3
|
+
import React, {
|
|
4
|
+
forwardRef,
|
|
5
|
+
InputHTMLAttributes,
|
|
6
|
+
useCallback,
|
|
7
|
+
useEffect,
|
|
8
|
+
useRef,
|
|
9
|
+
useState,
|
|
10
|
+
} from "react";
|
|
11
|
+
import mergeRefs from "react-merge-refs";
|
|
12
|
+
import { BodyShort, Label, omit, useEventListener } from "../..";
|
|
13
|
+
import { FormFieldProps } from "../useFormField";
|
|
14
|
+
import SearchButton, { SearchButtonType } from "./SearchButton";
|
|
15
|
+
import { useSearch } from "./useSearch";
|
|
16
|
+
|
|
17
|
+
export type SearchClearEvent =
|
|
18
|
+
| {
|
|
19
|
+
trigger: "Click";
|
|
20
|
+
event: React.MouseEvent<HTMLButtonElement, MouseEvent>;
|
|
21
|
+
}
|
|
22
|
+
| { trigger: "Escape"; event: React.KeyboardEvent<HTMLDivElement> };
|
|
23
|
+
|
|
24
|
+
export interface SearchProps
|
|
25
|
+
extends Omit<FormFieldProps, "error" | "errorId">,
|
|
26
|
+
Omit<InputHTMLAttributes<HTMLInputElement>, "size" | "onChange"> {
|
|
27
|
+
children?: React.ReactNode;
|
|
28
|
+
/**
|
|
29
|
+
* Search label
|
|
30
|
+
* @info Will be hidden by default, is required for accessibility reasons.
|
|
31
|
+
*/
|
|
32
|
+
label: React.ReactNode;
|
|
33
|
+
/**
|
|
34
|
+
* Shows label and description for screenreaders-only
|
|
35
|
+
* @default true
|
|
36
|
+
*/
|
|
37
|
+
hideLabel?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Callback for value-change in input
|
|
40
|
+
*/
|
|
41
|
+
onChange?: (value: string) => void;
|
|
42
|
+
/**
|
|
43
|
+
* Callback for <Search.Button/> click with current input-value
|
|
44
|
+
*/
|
|
45
|
+
onSearch?: (value: string | number | readonly string[]) => void;
|
|
46
|
+
/**
|
|
47
|
+
* Callback for click on clear-button or Escape keydown
|
|
48
|
+
*/
|
|
49
|
+
onClear?: (e: SearchClearEvent) => void;
|
|
50
|
+
/**
|
|
51
|
+
* aria-label on clear button
|
|
52
|
+
* @default "Tøm"
|
|
53
|
+
*/
|
|
54
|
+
clearButtonLabel?: string;
|
|
55
|
+
/**
|
|
56
|
+
* If false, removes clear-button option from input.
|
|
57
|
+
* @default true
|
|
58
|
+
*/
|
|
59
|
+
clearButton?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Changes button-variant
|
|
62
|
+
* @default "tertiary"
|
|
63
|
+
*/
|
|
64
|
+
variant?: "tertiary" | "primary";
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface SearchComponent
|
|
68
|
+
extends React.ForwardRefExoticComponent<
|
|
69
|
+
SearchProps & React.RefAttributes<HTMLDivElement>
|
|
70
|
+
> {
|
|
71
|
+
Button: SearchButtonType;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface SearchContextProps {
|
|
75
|
+
disabled?: boolean;
|
|
76
|
+
size: "medium" | "small";
|
|
77
|
+
onSearch: () => void;
|
|
78
|
+
variant?: "tertiary" | "primary";
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export const SearchContext = React.createContext<SearchContextProps | null>(
|
|
82
|
+
null
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const Search = forwardRef<HTMLInputElement, SearchProps>((props, ref) => {
|
|
86
|
+
const { inputProps, size = "medium", inputDescriptionId } = useSearch(
|
|
87
|
+
props,
|
|
88
|
+
"searchfield"
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
const {
|
|
92
|
+
className,
|
|
93
|
+
hideLabel = true,
|
|
94
|
+
label,
|
|
95
|
+
description,
|
|
96
|
+
value,
|
|
97
|
+
clearButtonLabel,
|
|
98
|
+
onClear,
|
|
99
|
+
clearButton = true,
|
|
100
|
+
children,
|
|
101
|
+
onSearch,
|
|
102
|
+
variant = "tertiary",
|
|
103
|
+
...rest
|
|
104
|
+
} = props;
|
|
105
|
+
|
|
106
|
+
const searchRef = useRef<HTMLInputElement | null>(null);
|
|
107
|
+
const mergedRef = mergeRefs([searchRef, ref]);
|
|
108
|
+
const [wrapperRef, setWrapperRef] = useState<HTMLFormElement | null>(null);
|
|
109
|
+
|
|
110
|
+
const [controlledValue, setControlledValue] = useState(value ?? "");
|
|
111
|
+
|
|
112
|
+
const handleChange = useCallback(
|
|
113
|
+
(v: string) => {
|
|
114
|
+
searchRef.current && value === undefined && setControlledValue(v);
|
|
115
|
+
props?.onChange?.(v);
|
|
116
|
+
},
|
|
117
|
+
[props, value]
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
const handleClear = useCallback(
|
|
121
|
+
(event: SearchClearEvent) => {
|
|
122
|
+
onClear?.(event);
|
|
123
|
+
handleChange("");
|
|
124
|
+
if (searchRef.current && value === undefined) {
|
|
125
|
+
searchRef.current.value = "";
|
|
126
|
+
}
|
|
127
|
+
searchRef.current && searchRef.current?.focus?.();
|
|
128
|
+
},
|
|
129
|
+
[handleChange, onClear, value]
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
useEventListener(
|
|
133
|
+
"keydown",
|
|
134
|
+
useCallback(
|
|
135
|
+
(e) => {
|
|
136
|
+
if (e.key === "Escape") {
|
|
137
|
+
e.preventDefault();
|
|
138
|
+
handleClear({ trigger: "Escape", event: e });
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
[handleClear]
|
|
142
|
+
),
|
|
143
|
+
wrapperRef
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
useEffect(() => {
|
|
147
|
+
value !== undefined && setControlledValue(value);
|
|
148
|
+
}, [value]);
|
|
149
|
+
|
|
150
|
+
return (
|
|
151
|
+
<form
|
|
152
|
+
role="search"
|
|
153
|
+
ref={setWrapperRef}
|
|
154
|
+
className={cl(
|
|
155
|
+
className,
|
|
156
|
+
"navds-form-field",
|
|
157
|
+
`navds-form-field--${size}`,
|
|
158
|
+
"navds-search",
|
|
159
|
+
{
|
|
160
|
+
"navds-search--disabled": !!inputProps.disabled,
|
|
161
|
+
}
|
|
162
|
+
)}
|
|
163
|
+
onSubmit={(e) => e.preventDefault()}
|
|
164
|
+
>
|
|
165
|
+
<Label
|
|
166
|
+
htmlFor={inputProps.id}
|
|
167
|
+
size={size}
|
|
168
|
+
as="label"
|
|
169
|
+
className={cl("navds-text-field__label", {
|
|
170
|
+
"navds-sr-only": hideLabel,
|
|
171
|
+
})}
|
|
172
|
+
>
|
|
173
|
+
{label}
|
|
174
|
+
</Label>
|
|
175
|
+
{!!description && (
|
|
176
|
+
<BodyShort
|
|
177
|
+
as="div"
|
|
178
|
+
className={cl("navds-text-field__description", {
|
|
179
|
+
"navds-sr-only": hideLabel,
|
|
180
|
+
})}
|
|
181
|
+
id={inputDescriptionId}
|
|
182
|
+
size={size}
|
|
183
|
+
>
|
|
184
|
+
{description}
|
|
185
|
+
</BodyShort>
|
|
186
|
+
)}
|
|
187
|
+
<div className="navds-search__wrapper">
|
|
188
|
+
<div className="navds-search__wrapper-inner">
|
|
189
|
+
<input
|
|
190
|
+
ref={mergedRef}
|
|
191
|
+
{...omit(rest, ["size"])}
|
|
192
|
+
{...inputProps}
|
|
193
|
+
{...(props.value !== undefined && { value: props.value })}
|
|
194
|
+
onChange={(e) => handleChange(e.target.value)}
|
|
195
|
+
type="search"
|
|
196
|
+
role="searchbox"
|
|
197
|
+
className={cl(
|
|
198
|
+
className,
|
|
199
|
+
"navds-search__input",
|
|
200
|
+
"navds-text-field__input",
|
|
201
|
+
"navds-body-short",
|
|
202
|
+
`navds-body-${size ?? "medium"}`
|
|
203
|
+
)}
|
|
204
|
+
/>
|
|
205
|
+
{controlledValue && clearButton && (
|
|
206
|
+
<button
|
|
207
|
+
type="button"
|
|
208
|
+
onClick={(e) => handleClear({ trigger: "Click", event: e })}
|
|
209
|
+
className="navds-search__button-clear"
|
|
210
|
+
>
|
|
211
|
+
<span className="navds-sr-only">
|
|
212
|
+
{clearButtonLabel ? clearButtonLabel : "Tøm"}
|
|
213
|
+
</span>
|
|
214
|
+
<Close aria-hidden />
|
|
215
|
+
</button>
|
|
216
|
+
)}
|
|
217
|
+
</div>
|
|
218
|
+
<SearchContext.Provider
|
|
219
|
+
value={{
|
|
220
|
+
size,
|
|
221
|
+
disabled: inputProps.disabled,
|
|
222
|
+
onSearch: () => onSearch?.(controlledValue),
|
|
223
|
+
variant,
|
|
224
|
+
}}
|
|
225
|
+
>
|
|
226
|
+
{children ? children : <SearchButton />}
|
|
227
|
+
</SearchContext.Provider>
|
|
228
|
+
</div>
|
|
229
|
+
</form>
|
|
230
|
+
);
|
|
231
|
+
}) as SearchComponent;
|
|
232
|
+
|
|
233
|
+
Search.Button = SearchButton;
|
|
234
|
+
|
|
235
|
+
export default Search;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Search } from "@navikt/ds-icons";
|
|
2
|
+
import cl from "classnames";
|
|
3
|
+
import React, { forwardRef, useContext } from "react";
|
|
4
|
+
import { Button, ButtonProps } from "../..";
|
|
5
|
+
import { SearchContext } from "./Search";
|
|
6
|
+
|
|
7
|
+
export interface SearchButtonProps
|
|
8
|
+
extends Omit<ButtonProps, "size" | "children" | "variant"> {
|
|
9
|
+
/**
|
|
10
|
+
* Text set before <Search/> icon
|
|
11
|
+
*/
|
|
12
|
+
children?: React.ReactNode;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type SearchButtonType = React.ForwardRefExoticComponent<
|
|
16
|
+
SearchButtonProps & React.RefAttributes<HTMLButtonElement>
|
|
17
|
+
>;
|
|
18
|
+
|
|
19
|
+
const SearchButton: SearchButtonType = forwardRef(
|
|
20
|
+
({ className, children, disabled, onClick, ...rest }, ref) => {
|
|
21
|
+
const context = useContext(SearchContext);
|
|
22
|
+
|
|
23
|
+
if (context === null) {
|
|
24
|
+
console.warn("<Search.Button> has to be wrapped in <Search />");
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const { size, onSearch, variant } = context;
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<Button
|
|
32
|
+
type="button"
|
|
33
|
+
{...rest}
|
|
34
|
+
ref={ref}
|
|
35
|
+
size={size}
|
|
36
|
+
variant={variant}
|
|
37
|
+
className={cl("navds-search__button-search", className)}
|
|
38
|
+
disabled={context?.disabled ?? disabled}
|
|
39
|
+
onClick={(e) => {
|
|
40
|
+
onSearch();
|
|
41
|
+
onClick?.(e);
|
|
42
|
+
}}
|
|
43
|
+
>
|
|
44
|
+
<Search aria-hidden />
|
|
45
|
+
{children ? children : <span className="navds-sr-only">Søk</span>}
|
|
46
|
+
</Button>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
export default SearchButton;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as Search, SearchProps, SearchClearEvent } from "./Search";
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { Meta } from "@storybook/react/types-6-0";
|
|
2
|
+
import React, { useState } from "react";
|
|
3
|
+
|
|
4
|
+
import { Search } from "../index";
|
|
5
|
+
export default {
|
|
6
|
+
title: "ds-react/form/search",
|
|
7
|
+
component: Search,
|
|
8
|
+
} as Meta;
|
|
9
|
+
|
|
10
|
+
export const All = () => {
|
|
11
|
+
const [value, setValue] = useState("");
|
|
12
|
+
return (
|
|
13
|
+
<div style={{ maxWidth: 300 }}>
|
|
14
|
+
<h1>Search</h1>
|
|
15
|
+
<div>
|
|
16
|
+
<Search
|
|
17
|
+
label="Søk alle sider om X og Y"
|
|
18
|
+
onSearch={console.log}
|
|
19
|
+
></Search>
|
|
20
|
+
<br />
|
|
21
|
+
<Search
|
|
22
|
+
label="Søk alle sider om X og Y"
|
|
23
|
+
onSearch={console.log}
|
|
24
|
+
variant="primary"
|
|
25
|
+
></Search>
|
|
26
|
+
</div>
|
|
27
|
+
|
|
28
|
+
<h2>Search small</h2>
|
|
29
|
+
<Search
|
|
30
|
+
label="Søk alle sider om X og Y"
|
|
31
|
+
description="Beskrivelse av søket"
|
|
32
|
+
size="small"
|
|
33
|
+
>
|
|
34
|
+
<Search.Button />
|
|
35
|
+
</Search>
|
|
36
|
+
<br />
|
|
37
|
+
<Search
|
|
38
|
+
label="Søk alle sider om X og Y"
|
|
39
|
+
description="Beskrivelse av søket"
|
|
40
|
+
size="small"
|
|
41
|
+
hideLabel
|
|
42
|
+
variant="primary"
|
|
43
|
+
>
|
|
44
|
+
<Search.Button />
|
|
45
|
+
</Search>
|
|
46
|
+
|
|
47
|
+
<h2>Med knappe-tekst</h2>
|
|
48
|
+
<Search label="Søk alle sider om X og Y" hideLabel={false}>
|
|
49
|
+
<Search.Button>Søk</Search.Button>
|
|
50
|
+
</Search>
|
|
51
|
+
<Search
|
|
52
|
+
label="Søk alle sider om X og Y"
|
|
53
|
+
hideLabel={false}
|
|
54
|
+
variant="primary"
|
|
55
|
+
>
|
|
56
|
+
<Search.Button>Søk</Search.Button>
|
|
57
|
+
</Search>
|
|
58
|
+
<h2>Hidelabel false</h2>
|
|
59
|
+
<Search label="Søk alle sider om X og Y" hideLabel={false}>
|
|
60
|
+
<Search.Button />
|
|
61
|
+
</Search>
|
|
62
|
+
<h2>Controlled state </h2>
|
|
63
|
+
<Search
|
|
64
|
+
value={value}
|
|
65
|
+
label="Søk alle sider om X og Y"
|
|
66
|
+
description="Beskrivelse av søket"
|
|
67
|
+
onChange={(e) => setValue(e)}
|
|
68
|
+
onClear={() => setValue("")}
|
|
69
|
+
>
|
|
70
|
+
<Search.Button />
|
|
71
|
+
</Search>
|
|
72
|
+
<h2>No clear button</h2>
|
|
73
|
+
<Search
|
|
74
|
+
hideLabel
|
|
75
|
+
label="Søk alle sider om X og Y"
|
|
76
|
+
description="Beskrivelse av søket"
|
|
77
|
+
clearButton={false}
|
|
78
|
+
>
|
|
79
|
+
<Search.Button />
|
|
80
|
+
</Search>
|
|
81
|
+
</div>
|
|
82
|
+
);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export const UUDemo = () => {
|
|
86
|
+
return (
|
|
87
|
+
<Search label="Søk på nav.no" onSearch={console.log} placeholder="Søk">
|
|
88
|
+
<Search.Button />
|
|
89
|
+
</Search>
|
|
90
|
+
);
|
|
91
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import cl from "classnames";
|
|
2
|
+
import { useContext } from "react";
|
|
3
|
+
import { useId } from "../../index";
|
|
4
|
+
import { FieldsetContext } from "../index";
|
|
5
|
+
import { FormFieldProps } from "../useFormField";
|
|
6
|
+
|
|
7
|
+
export const useSearch = (props: FormFieldProps, prefix: string) => {
|
|
8
|
+
const { size } = props;
|
|
9
|
+
|
|
10
|
+
const fieldset = useContext(FieldsetContext);
|
|
11
|
+
|
|
12
|
+
const genId = useId();
|
|
13
|
+
|
|
14
|
+
const id = props.id ?? `${prefix}-${genId}`;
|
|
15
|
+
const inputDescriptionId = `${prefix}-description-${genId}`;
|
|
16
|
+
|
|
17
|
+
const disabled = fieldset?.disabled || props.disabled;
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
inputDescriptionId,
|
|
21
|
+
size: size ?? fieldset?.size ?? "medium",
|
|
22
|
+
inputProps: {
|
|
23
|
+
id,
|
|
24
|
+
"aria-describedby":
|
|
25
|
+
cl(props["aria-describedby"], {
|
|
26
|
+
[inputDescriptionId]: !!props?.description,
|
|
27
|
+
}) || undefined,
|
|
28
|
+
disabled,
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
};
|
package/src/modal/Modal.tsx
CHANGED
|
@@ -33,9 +33,18 @@ export interface ModalProps {
|
|
|
33
33
|
* @default true
|
|
34
34
|
*/
|
|
35
35
|
closeButton?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Callback for getting parent element modal will attach to
|
|
38
|
+
*/
|
|
39
|
+
parentSelector?(): HTMLElement;
|
|
36
40
|
"aria-labelledby"?: string;
|
|
37
41
|
"aria-describedby"?: string;
|
|
38
42
|
"aria-modal"?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Sets aria-label on modal
|
|
45
|
+
* @warning This should be set if not using 'aria-labelledby' or 'aria-describedby'
|
|
46
|
+
*/
|
|
47
|
+
"aria-label"?: string;
|
|
39
48
|
}
|
|
40
49
|
|
|
41
50
|
interface ModalComponent
|
|
@@ -62,6 +71,7 @@ const Modal = forwardRef<ReactModal, ModalProps>(
|
|
|
62
71
|
"aria-describedby": ariaDescribedBy,
|
|
63
72
|
"aria-labelledby": ariaLabelledBy,
|
|
64
73
|
"aria-modal": ariaModal,
|
|
74
|
+
"aria-label": contentLabel,
|
|
65
75
|
...rest
|
|
66
76
|
},
|
|
67
77
|
ref
|
|
@@ -92,6 +102,7 @@ const Modal = forwardRef<ReactModal, ModalProps>(
|
|
|
92
102
|
labelledby: ariaLabelledBy,
|
|
93
103
|
modal: ariaModal,
|
|
94
104
|
}}
|
|
105
|
+
contentLabel={contentLabel}
|
|
95
106
|
>
|
|
96
107
|
{children}
|
|
97
108
|
{closeButton && (
|
|
@@ -16,9 +16,13 @@ export const All = () => {
|
|
|
16
16
|
return (
|
|
17
17
|
<>
|
|
18
18
|
<button onClick={() => setOpen(true)}>Open modal</button>
|
|
19
|
-
<Modal
|
|
19
|
+
<Modal
|
|
20
|
+
open={open}
|
|
21
|
+
onClose={() => setOpen(false)}
|
|
22
|
+
aria-labelledby="header123"
|
|
23
|
+
>
|
|
20
24
|
<Modal.Content>
|
|
21
|
-
<h1>Header</h1>
|
|
25
|
+
<h1 id="header123">Header</h1>
|
|
22
26
|
<h2>subheader</h2>
|
|
23
27
|
<p>Cupidatat irure ipsum veniam ad in esse.</p>
|
|
24
28
|
<p>Cillum tempor pariatur amet ut laborum Lorem enim enim.</p>
|
|
@@ -32,9 +36,10 @@ export const All = () => {
|
|
|
32
36
|
shouldCloseOnOverlayClick={false}
|
|
33
37
|
open={openTwo}
|
|
34
38
|
onClose={() => setOpenTwo(false)}
|
|
39
|
+
aria-labelledby="header123"
|
|
35
40
|
>
|
|
36
41
|
<Modal.Content>
|
|
37
|
-
<h1>Header</h1>
|
|
42
|
+
<h1 id="header123">Header</h1>
|
|
38
43
|
<h2>subheader</h2>
|
|
39
44
|
<p>
|
|
40
45
|
Cupidatat irure ipsum veniam ad in esse. Voluptate do nulla amet
|
|
@@ -54,10 +59,9 @@ export const All = () => {
|
|
|
54
59
|
open={openThree}
|
|
55
60
|
closeButton={false}
|
|
56
61
|
onClose={() => setOpenThree(false)}
|
|
62
|
+
aria-label="Min modal"
|
|
57
63
|
>
|
|
58
64
|
<Modal.Content>
|
|
59
|
-
<h1>Header</h1>
|
|
60
|
-
<h2>subheader</h2>
|
|
61
65
|
<p>Cupidatat irure ipsum veniam ad in esse.</p>
|
|
62
66
|
<p>Cillum tempor pariatur amet ut laborum Lorem enim enim.</p>
|
|
63
67
|
</Modal.Content>
|
package/src/util/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
|
|
1
3
|
export * from "./OverridableComponent";
|
|
2
4
|
export * from "./useId";
|
|
3
5
|
|
|
@@ -11,3 +13,34 @@ export const omit = (obj: object, props: string[]) =>
|
|
|
11
13
|
}),
|
|
12
14
|
{}
|
|
13
15
|
);
|
|
16
|
+
|
|
17
|
+
export interface ListenerT {
|
|
18
|
+
addEventListener(
|
|
19
|
+
name: string,
|
|
20
|
+
handler: (event?: any) => void,
|
|
21
|
+
...args: any[]
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
removeEventListener(
|
|
25
|
+
name: string,
|
|
26
|
+
handler: (event?: any) => void,
|
|
27
|
+
...args: any[]
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/* https://github.com/streamich/react-use/blob/master/src/useEvent.ts */
|
|
32
|
+
export const useEventListener = <T extends ListenerT>(
|
|
33
|
+
name: Parameters<ListenerT["addEventListener"]>[0],
|
|
34
|
+
handler: Parameters<ListenerT["addEventListener"]>[1],
|
|
35
|
+
target: null | T | Window = window
|
|
36
|
+
): void => {
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
if (!target) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
target?.addEventListener(name, handler);
|
|
42
|
+
return () => {
|
|
43
|
+
target?.addEventListener(name, handler);
|
|
44
|
+
};
|
|
45
|
+
}, [name, handler, target]);
|
|
46
|
+
};
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
-
}) : function(o, v) {
|
|
12
|
-
o["default"] = v;
|
|
13
|
-
});
|
|
14
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
-
if (mod && mod.__esModule) return mod;
|
|
16
|
-
var result = {};
|
|
17
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
-
__setModuleDefault(result, mod);
|
|
19
|
-
return result;
|
|
20
|
-
};
|
|
21
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
22
|
-
var t = {};
|
|
23
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
24
|
-
t[p] = s[p];
|
|
25
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
26
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
27
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
28
|
-
t[p[i]] = s[p[i]];
|
|
29
|
-
}
|
|
30
|
-
return t;
|
|
31
|
-
};
|
|
32
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
33
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
34
|
-
};
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.SearchFieldContext = void 0;
|
|
37
|
-
const classnames_1 = __importDefault(require("classnames"));
|
|
38
|
-
const react_1 = __importStar(require("react"));
|
|
39
|
-
const __1 = require("../..");
|
|
40
|
-
const ErrorMessage_1 = __importDefault(require("../ErrorMessage"));
|
|
41
|
-
const useFormField_1 = require("../useFormField");
|
|
42
|
-
const SearchFieldButton_1 = __importDefault(require("./SearchFieldButton"));
|
|
43
|
-
const SearchFieldClearButton_1 = __importDefault(require("./SearchFieldClearButton"));
|
|
44
|
-
const SearchFieldInput_1 = __importDefault(require("./SearchFieldInput"));
|
|
45
|
-
exports.SearchFieldContext = react_1.default.createContext(null);
|
|
46
|
-
const SearchField = (0, react_1.forwardRef)((props, ref) => {
|
|
47
|
-
const { inputProps, errorId, showErrorMsg, hasError, size, inputDescriptionId, } = (0, useFormField_1.useFormField)(props, "searchfield");
|
|
48
|
-
const { className, hideLabel, children, label, description, error } = props, rest = __rest(props, ["className", "hideLabel", "children", "label", "description", "error"]);
|
|
49
|
-
return (react_1.default.createElement("div", Object.assign({ ref: ref }, (0, __1.omit)(rest, ["id", "error", "errorId", "size", "disabled"]), { className: (0, classnames_1.default)(className, "navds-form-field", `navds-form-field--${size !== null && size !== void 0 ? size : "medium"}`, "navds-search-field", {
|
|
50
|
-
"navds-search-field--error": hasError,
|
|
51
|
-
"navds-search-field--disabled": !!inputProps.disabled,
|
|
52
|
-
}) }),
|
|
53
|
-
react_1.default.createElement(__1.Label, { htmlFor: inputProps.id, size: size, as: "label", className: (0, classnames_1.default)("navds-text-field__label", {
|
|
54
|
-
"navds-sr-only": hideLabel,
|
|
55
|
-
}) }, label),
|
|
56
|
-
!!description && (react_1.default.createElement(__1.BodyShort, { as: "div", className: (0, classnames_1.default)("navds-text-field__description", {
|
|
57
|
-
"navds-sr-only": hideLabel,
|
|
58
|
-
}), id: inputDescriptionId, size: size }, description)),
|
|
59
|
-
react_1.default.createElement("div", { className: "navds-search-field__input-wrapper" },
|
|
60
|
-
react_1.default.createElement(exports.SearchFieldContext.Provider, { value: {
|
|
61
|
-
inputProps,
|
|
62
|
-
size,
|
|
63
|
-
} }, children)),
|
|
64
|
-
react_1.default.createElement("div", { id: errorId, "aria-relevant": "additions removals", "aria-live": "polite" }, showErrorMsg && react_1.default.createElement(ErrorMessage_1.default, { size: size }, error))));
|
|
65
|
-
});
|
|
66
|
-
SearchField.Button = SearchFieldButton_1.default;
|
|
67
|
-
SearchField.Clear = SearchFieldClearButton_1.default;
|
|
68
|
-
SearchField.Input = SearchFieldInput_1.default;
|
|
69
|
-
exports.default = SearchField;
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
-
}) : function(o, v) {
|
|
12
|
-
o["default"] = v;
|
|
13
|
-
});
|
|
14
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
-
if (mod && mod.__esModule) return mod;
|
|
16
|
-
var result = {};
|
|
17
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
-
__setModuleDefault(result, mod);
|
|
19
|
-
return result;
|
|
20
|
-
};
|
|
21
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
22
|
-
var t = {};
|
|
23
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
24
|
-
t[p] = s[p];
|
|
25
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
26
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
27
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
28
|
-
t[p[i]] = s[p[i]];
|
|
29
|
-
}
|
|
30
|
-
return t;
|
|
31
|
-
};
|
|
32
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
33
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
34
|
-
};
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
const react_1 = __importStar(require("react"));
|
|
37
|
-
const classnames_1 = __importDefault(require("classnames"));
|
|
38
|
-
const SearchField_1 = require("./SearchField");
|
|
39
|
-
const __1 = require("../..");
|
|
40
|
-
const SearchFieldClearButton = (0, react_1.forwardRef)((_a, ref) => {
|
|
41
|
-
var { className, variant = "secondary", disabled } = _a, rest = __rest(_a, ["className", "variant", "disabled"]);
|
|
42
|
-
const searchField = (0, react_1.useContext)(SearchField_1.SearchFieldContext);
|
|
43
|
-
if (searchField === null) {
|
|
44
|
-
console.warn("SearchFieldClearButton has to be wrapped in <SearchField />");
|
|
45
|
-
return null;
|
|
46
|
-
}
|
|
47
|
-
const { size, inputProps } = searchField;
|
|
48
|
-
return (react_1.default.createElement(__1.Button, Object.assign({}, rest, { ref: ref, className: (0, classnames_1.default)(className, "navds-search-field__clear-button"), size: size, variant: variant, disabled: disabled !== null && disabled !== void 0 ? disabled : inputProps === null || inputProps === void 0 ? void 0 : inputProps.disabled })));
|
|
49
|
-
});
|
|
50
|
-
exports.default = SearchFieldClearButton;
|