@kimdw-rtk/ui 0.1.16 → 0.1.17

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.
@@ -11,15 +11,33 @@ var SelectOptionList = require('./SelectOptionList.cjs');
11
11
  var SelectTrigger = require('./SelectTrigger.cjs');
12
12
  var sx = require('../../styles/sx.cjs');
13
13
 
14
+ const getDefaultSelectedLabel = (children, value) => {
15
+ if (!value) {
16
+ return undefined;
17
+ }
18
+ for (const child of react.Children.toArray(children)) {
19
+ if (!react.isValidElement(child)) {
20
+ continue;
21
+ }
22
+ if (child.props.value === value) {
23
+ return child.props.children;
24
+ }
25
+ }
26
+ return undefined;
27
+ };
14
28
  const Select = react.forwardRef(({ children, className, style, name, defaultValue, width = '100%', size = 'md', sx: propSx, variant = 'outlined', onChange, ...props }, ref) => {
15
29
  const containerRef = react.useRef(null);
16
30
  const targetRef = utils.useCombinedRefs(ref, containerRef);
17
31
  const [state, dispatch] = react.useReducer(SelectContext.selectReducer, {
18
32
  isActive: false,
33
+ selected: defaultValue,
19
34
  containerRef,
20
35
  defaultValue,
21
36
  items: new Map(),
22
37
  });
38
+ const selected = state.selected ?? defaultValue;
39
+ const selectedLabel = state.items.get(selected || '') ??
40
+ getDefaultSelectedLabel(children, selected);
23
41
  react.useEffect(() => {
24
42
  const container = containerRef.current;
25
43
  if (!container) {
@@ -43,7 +61,7 @@ const Select = react.forwardRef(({ children, className, style, name, defaultValu
43
61
  onChange(state.selected);
44
62
  //eslint-disable-next-line
45
63
  }, [state.selected]);
46
- return (jsxRuntime.jsx(SelectContext.SelectContext.Provider, { value: { state, dispatch }, children: jsxRuntime.jsxs("div", { ref: targetRef, className: clsx.clsx(Select_css.select({ size }), className, sx.sx(propSx)), style: { ...style, width }, ...props, children: [jsxRuntime.jsx(SelectTrigger.default, { variant: variant, children: state.selected !== null && state.items.get(state.selected || '') }), jsxRuntime.jsx(SelectOptionList.default, { children: children }), jsxRuntime.jsx("input", { name: name, type: "hidden", value: state.selected || '' })] }) }));
64
+ return (jsxRuntime.jsx(SelectContext.SelectContext.Provider, { value: { state, dispatch }, children: jsxRuntime.jsxs("div", { ref: targetRef, className: clsx.clsx(Select_css.select({ size }), className, sx.sx(propSx)), style: { ...style, width }, ...props, children: [jsxRuntime.jsx(SelectTrigger.default, { variant: variant, children: selectedLabel }), jsxRuntime.jsx(SelectOptionList.default, { children: children }), jsxRuntime.jsx("input", { name: name, type: "hidden", value: selected || '' })] }) }));
47
65
  });
48
66
  Select.displayName = 'Select';
49
67
 
@@ -1 +1 @@
1
- {"version":3,"file":"Select.d.ts","sourceRoot":"","sources":["../../../src/components/Select/Select.tsx"],"names":[],"mappings":"AAEA,OAAO,EAKL,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,SAAS,EACf,MAAM,OAAO,CAAC;AAMf,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAE1C,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAGlC,OAAO,aAAa,MAAM,iBAAiB,CAAC;AAE5C,UAAU,WACR,SAAQ,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,MAAM,CAAC,EAAE,KAAK,GAAG,UAAU,CAAC;IACrE,GAAG,CAAC,EAAE,SAAS,CAAC;QAAE,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC,CAAC;IAC3C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;IAC/B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,EAAE,cAAc,CAAC,OAAO,aAAa,CAAC,CAAC,SAAS,CAAC,CAAC;IAC1D,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,KAAK,IAAI,CAAC;CAChD;AAED,eAAO,MAAM,MAAM,qHA0ElB,CAAC;AAGF,OAAO,EAAE,CAAC,IAAI,SAAS,EAAE,CAAC"}
1
+ {"version":3,"file":"Select.d.ts","sourceRoot":"","sources":["../../../src/components/Select/Select.tsx"],"names":[],"mappings":"AAEA,OAAO,EAQL,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,SAAS,EACf,MAAM,OAAO,CAAC;AAMf,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAE1C,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAGlC,OAAO,aAAa,MAAM,iBAAiB,CAAC;AAE5C,UAAU,WACR,SAAQ,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,MAAM,CAAC,EAAE,KAAK,GAAG,UAAU,CAAC;IACrE,GAAG,CAAC,EAAE,SAAS,CAAC;QAAE,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC,CAAC;IAC3C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;IAC/B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,EAAE,cAAc,CAAC,OAAO,aAAa,CAAC,CAAC,SAAS,CAAC,CAAC;IAC1D,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,KAAK,IAAI,CAAC;CAChD;AAuBD,eAAO,MAAM,MAAM,qHA6ElB,CAAC;AAGF,OAAO,EAAE,CAAC,IAAI,SAAS,EAAE,CAAC"}
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
  import { jsx, jsxs } from 'react/jsx-runtime';
3
- import { forwardRef, useRef, useReducer, useEffect } from 'react';
3
+ import { forwardRef, useRef, useReducer, useEffect, Children, isValidElement } from 'react';
4
4
  import { useCombinedRefs } from '@kimdw-rtk/utils';
5
5
  import { clsx } from '../../node_modules/.pnpm/clsx@2.1.1/node_modules/clsx/dist/clsx.js';
6
6
  import { select } from './Select.css.vanilla.js';
@@ -11,15 +11,33 @@ import SelectOptionList from './SelectOptionList.js';
11
11
  import SelectTrigger from './SelectTrigger.js';
12
12
  import { sx } from '../../styles/sx.js';
13
13
 
14
+ const getDefaultSelectedLabel = (children, value) => {
15
+ if (!value) {
16
+ return undefined;
17
+ }
18
+ for (const child of Children.toArray(children)) {
19
+ if (!isValidElement(child)) {
20
+ continue;
21
+ }
22
+ if (child.props.value === value) {
23
+ return child.props.children;
24
+ }
25
+ }
26
+ return undefined;
27
+ };
14
28
  const Select = forwardRef(({ children, className, style, name, defaultValue, width = '100%', size = 'md', sx: propSx, variant = 'outlined', onChange, ...props }, ref) => {
15
29
  const containerRef = useRef(null);
16
30
  const targetRef = useCombinedRefs(ref, containerRef);
17
31
  const [state, dispatch] = useReducer(selectReducer, {
18
32
  isActive: false,
33
+ selected: defaultValue,
19
34
  containerRef,
20
35
  defaultValue,
21
36
  items: new Map(),
22
37
  });
38
+ const selected = state.selected ?? defaultValue;
39
+ const selectedLabel = state.items.get(selected || '') ??
40
+ getDefaultSelectedLabel(children, selected);
23
41
  useEffect(() => {
24
42
  const container = containerRef.current;
25
43
  if (!container) {
@@ -43,7 +61,7 @@ const Select = forwardRef(({ children, className, style, name, defaultValue, wid
43
61
  onChange(state.selected);
44
62
  //eslint-disable-next-line
45
63
  }, [state.selected]);
46
- return (jsx(SelectContext.Provider, { value: { state, dispatch }, children: jsxs("div", { ref: targetRef, className: clsx(select({ size }), className, sx(propSx)), style: { ...style, width }, ...props, children: [jsx(SelectTrigger, { variant: variant, children: state.selected !== null && state.items.get(state.selected || '') }), jsx(SelectOptionList, { children: children }), jsx("input", { name: name, type: "hidden", value: state.selected || '' })] }) }));
64
+ return (jsx(SelectContext.Provider, { value: { state, dispatch }, children: jsxs("div", { ref: targetRef, className: clsx(select({ size }), className, sx(propSx)), style: { ...style, width }, ...props, children: [jsx(SelectTrigger, { variant: variant, children: selectedLabel }), jsx(SelectOptionList, { children: children }), jsx("input", { name: name, type: "hidden", value: selected || '' })] }) }));
47
65
  });
48
66
  Select.displayName = 'Select';
49
67
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kimdw-rtk/ui",
3
- "version": "0.1.16",
3
+ "version": "0.1.17",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "imports": {