@inspirare/design-system 0.0.24 → 0.0.25

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.
@@ -7,16 +7,34 @@ import { Select as s } from "@base-ui/react/select";
7
7
  //#region src/components/ui/select.tsx
8
8
  var c = o.createContext(null);
9
9
  function l({ children: e, ...t }) {
10
- let [n, r] = o.useState({}), a = o.useCallback((e, t) => {
10
+ let [n, r] = o.useState({}), a = {
11
+ ...o.useMemo(() => {
12
+ let t = {}, n = (e) => {
13
+ o.Children.forEach(e, (e) => {
14
+ if (o.isValidElement(e)) {
15
+ if (e.props && e.props.value !== void 0 && e.props.children) {
16
+ let n = "";
17
+ o.Children.forEach(e.props.children, (e) => {
18
+ (typeof e == "string" || typeof e == "number") && (n += e);
19
+ }), n && (t[e.props.value] = n);
20
+ }
21
+ e.props && e.props.children && n(e.props.children);
22
+ }
23
+ });
24
+ };
25
+ return n(e), t;
26
+ }, [e]),
27
+ ...n
28
+ }, l = o.useCallback((e, t) => {
11
29
  r((n) => n[e] === t ? n : {
12
30
  ...n,
13
31
  [e]: t
14
32
  });
15
- }, []), l = o.useCallback((e) => n[e], [n]);
33
+ }, []), u = o.useCallback((e) => a[e], [a]);
16
34
  return /* @__PURE__ */ i(c.Provider, {
17
35
  value: {
18
- registerLabel: a,
19
- getLabel: l
36
+ registerLabel: l,
37
+ getLabel: u
20
38
  },
21
39
  children: /* @__PURE__ */ i(s.Root, {
22
40
  ...t,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@inspirare/design-system",
3
3
  "private": false,
4
- "version": "0.0.24",
4
+ "version": "0.0.25",
5
5
  "type": "module",
6
6
  "sideEffects": [
7
7
  "*.css"
@@ -2,6 +2,7 @@ import { render } from '@testing-library/react';
2
2
  import { axe } from 'jest-axe';
3
3
  import { Select, SelectTrigger, SelectValue, SelectContent, SelectItem } from "./select";
4
4
 
5
+
5
6
  describe('Select Accessibility', () => {
6
7
  it('should have no accessibility violations in basic render', async () => {
7
8
  const Wrapper = () => (
@@ -11,4 +12,21 @@ describe('Select Accessibility', () => {
11
12
  const results = await axe(container);
12
13
  expect(results).toHaveNoViolations();
13
14
  });
14
- });
15
+
16
+ it('should display the default label instead of the value on initial render', () => {
17
+ const Wrapper = () => (
18
+ <Select defaultValue="dark">
19
+ <SelectTrigger aria-label="Select a theme" className="w-[180px]">
20
+ <SelectValue placeholder="Theme" />
21
+ </SelectTrigger>
22
+ <SelectContent>
23
+ <SelectItem value="light">Light</SelectItem>
24
+ <SelectItem value="dark">Dark Theme</SelectItem>
25
+ <SelectItem value="system">System</SelectItem>
26
+ </SelectContent>
27
+ </Select>
28
+ );
29
+ const { container } = render(<Wrapper />);
30
+ expect(container.textContent).toContain('Dark Theme');
31
+ });
32
+ });
@@ -12,10 +12,37 @@ const SelectContext = React.createContext<{
12
12
  } | null>(null);
13
13
 
14
14
  function Select({ children, ...props }: React.ComponentProps<typeof SelectPrimitive.Root>) {
15
- const [labels, setLabels] = React.useState<Record<string, string>>({});
15
+ const [dynamicLabels, setDynamicLabels] = React.useState<Record<string, string>>({});
16
16
 
17
+ const staticLabels = React.useMemo(() => {
18
+ const result: Record<string, string> = {};
19
+ const extract = (node: React.ReactNode) => {
20
+ React.Children.forEach(node, (child) => {
21
+ if (!React.isValidElement(child)) return;
22
+ if ((child as any).props && (child as any).props.value !== undefined) {
23
+ if ((child as any).props.children) {
24
+ let label = "";
25
+ React.Children.forEach((child as any).props.children, (grandChild) => {
26
+ if (typeof grandChild === "string" || typeof grandChild === "number") {
27
+ label += grandChild;
28
+ }
29
+ });
30
+ if (label) result[(child as any).props.value] = label;
31
+ }
32
+ }
33
+ if ((child as any).props && (child as any).props.children) {
34
+ extract((child as any).props.children);
35
+ }
36
+ });
37
+ };
38
+ extract(children);
39
+ return result;
40
+ }, [children]);
41
+
42
+ const labels = { ...staticLabels, ...dynamicLabels };
43
+
17
44
  const registerLabel = React.useCallback((val: any, label: string) => {
18
- setLabels((prev) => {
45
+ setDynamicLabels((prev) => {
19
46
  if (prev[val] === label) return prev;
20
47
  return { ...prev, [val]: label };
21
48
  });