@olenbetong/appframe-ds 1.7.1 → 1.7.2

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.
@@ -1,183 +1,183 @@
1
- import { useCallback, useRef, useState } from "react";
2
-
3
- import type {
4
- AutocompleteChangeReason,
5
- AutocompleteFilterOptionsState,
6
- AutocompleteInputChangeReason,
7
- } from "./types.js";
8
-
9
- /**
10
- * Default label resolver: uses `label`, then `name`, then the value itself.
11
- */
12
- export function defaultGetOptionLabel(option: unknown): string {
13
- if (option == null) return "";
14
- if (typeof option === "string") return option;
15
- if (typeof option === "object") {
16
- let record = option as Record<string, unknown>;
17
- if (typeof record.label === "string") return record.label;
18
- if (typeof record.name === "string") return record.name;
19
- }
20
-
21
- return String(option);
22
- }
23
-
24
- export interface CreateFilterOptionsConfig<Option> {
25
- /**
26
- * Removes diacritics before matching.
27
- * @default true
28
- */
29
- ignoreAccents?: boolean;
30
- /**
31
- * Matches case insensitively.
32
- * @default true
33
- */
34
- ignoreCase?: boolean;
35
- /**
36
- * Where in the option label the query has to match.
37
- * @default "any"
38
- */
39
- matchFrom?: "any" | "start";
40
- /**
41
- * The maximum number of options returned.
42
- */
43
- limit?: number;
44
- /**
45
- * Trims the query before matching.
46
- * @default false
47
- */
48
- trim?: boolean;
49
- /**
50
- * Returns the string the query is matched against.
51
- */
52
- stringify?: (option: Option) => string;
53
- }
54
-
55
- /**
56
- * Creates a filter function for the `filterOptions` prop, equivalent to MUI's
57
- * `createFilterOptions`.
58
- *
59
- * @example
60
- * ```jsx
61
- * let filterOptions = createFilterOptions({ matchFrom: "start", limit: 20 });
62
- * <Combobox options={options} filterOptions={filterOptions} />
63
- * ```
64
- */
65
- export function createFilterOptions<Option>(config: CreateFilterOptionsConfig<Option> = {}) {
66
- let { ignoreAccents = true, ignoreCase = true, limit, matchFrom = "any", stringify, trim = false } = config;
67
-
68
- return (options: readonly Option[], state: AutocompleteFilterOptionsState<Option>) => {
69
- let input = trim ? state.inputValue.trim() : state.inputValue;
70
- if (ignoreCase) input = input.toLowerCase();
71
- if (ignoreAccents) input = stripDiacritics(input);
72
-
73
- let filtered = input
74
- ? options.filter((option) => {
75
- let candidate = (stringify ?? state.getOptionLabel)(option);
76
- if (ignoreCase) candidate = candidate.toLowerCase();
77
- if (ignoreAccents) candidate = stripDiacritics(candidate);
78
-
79
- return matchFrom === "start" ? candidate.startsWith(input) : candidate.includes(input);
80
- })
81
- : [...options];
82
-
83
- return typeof limit === "number" ? filtered.slice(0, limit) : filtered;
84
- };
85
- }
86
-
87
- function stripDiacritics(value: string) {
88
- return value.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
89
- }
90
-
91
- export const defaultFilterOptions = createFilterOptions<unknown>();
92
-
93
- export interface OptionGroup<Option> {
94
- /**
95
- * Group header text. Named `value` because Base UI uses it as the group key.
96
- */
97
- value: string;
98
- items: Option[];
99
- [key: string]: unknown;
100
- }
101
-
102
- /**
103
- * Splits an already sorted list of options into consecutive groups, the same
104
- * way MUI's `groupBy` works.
105
- */
106
- export function groupOptions<Option>(options: readonly Option[], groupBy: (option: Option) => string) {
107
- let groups: OptionGroup<Option>[] = [];
108
-
109
- for (let option of options) {
110
- let group = groupBy(option);
111
- let last = groups[groups.length - 1];
112
-
113
- if (last && last.value === group) {
114
- last.items.push(option);
115
- } else {
116
- groups.push({ value: group, items: [option] });
117
- }
118
- }
119
-
120
- return groups;
121
- }
122
-
123
- /**
124
- * State that is controlled when `controlled` is not `undefined`, and
125
- * uncontrolled otherwise.
126
- */
127
- export function useControlled<T>(controlled: T | undefined, defaultValue: T) {
128
- let [uncontrolled, setUncontrolled] = useState(defaultValue);
129
- let isControlled = useRef(controlled !== undefined).current;
130
- let value = isControlled ? (controlled as T) : uncontrolled;
131
-
132
- let setValue = useCallback(
133
- (next: T) => {
134
- if (!isControlled) setUncontrolled(next);
135
- },
136
- [isControlled],
137
- );
138
-
139
- return [value, setValue] as const;
140
- }
141
-
142
- /**
143
- * Compares two options, falling back to reference equality like MUI does.
144
- */
145
- export function areOptionsEqual<Option>(
146
- a: Option,
147
- b: Option,
148
- isOptionEqualToValue?: (option: Option, value: Option) => boolean,
149
- ) {
150
- return isOptionEqualToValue ? isOptionEqualToValue(a, b) : Object.is(a, b);
151
- }
152
-
153
- /**
154
- * Maps a Base UI change reason to the MUI-style reason.
155
- */
156
- export function mapChangeReason(reason: string, next: unknown): AutocompleteChangeReason {
157
- if (reason === "clear-press" || reason === "input-clear") return "clear";
158
- if (reason === "chip-remove-press") return "removeOption";
159
- if (next == null || (Array.isArray(next) && next.length === 0)) return "clear";
160
-
161
- return "selectOption";
162
- }
163
-
164
- function mapInputChangeReason(reason: string): AutocompleteInputChangeReason {
165
- if (reason === "input-change") return "input";
166
- if (reason === "input-clear" || reason === "clear-press") return "clear";
167
-
168
- return "reset";
169
- }
170
-
171
- /**
172
- * Keeps the input value state in sync and forwards the change to
173
- * `onInputChange` with a MUI-style reason.
174
- */
175
- export function handleInputValueChange(
176
- next: string,
177
- details: { event?: Event; reason: string },
178
- setInputValue: (value: string) => void,
179
- onInputChange?: (event: Event | undefined, value: string, reason: AutocompleteInputChangeReason) => void,
180
- ) {
181
- setInputValue(next);
182
- onInputChange?.(details.event, next, mapInputChangeReason(details.reason));
183
- }
1
+ import { useCallback, useRef, useState } from "react";
2
+
3
+ import type {
4
+ AutocompleteChangeReason,
5
+ AutocompleteFilterOptionsState,
6
+ AutocompleteInputChangeReason,
7
+ } from "./types.js";
8
+
9
+ /**
10
+ * Default label resolver: uses `label`, then `name`, then the value itself.
11
+ */
12
+ export function defaultGetOptionLabel(option: unknown): string {
13
+ if (option == null) return "";
14
+ if (typeof option === "string") return option;
15
+ if (typeof option === "object") {
16
+ let record = option as Record<string, unknown>;
17
+ if (typeof record.label === "string") return record.label;
18
+ if (typeof record.name === "string") return record.name;
19
+ }
20
+
21
+ return String(option);
22
+ }
23
+
24
+ export interface CreateFilterOptionsConfig<Option> {
25
+ /**
26
+ * Removes diacritics before matching.
27
+ * @default true
28
+ */
29
+ ignoreAccents?: boolean;
30
+ /**
31
+ * Matches case insensitively.
32
+ * @default true
33
+ */
34
+ ignoreCase?: boolean;
35
+ /**
36
+ * Where in the option label the query has to match.
37
+ * @default "any"
38
+ */
39
+ matchFrom?: "any" | "start";
40
+ /**
41
+ * The maximum number of options returned.
42
+ */
43
+ limit?: number;
44
+ /**
45
+ * Trims the query before matching.
46
+ * @default false
47
+ */
48
+ trim?: boolean;
49
+ /**
50
+ * Returns the string the query is matched against.
51
+ */
52
+ stringify?: (option: Option) => string;
53
+ }
54
+
55
+ /**
56
+ * Creates a filter function for the `filterOptions` prop, equivalent to MUI's
57
+ * `createFilterOptions`.
58
+ *
59
+ * @example
60
+ * ```jsx
61
+ * let filterOptions = createFilterOptions({ matchFrom: "start", limit: 20 });
62
+ * <Combobox options={options} filterOptions={filterOptions} />
63
+ * ```
64
+ */
65
+ export function createFilterOptions<Option>(config: CreateFilterOptionsConfig<Option> = {}) {
66
+ let { ignoreAccents = true, ignoreCase = true, limit, matchFrom = "any", stringify, trim = false } = config;
67
+
68
+ return (options: readonly Option[], state: AutocompleteFilterOptionsState<Option>) => {
69
+ let input = trim ? state.inputValue.trim() : state.inputValue;
70
+ if (ignoreCase) input = input.toLowerCase();
71
+ if (ignoreAccents) input = stripDiacritics(input);
72
+
73
+ let filtered = input
74
+ ? options.filter((option) => {
75
+ let candidate = (stringify ?? state.getOptionLabel)(option);
76
+ if (ignoreCase) candidate = candidate.toLowerCase();
77
+ if (ignoreAccents) candidate = stripDiacritics(candidate);
78
+
79
+ return matchFrom === "start" ? candidate.startsWith(input) : candidate.includes(input);
80
+ })
81
+ : [...options];
82
+
83
+ return typeof limit === "number" ? filtered.slice(0, limit) : filtered;
84
+ };
85
+ }
86
+
87
+ function stripDiacritics(value: string) {
88
+ return value.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
89
+ }
90
+
91
+ export const defaultFilterOptions = createFilterOptions<unknown>();
92
+
93
+ export interface OptionGroup<Option> {
94
+ /**
95
+ * Group header text. Named `value` because Base UI uses it as the group key.
96
+ */
97
+ value: string;
98
+ items: Option[];
99
+ [key: string]: unknown;
100
+ }
101
+
102
+ /**
103
+ * Splits an already sorted list of options into consecutive groups, the same
104
+ * way MUI's `groupBy` works.
105
+ */
106
+ export function groupOptions<Option>(options: readonly Option[], groupBy: (option: Option) => string) {
107
+ let groups: OptionGroup<Option>[] = [];
108
+
109
+ for (let option of options) {
110
+ let group = groupBy(option);
111
+ let last = groups[groups.length - 1];
112
+
113
+ if (last && last.value === group) {
114
+ last.items.push(option);
115
+ } else {
116
+ groups.push({ value: group, items: [option] });
117
+ }
118
+ }
119
+
120
+ return groups;
121
+ }
122
+
123
+ /**
124
+ * State that is controlled when `controlled` is not `undefined`, and
125
+ * uncontrolled otherwise.
126
+ */
127
+ export function useControlled<T>(controlled: T | undefined, defaultValue: T) {
128
+ let [uncontrolled, setUncontrolled] = useState(defaultValue);
129
+ let isControlled = useRef(controlled !== undefined).current;
130
+ let value = isControlled ? (controlled as T) : uncontrolled;
131
+
132
+ let setValue = useCallback(
133
+ (next: T) => {
134
+ if (!isControlled) setUncontrolled(next);
135
+ },
136
+ [isControlled],
137
+ );
138
+
139
+ return [value, setValue] as const;
140
+ }
141
+
142
+ /**
143
+ * Compares two options, falling back to reference equality like MUI does.
144
+ */
145
+ export function areOptionsEqual<Option>(
146
+ a: Option,
147
+ b: Option,
148
+ isOptionEqualToValue?: (option: Option, value: Option) => boolean,
149
+ ) {
150
+ return isOptionEqualToValue ? isOptionEqualToValue(a, b) : Object.is(a, b);
151
+ }
152
+
153
+ /**
154
+ * Maps a Base UI change reason to the MUI-style reason.
155
+ */
156
+ export function mapChangeReason(reason: string, next: unknown): AutocompleteChangeReason {
157
+ if (reason === "clear-press" || reason === "input-clear") return "clear";
158
+ if (reason === "chip-remove-press") return "removeOption";
159
+ if (next == null || (Array.isArray(next) && next.length === 0)) return "clear";
160
+
161
+ return "selectOption";
162
+ }
163
+
164
+ function mapInputChangeReason(reason: string): AutocompleteInputChangeReason {
165
+ if (reason === "input-change") return "input";
166
+ if (reason === "input-clear" || reason === "clear-press") return "clear";
167
+
168
+ return "reset";
169
+ }
170
+
171
+ /**
172
+ * Keeps the input value state in sync and forwards the change to
173
+ * `onInputChange` with a MUI-style reason.
174
+ */
175
+ export function handleInputValueChange(
176
+ next: string,
177
+ details: { event?: Event; reason: string },
178
+ setInputValue: (value: string) => void,
179
+ onInputChange?: (event: Event | undefined, value: string, reason: AutocompleteInputChangeReason) => void,
180
+ ) {
181
+ setInputValue(next);
182
+ onInputChange?.(details.event, next, mapInputChangeReason(details.reason));
183
+ }
@@ -1,80 +1,101 @@
1
- /*
2
- * The wrapper is the visual input: it owns the border, background, hover and
3
- * focus styling, while the inner Designsystemet input is rendered bare so the
4
- * input and the adornments read as a single control.
5
- */
6
- .ObInputAdornments-root {
7
- --obInputAdornments-padding: 0.25rem;
8
- --obInputAdornments-gap: 0.25rem;
9
- --obInputAdornments-fontSize: var(--ds-font-size-3, 1rem);
10
-
11
- display: flex;
12
- align-items: center;
13
- box-sizing: border-box;
14
- gap: var(--obInputAdornments-gap);
15
- padding: var(--obInputAdornments-padding);
16
- inline-size: 100%;
17
- color: var(--ds-color-neutral-text-default, #1e2b3c);
18
- background-color: var(--ds-color-neutral-surface-default, #fff);
19
- border: var(--ds-border-width-default, 1px) solid var(--ds-color-neutral-border-default, #b3b3b3);
20
- border-radius: var(--ds-border-radius-md, 0.375rem);
21
- }
22
-
23
- /* Designsystemet components come in sm, md and lg; xs and xl are typography-only sizes */
24
- .ObInputAdornments-root[data-size="sm"] {
25
- --obInputAdornments-padding: 0.25rem;
26
- --obInputAdornments-fontSize: var(--ds-font-size-2, 0.875rem);
27
- }
28
-
29
- .ObInputAdornments-root[data-size="md"] {
30
- --obInputAdornments-padding: 0.25rem;
31
- --obInputAdornments-fontSize: var(--ds-font-size-3, 1rem);
32
- }
33
-
34
- .ObInputAdornments-root[data-size="lg"] {
35
- --obInputAdornments-padding: 0.375rem;
36
- --obInputAdornments-fontSize: var(--ds-font-size-4, 1.125rem);
37
- }
38
-
39
- /* The Designsystemet input hover (an inner outline doubling the border), drawn on the wrapper */
40
- .ObInputAdornments-root:hover:not(
41
- :focus-within,
42
- :has(.ObInputAdornments-field:disabled, .ObInputAdornments-field[readonly])
43
- ) {
44
- outline: var(--ds-border-width-default, 1px) solid var(--ds-color-neutral-border-default, #b3b3b3);
45
- outline-offset: -0.1px;
46
- }
47
-
48
- .ObInputAdornments-root:has(.ObInputAdornments-field:focus-visible) {
49
- box-shadow: var(--dsc-focus-boxShadow);
50
- outline: var(--dsc-focus-outline);
51
- outline-offset: var(--ds-border-width-focus, 3px);
52
- }
53
-
54
- .ObInputAdornments-root:has(.ObInputAdornments-field:disabled) {
55
- cursor: not-allowed;
56
- opacity: var(--ds-disabled-opacity, 0.3);
57
- }
58
-
59
- /* The bare input: all the visual styling lives on the wrapper instead */
60
- .ObInputAdornments-root .ObInputAdornments-field {
61
- flex: 1;
62
- min-inline-size: 0;
63
- font-size: var(--obInputAdornments-fontSize);
64
- background: transparent;
65
- border: none;
66
- }
67
-
68
- .ObInputAdornments-root .ObInputAdornments-field:disabled {
69
- opacity: 1;
70
- }
71
-
72
- .ObInputAdornments-root .ObInputAdornments-field:hover {
73
- outline: none;
74
- }
75
-
76
- .ObInputAdornments-root .ObInputAdornments-field:focus-visible {
77
- box-shadow: none;
78
- outline: none;
79
- outline-offset: 0;
80
- }
1
+ /*
2
+ * The wrapper is the visual input: it owns the border, background, hover and
3
+ * focus styling, while the inner Designsystemet input is rendered bare so the
4
+ * input and the adornments read as a single control.
5
+ */
6
+ .ObInputAdornments-root {
7
+ --obInputAdornments-padding: 0.25rem;
8
+ --obInputAdornments-gap: 0.25rem;
9
+ /* A Textfield's font size (`--dsc-input-font-size`) when no `data-size` is given. */
10
+ --obInputAdornments-fontSize: max(1rem, 1em);
11
+
12
+ display: flex;
13
+ align-items: center;
14
+ box-sizing: border-box;
15
+ gap: var(--obInputAdornments-gap);
16
+ padding: var(--obInputAdornments-padding);
17
+ inline-size: 100%;
18
+ color: var(--ds-color-neutral-text-default, #1e2b3c);
19
+ background-color: var(--ds-color-neutral-surface-default, #fff);
20
+ border: var(--ds-border-width-default, 1px) solid var(--ds-color-neutral-border-default, #b3b3b3);
21
+ border-radius: var(--ds-border-radius-md, 0.375rem);
22
+
23
+ /*
24
+ * As tall as a Textfield (`--dsc-input-size`), whatever the adornments are: the bare input and
25
+ * any button inside get the height left inside the padding and border, so a full-size
26
+ * Designsystemet button no longer makes the control taller than the fields beside it.
27
+ */
28
+ --obInputAdornments-innerSize: calc(
29
+ var(--ds-size-12) - 2 * var(--obInputAdornments-padding) - 2 * var(--ds-border-width-default, 1px)
30
+ );
31
+
32
+ min-block-size: var(--ds-size-12);
33
+ }
34
+
35
+ .ObInputAdornments-root > .ds-button {
36
+ --dsc-button-size: var(--obInputAdornments-innerSize);
37
+ --dsc-button-padding: 0 var(--ds-size-2);
38
+ }
39
+
40
+ /* Designsystemet components come in sm, md and lg; xs and xl are typography-only sizes */
41
+ .ObInputAdornments-root[data-size="sm"] {
42
+ --obInputAdornments-padding: 0.25rem;
43
+ --obInputAdornments-fontSize: var(--ds-font-size-2, 0.875rem);
44
+ }
45
+
46
+ .ObInputAdornments-root[data-size="md"] {
47
+ --obInputAdornments-padding: 0.25rem;
48
+ --obInputAdornments-fontSize: var(--ds-font-size-3, 1rem);
49
+ }
50
+
51
+ .ObInputAdornments-root[data-size="lg"] {
52
+ --obInputAdornments-padding: 0.375rem;
53
+ --obInputAdornments-fontSize: var(--ds-font-size-4, 1.125rem);
54
+ }
55
+
56
+ /* The Designsystemet input hover (an inner outline doubling the border), drawn on the wrapper */
57
+ .ObInputAdornments-root:hover:not(
58
+ :focus-within,
59
+ :has(.ObInputAdornments-field:disabled, .ObInputAdornments-field[readonly])
60
+ ) {
61
+ outline: var(--ds-border-width-default, 1px) solid var(--ds-color-neutral-border-default, #b3b3b3);
62
+ outline-offset: -0.1px;
63
+ }
64
+
65
+ .ObInputAdornments-root:has(.ObInputAdornments-field:focus-visible) {
66
+ box-shadow: var(--dsc-focus-boxShadow);
67
+ outline: var(--dsc-focus-outline);
68
+ outline-offset: var(--ds-border-width-focus, 3px);
69
+ }
70
+
71
+ .ObInputAdornments-root:has(.ObInputAdornments-field:disabled) {
72
+ cursor: not-allowed;
73
+ opacity: var(--ds-disabled-opacity, 0.3);
74
+ }
75
+
76
+ /* The bare input: all the visual styling lives on the wrapper instead */
77
+ .ObInputAdornments-root .ObInputAdornments-field {
78
+ --dsc-input-size: var(--obInputAdornments-innerSize);
79
+ /* The text starts where a Textfield's does (its inline padding, inside the wrapper's own). */
80
+ --dsc-input-padding-inline: calc(var(--ds-size-3) - var(--obInputAdornments-padding));
81
+
82
+ flex: 1;
83
+ min-inline-size: 0;
84
+ font-size: var(--obInputAdornments-fontSize);
85
+ background: transparent;
86
+ border: none;
87
+ }
88
+
89
+ .ObInputAdornments-root .ObInputAdornments-field:disabled {
90
+ opacity: 1;
91
+ }
92
+
93
+ .ObInputAdornments-root .ObInputAdornments-field:hover {
94
+ outline: none;
95
+ }
96
+
97
+ .ObInputAdornments-root .ObInputAdornments-field:focus-visible {
98
+ box-shadow: none;
99
+ outline: none;
100
+ outline-offset: 0;
101
+ }