@xyhp915/slack-base-ui 0.0.7 → 0.0.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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@xyhp915/slack-base-ui",
3
3
  "main": "libs/index.js",
4
4
  "types": "libs/index.d.ts",
5
- "version": "0.0.7",
5
+ "version": "0.0.8",
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "dev": "vite",
@@ -61,6 +61,30 @@ export const Select = React.forwardRef<HTMLButtonElement, SelectProps>(
61
61
  ) => {
62
62
  const generatedId = React.useId()
63
63
  const triggerId = id ?? generatedId
64
+ const isControlled = value !== undefined
65
+ const [internalValue, setInternalValue] = React.useState<string | null>(defaultValue ?? null)
66
+
67
+ const allOptions = React.useMemo(
68
+ () => (groups ? groups.flatMap((group) => group.options) : options ?? []),
69
+ [groups, options],
70
+ )
71
+
72
+ const selectedValue = isControlled ? value ?? null : internalValue
73
+ const selectedLabel = React.useMemo(
74
+ () => allOptions.find((opt) => opt.value === selectedValue)?.label,
75
+ [allOptions, selectedValue],
76
+ )
77
+
78
+ const handleValueChange = React.useCallback(
79
+ (nextValue: string | null) => {
80
+ if (!isControlled) {
81
+ setInternalValue(nextValue)
82
+ }
83
+
84
+ onValueChange?.(nextValue)
85
+ },
86
+ [isControlled, onValueChange],
87
+ )
64
88
 
65
89
  return (
66
90
  <div className={clsx('flex flex-col gap-1.5', fullWidth && 'w-full')}>
@@ -75,9 +99,9 @@ export const Select = React.forwardRef<HTMLButtonElement, SelectProps>(
75
99
  )}
76
100
 
77
101
  <BaseSelect.Root
78
- value={value}
79
- defaultValue={defaultValue}
80
- onValueChange={onValueChange}
102
+ value={isControlled ? value : undefined}
103
+ defaultValue={isControlled ? undefined : defaultValue}
104
+ onValueChange={handleValueChange}
81
105
  disabled={disabled}
82
106
  required={required}
83
107
  >
@@ -99,7 +123,9 @@ export const Select = React.forwardRef<HTMLButtonElement, SelectProps>(
99
123
  <BaseSelect.Value
100
124
  className="flex-1 text-left data-[placeholder]:text-(--text-muted)"
101
125
  placeholder={placeholder}
102
- />
126
+ >
127
+ {selectedLabel}
128
+ </BaseSelect.Value>
103
129
  <BaseSelect.Icon className="shrink-0 text-(--text-muted)">
104
130
  <ChevronDown size={14} />
105
131
  </BaseSelect.Icon>