@synerise/ds-input 1.7.11 → 1.7.13

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.
Files changed (44) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/CLAUDE.md +309 -0
  3. package/dist/AutosizeInput/AutosizeInput.d.ts +6 -7
  4. package/dist/AutosizeInput/AutosizeInput.js +39 -116
  5. package/dist/AutosizeInput/AutosizeInput.types.d.ts +5 -18
  6. package/dist/AutosizeInput/useAutosizeWidth.d.ts +18 -0
  7. package/dist/AutosizeInput/useAutosizeWidth.js +145 -0
  8. package/dist/AutosizeInput/useAutosizeWidth.types.d.ts +29 -0
  9. package/dist/AutosizeInput/useAutosizeWidth.types.js +1 -0
  10. package/dist/AutosizeInput/useStretchToFit.d.ts +26 -0
  11. package/dist/AutosizeInput/useStretchToFit.js +31 -0
  12. package/dist/Input.d.ts +13 -3
  13. package/dist/Input.js +170 -81
  14. package/dist/Input.styles.d.ts +32 -11
  15. package/dist/Input.styles.js +64 -38
  16. package/dist/Input.types.d.ts +23 -7
  17. package/dist/InputGroup.d.ts +1 -1
  18. package/dist/InputGroup.js +11 -4
  19. package/dist/InputGroup.styles.d.ts +1 -0
  20. package/dist/InputGroup.styles.js +9 -2
  21. package/dist/InputGroup.types.d.ts +22 -3
  22. package/dist/InputMultivalue/InputMultivalue.styles.js +1 -1
  23. package/dist/TextArea.d.ts +2 -2
  24. package/dist/TextArea.js +4 -5
  25. package/dist/Textarea/Textarea.d.ts +4 -3
  26. package/dist/Textarea/Textarea.js +33 -4
  27. package/dist/Textarea/Textarea.styles.js +2 -2
  28. package/dist/Textarea/Textarea.types.d.ts +13 -8
  29. package/dist/components/ExpandableWrapper.d.ts +1 -2
  30. package/dist/components/index.d.ts +0 -1
  31. package/dist/components/index.js +0 -2
  32. package/dist/hooks/useElementFocus.d.ts +3 -2
  33. package/dist/hooks/useInputAddonHeight.d.ts +1 -2
  34. package/dist/hooks/useInputAddonHeight.js +1 -1
  35. package/dist/hooks/useTextareaAutosize.d.ts +53 -0
  36. package/dist/hooks/useTextareaAutosize.js +82 -0
  37. package/dist/index.d.ts +6 -2
  38. package/dist/index.js +8 -3
  39. package/dist/utils/getCharCount.d.ts +3 -2
  40. package/package.json +9 -12
  41. package/dist/assets/style/index-tn0RQdqM.css +0 -0
  42. package/dist/components/AutosizeWrapper.d.ts +0 -17
  43. package/dist/components/AutosizeWrapper.js +0 -18
  44. package/dist/style/index.css +0 -1
package/CHANGELOG.md CHANGED
@@ -3,6 +3,14 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [1.7.13](https://github.com/synerise/synerise-design/compare/@synerise/ds-input@1.7.12...@synerise/ds-input@1.7.13) (2026-07-23)
7
+
8
+ **Note:** Version bump only for package @synerise/ds-input
9
+
10
+ ## [1.7.12](https://github.com/synerise/synerise-design/compare/@synerise/ds-input@1.7.11...@synerise/ds-input@1.7.12) (2026-07-09)
11
+
12
+ **Note:** Version bump only for package @synerise/ds-input
13
+
6
14
  ## [1.7.11](https://github.com/synerise/synerise-design/compare/@synerise/ds-input@1.7.10...@synerise/ds-input@1.7.11) (2026-06-17)
7
15
 
8
16
  **Note:** Version bump only for package @synerise/ds-input
package/CLAUDE.md ADDED
@@ -0,0 +1,309 @@
1
+ # Input (`@synerise/ds-input`)
2
+
3
+ > A family of enhanced input components — text input, textarea, password, multi-value tags, and auto-sizing — each wrapped in a `FormField` shell that adds label, description, error text, counter, and tooltip.
4
+
5
+ ## Package structure
6
+
7
+ ```
8
+ src/
9
+ Input.tsx — main enhanced input (forwardRef to outer div)
10
+ Input.types.ts — BaseProps, InputProps, AutoResizeProp, StyledInput
11
+ Input.styles.tsx — all styled-components; also exports as InputStyles namespace
12
+ TextArea.tsx — enhanced textarea (FormField wrapper)
13
+ TextArea.types.ts — TextareaProps
14
+ Textarea/
15
+ Textarea.tsx — DSRawTextArea styled component (inner implementation)
16
+ Textarea.types.ts — RawTextAreaProps (TextAreaProps is @deprecated alias)
17
+ Textarea.styles.ts — textarea styled-components
18
+ InputGroup.tsx — group wrapper for multiple inputs
19
+ InputGroup.types.ts — Props (extends Ant GroupProps)
20
+ InputMultivalue/
21
+ InputMultivalue.tsx — tag-style multi-value input; Enter to add values
22
+ InputMultivalue.types.ts — InputMultivalueProps
23
+ Elements/Value.tsx — individual tag chip with remove button
24
+ PasswordInput.tsx — wraps Input with show/hide toggle (uses icon1 slot)
25
+ PasswordInput.types.tsx — PasswordInputProps, PasswordInputTexts
26
+ AutosizeInput/
27
+ AutosizeInput.tsx — standalone auto-resizing <input> (not Ant Design based)
28
+ AutosizeInput.types.ts — AutosizeInputProps, AutosizeWrapperProps, AutosizeInputRefType
29
+ utils/calculateInputWidth.ts
30
+ components/
31
+ AutosizeWrapper.tsx — conditionally wraps input with autosize behaviour
32
+ ElementIcons.tsx — renders icon1/icon2 with Tooltip wrappers and expand icon
33
+ ExpandableWrapper.tsx — expandable textarea overlay shown when input text overflows
34
+ hooks/
35
+ useCounterLimit.tsx — generates the counter display ReactNode (rightSide slot)
36
+ useElementFocus.ts — returns focus handler for icon click
37
+ useInputAddonHeight.ts — measures input height for matching addon/prefixel height
38
+ utils/getCharCount.ts — counts characters from controlled value
39
+ index.ts — public exports
40
+ ```
41
+
42
+ ## Public exports
43
+
44
+ ### `Input`
45
+
46
+ Enhanced text input with `FormField` wrapper. `forwardRef<HTMLDivElement, InputProps>` — **ref points to the outer wrapper `<div>`, not the `<input>` element**. Use `handleInputRef` to access the underlying `HTMLInputElement`.
47
+
48
+ Props (`InputProps` = `BaseProps & AntdInputProps`):
49
+
50
+ #### DS-specific props (`BaseProps`)
51
+
52
+ | Prop | Type | Default | Description |
53
+ |------|------|---------|-------------|
54
+ | `label` | `ReactNode` | — | Field label (from `FormFieldCommonProps`) |
55
+ | `description` | `string` | — | Helper text below the input |
56
+ | `errorText` | `string` | — | Error message; also sets error styling |
57
+ | `error` | `boolean` | — | Sets error styling without a message |
58
+ | `tooltip` | `ReactNode` | — | Tooltip content on the label |
59
+ | `tooltipConfig` | `TooltipProps` | — | Tooltip configuration object |
60
+ | `counterLimit` | `number` | — | Max chars; shows `current/max` counter; blocks input at limit |
61
+ | `renderCustomCounter` | `(count?: number) => ReactNode` | — | Custom counter renderer (replaces default `current/max`) |
62
+ | `icon1` | `ReactElement` | — | Left action icon (inside the input, right side) |
63
+ | `icon1Tooltip` | `ReactElement` | — | Tooltip for `icon1` |
64
+ | `icon2` | `ReactElement` | — | Second action icon (stacks with `icon1`) |
65
+ | `icon2Tooltip` | `ReactElement` | — | Tooltip for `icon2` |
66
+ | `prefixel` | `ReactNode` | — | Attached left addon block (rendered via `addonBefore`; height matched to input) |
67
+ | `suffixel` | `ReactNode` | — | Attached right addon block (rendered via `addonAfter`; height matched to input) |
68
+ | `handleInputRef` | `(ref: MutableRefObject<HTMLInputElement \| null>) => void` | — | Callback receiving the underlying `<input>` ref after mount |
69
+ | `resetMargin` | `boolean` | — | Removes outer wrapper margin |
70
+ | `expandable` | `boolean` | — | Shows expand icon when text overflows; click opens a textarea overlay |
71
+ | `expandableTooltip` | `ReactNode` | — | Tooltip on the expand icon |
72
+ | `autoResize` | `AutoResizeProp` | — | Auto-sizes input width to its text content |
73
+ | `autoResizeProps` | `Partial<Pick<AutosizeInputProps, 'placeholderIsMinWidth' \| 'wrapperClassName' \| 'wrapperStyle' \| 'extraWidth'>>` | — | Fine-tuning for `autoResize` behaviour |
74
+ | `className` | `string` | — | Applied to the outer wrapper |
75
+
76
+ All standard Ant Design `InputProps` are also accepted and forwarded (e.g. `value`, `onChange`, `disabled`, `size`, `prefix`, `suffix`, `addonBefore`, `addonAfter`, `allowClear`, `defaultValue`, `type`, `onPressEnter`, etc.).
77
+
78
+ #### `AutoResizeProp`
79
+
80
+ ```ts
81
+ type AutoResizeProp =
82
+ | boolean
83
+ | { minWidth: string; maxWidth?: string; stretchToFit?: boolean };
84
+ ```
85
+
86
+ `stretchToFit: true` makes the input fill its containing element. **Important**: the flex parent must have `min-width: 0; flex-grow: 1` applied to work correctly.
87
+
88
+ ### `RawInput`
89
+
90
+ Bare Ant Design `Input` that adds the DS `error` CSS class. No `FormField` wrapper, no label/description/counter.
91
+
92
+ ```tsx
93
+ export const RawInput = (props: InputProps) => (
94
+ <S.AntdInput className={props.error ? 'error' : ''} {..props} />
95
+ );
96
+ ```
97
+
98
+ ### `TextArea`
99
+
100
+ Enhanced textarea with `FormField` wrapper. Same label/description/error/counter/icon features as `Input`. Does **not** support `expandable`, `expandableTooltip`, `autoResize`, or `autoResizeProps` (these are omitted from `TextareaProps`).
101
+
102
+ `TextareaProps` = `RawTextAreaProps & Omit<BaseProps<HTMLTextAreaElement>, 'expandable' | 'expandableTooltip' | 'autoResize' | 'autoResizeProps'>`
103
+
104
+ `RawTextAreaProps` = Ant Design `TextAreaProps` + `error?: boolean`, `errorText?: string | ReactNode`, `wrapperStyle?: CSSProperties`, `resize?: ResizeProperty`
105
+
106
+ ### `RawTextArea`
107
+
108
+ Styled Ant Design `TextArea` component (exported from `Input.styles.tsx`). No FormField wrapper.
109
+
110
+ ### `DSRawTextArea`
111
+
112
+ The inner `Textarea/Textarea.tsx` raw DS textarea. Same API as `RawTextAreaProps`.
113
+
114
+ ### `InputGroup`
115
+
116
+ Wraps Ant Design `Input.Group` with `FormField` support. Uses the standard `FormFieldCommonProps.errorText` for its error message. Children are each wrapped in a `ds-input-group-item` div.
117
+
118
+ Props (`Props` = `FormFieldCommonProps` + the below):
119
+
120
+ | Prop | Type | Default | Description |
121
+ |------|------|---------|-------------|
122
+ | `compact` | `boolean` | `false` | Compact adjacent inputs (removes borders between them) |
123
+ | `growItem` | `'first' \| 'last'` | `'last'` | Which item stretches to fill the row (others keep natural width). `'last'` = trailing control grows (e.g. fixed-width Select left, input fills right). `'first'` = flipped: leading input grows, fixed-width trailing control (e.g. ColorPicker) stays compact right. |
124
+ | `size` | `'large' \| 'default' \| 'small'` | `'default'` | Applied to the Ant `Input.Group` |
125
+ | `errorText` | `ReactNode` | — | Error message displayed below the group (from `FormFieldCommonProps`) |
126
+ | `errors` | `string[]` | — | **@deprecated** — use `errorText`. Antd-proprietary array of error strings (each rendered as its own `ErrorText` line); ignored when `errorText` is provided |
127
+ | `resetMargin` | `boolean` | — | Removes outer wrapper margin |
128
+ | `label` | `ReactNode` | — | Group label |
129
+ | `description` | `string` | — | Helper text |
130
+ | `tooltip` | `ReactNode` | — | Tooltip on label |
131
+ | `tooltipConfig` | `TooltipProps` | — | Tooltip configuration |
132
+
133
+ ### `InputMultivalue`
134
+
135
+ Tag-style input that accumulates values. Press **Enter** to add the current text as a new tag. Tags can be individually removed.
136
+
137
+ **Semi-controlled bug**: `selectedValues` state is initialised from the `values` prop on first render only. External changes to `values` after mount do **not** update the internal state.
138
+
139
+ Props (`InputMultivalueProps`):
140
+
141
+ | Prop | Type | Default | Description |
142
+ |------|------|---------|-------------|
143
+ | `values` | `string[]` | — | **Required.** Initial set of tag values |
144
+ | `onChange` | `(values: string[]) => void` | — | Called with the updated values array |
145
+ | `error` | `boolean` | — | Sets error styling |
146
+ | `errorText` | `ReactNode \| string` | — | Error message |
147
+ | `label` | `ReactNode \| string` | — | Field label |
148
+ | `description` | `ReactNode \| string` | — | Helper text |
149
+ | `disabled` | `boolean` | — | Disables interaction |
150
+ | `onBlur` | `() => void` | — | Called on blur |
151
+ | `onFocus` | `() => void` | — | Called on focus |
152
+ | `maxLength` | `number` | — | Max length of the current entry (not the total values count) |
153
+ | `className` | `string` | — | Applied to the input wrapper |
154
+
155
+ ### `PasswordInput`
156
+
157
+ Wraps `Input` with a show/hide password toggle in the `icon1` slot. Accepts all `InputProps` except `icon1`, `icon1Tooltip`, `icon2`, `icon2Tooltip`, `suffixel`, `prefixel`, `type`, and `expandableTooltip`.
158
+
159
+ Additional prop: `texts?: Partial<PasswordInputTexts>` to override show/hide label strings. Defaults come from `react-intl` (`DS.INPUT.SHOW-PASSWORD` / `DS.INPUT.HIDE-PASSWORD`).
160
+
161
+ ### `AutosizeInput`
162
+
163
+ Standalone auto-resizing `<input>` (not Ant Design based). Renders a hidden sizer element to measure text width and adjusts the input accordingly.
164
+
165
+ Props (`AutosizeInputProps`): Extends `InputHTMLAttributes<HTMLInputElement>` (omitting `size`) plus:
166
+
167
+ | Prop | Type | Description |
168
+ |------|------|-------------|
169
+ | `children` | `ReactNode` | **Required.** The actual `<input>` element to resize |
170
+ | `placeholderIsMinWidth` | `boolean` | Use placeholder width as minimum width |
171
+ | `wrapperClassName` | `string` | Class on the wrapper div |
172
+ | `wrapperStyle` | `CSSProperties` | Style on the wrapper div |
173
+ | `extraWidth` | `number \| string` | Extra pixels added to the calculated width |
174
+ | `minWidth` | `number \| string` | Minimum width override |
175
+ | `onAutosize` | `(newWidth: number) => void` | Called after resize |
176
+ | `preAutosize` | `(newWidth: number) => void` | Called before resize |
177
+
178
+ `AutosizeInputRefType` (from `useImperativeHandle`):
179
+
180
+ ```ts
181
+ {
182
+ inputRef: MutableRefObject<HTMLInputElement | null>;
183
+ sizerRef: RefObject<HTMLDivElement>;
184
+ wrapperRef: RefObject<HTMLDivElement>;
185
+ inputWrapperRef: RefObject<HTMLElement>;
186
+ placeholderSizerRef: RefObject<HTMLDivElement>;
187
+ copyInputStyles: () => void;
188
+ updateInputWidth: () => void;
189
+ }
190
+ ```
191
+
192
+ ### `AutosizeWrapper`
193
+
194
+ Conditionally applies `AutosizeInput` behaviour when `autoResize` is truthy. Used internally by `Input`.
195
+
196
+ ### `Label` _(deprecated)_
197
+
198
+ Re-export of `FormFieldLabel` from `@synerise/ds-form-field`. **Import directly from `@synerise/ds-form-field` instead.**
199
+
200
+ ### `InputStyles`
201
+
202
+ Namespace export of all styled-components from `Input.styles.tsx`. Useful for extending via `styled(InputStyles.OuterWrapper)`.
203
+
204
+ ### Types exported
205
+
206
+ | Export | Description |
207
+ |--------|-------------|
208
+ | `InputProps` | Main input props (`BaseProps & AntdInputProps`) |
209
+ | `BaseProps` | DS-specific additions to Ant inputs |
210
+ | `AutoResizeProp` | `boolean \| { minWidth, maxWidth?, stretchToFit? }` |
211
+ | `TextareaProps` | Enhanced textarea props |
212
+ | `InputMultivalueProps` | Multi-value tag input props |
213
+ | `AutosizeInputProps` | Standalone autosize input props |
214
+ | `AutosizeWrapperProps` | Autosize wrapper props |
215
+ | `AutosizeInputRefType` | Imperative ref shape for `AutosizeInput` |
216
+ | `PasswordInputProps` | Password input props |
217
+ | `PasswordInputTexts` | Show/hide text labels |
218
+ | `StyledInput` | Helper type for styled-component extensions of `Input` |
219
+ | `EnhancedProps` | **@deprecated** — use `InputProps` or `TextareaProps` |
220
+ | `Props` (from Input.types) | **@deprecated** — use `InputProps` or `TextareaProps` |
221
+
222
+ ## Usage patterns
223
+
224
+ ```tsx
225
+ import { Input, TextArea, InputGroup, InputMultivalue, PasswordInput } from '@synerise/ds-input';
226
+
227
+ // Basic input with label and counter
228
+ <Input
229
+ label="Email"
230
+ value={value}
231
+ onChange={e => setValue(e.target.value)}
232
+ counterLimit={100}
233
+ errorText={error}
234
+ />
235
+
236
+ // Password input
237
+ <PasswordInput
238
+ label="Password"
239
+ value={password}
240
+ onChange={e => setPassword(e.target.value)}
241
+ />
242
+
243
+ // Multi-value tags
244
+ <InputMultivalue
245
+ label="Tags"
246
+ values={tags}
247
+ onChange={setTags}
248
+ />
249
+
250
+ // Auto-resizing input
251
+ <Input
252
+ value={value}
253
+ onChange={e => setValue(e.target.value)}
254
+ autoResize={{ minWidth: '80px', maxWidth: '400px' }}
255
+ />
256
+
257
+ // Input with left/right addon blocks
258
+ <Input
259
+ value={value}
260
+ onChange={e => setValue(e.target.value)}
261
+ prefixel={<span>https://</span>}
262
+ suffixel={<span>.com</span>}
263
+ />
264
+
265
+ // Group of inputs
266
+ <InputGroup label="Address" errorText={error} compact>
267
+ <Input placeholder="Street" />
268
+ <Input placeholder="City" />
269
+ </InputGroup>
270
+ ```
271
+
272
+ ## Custom hooks
273
+
274
+ ### `useCounterLimit`
275
+
276
+ Generates the counter `ReactNode` (rendered in `FormField`'s `rightSide` slot). Returns a `<Counter>` element showing `charCount/counterLimit` or the result of `renderCustomCounter`. Returns `undefined` when neither prop is provided.
277
+
278
+ ### `useElementFocus`
279
+
280
+ Returns a click handler that calls `.focus()` on the passed ref. Used to focus the input when an icon is clicked.
281
+
282
+ ### `useInputAddonHeight`
283
+
284
+ Reads `inputRef.current.input.offsetHeight` after mount and returns `{ inputAddonHeight }`. Used to match the height of `prefixel`/`suffixel` addon blocks to the input.
285
+
286
+ ## Styling
287
+
288
+ Styles live in `Input.styles.tsx` using `@synerise/ds-core` theme tokens. Error state is applied via `.error` CSS class on the Ant Design input. Icon padding is calculated dynamically: each icon adds 24 px + 4 px gap via `getIconsWidth`.
289
+
290
+ ## Key dependencies
291
+
292
+ - `antd` — `Input`, `Input.Group`, and `TextArea` base components
293
+ - `@synerise/ds-form-field` — `FormField` wrapper providing label, tooltip, description, errorText slots
294
+ - `@floating-ui/react` — `useMergeRefs` for combining forwarded ref with internal wrapper ref
295
+ - `@synerise/ds-utils` — `useResizeObserver` for `stretchToFit` auto-resize
296
+ - `uuid` — generates a stable `id` per instance for `<label htmlFor>` association
297
+ - `react-intl` — default `showText`/`hideText` strings in `PasswordInput`
298
+
299
+ ## Implementation notes
300
+
301
+ - **`Input` ref targets the outer `<div>`**, not the `<input>` element. Pass `handleInputRef` to get the `HTMLInputElement` ref.
302
+ - **`counterLimit` blocks input** — the `onChange` handler returns early when the new value would exceed the limit. The underlying Ant `input.maxLength` is **not** used.
303
+ - **`expandable` overlay**: when text overflows, an expand icon appears. Clicking it focuses a `<textarea>` overlay positioned over the input. On blur the textarea collapses and the value is synced back to the hidden input.
304
+ - **`autoResize` and `expandable` together**: `overflown` is computed from `AutosizeInput`'s reported width vs. `minWidth`, enabling the expand icon to appear correctly.
305
+ - **`InputMultivalue` is semi-controlled**: `selectedValues` initialises from `values` on mount only. To reset the values externally, pass a new `key` prop.
306
+ - **`@ts-expect-error`** in `ExpandableWrapper.tsx` for setting `.value` on `TextAreaRef` — the Ant Design type does not expose `.value` directly.
307
+ - **Uses Vitest** for testing.
308
+ - **`Label` re-export is `@deprecated`**: import `FormFieldLabel` from `@synerise/ds-form-field` directly.
309
+ - **`DSRawTextArea`** (`RawTextArea as DSRawTextArea` in `index.ts`) is a second raw textarea export from the inner `Textarea/Textarea.tsx`; it is distinct from `RawTextArea` which comes from `Input.styles.tsx`.
@@ -1,5 +1,9 @@
1
1
  import { default as React, CSSProperties } from 'react';
2
- import { AutosizeInputRefType } from './AutosizeInput.types';
2
+ /**
3
+ * Native auto-width `<input>`: renders its own input plus a hidden sizer, and
4
+ * sizes the input to the text width via `useAutosizeWidth`. The forwarded ref
5
+ * points at the underlying `<input>`.
6
+ */
3
7
  declare const AutosizeInput: React.ForwardRefExoticComponent<Omit<React.InputHTMLAttributes<HTMLInputElement>, "size"> & {
4
8
  placeholderIsMinWidth?: boolean;
5
9
  wrapperClassName?: string;
@@ -7,10 +11,5 @@ declare const AutosizeInput: React.ForwardRefExoticComponent<Omit<React.InputHTM
7
11
  extraWidth?: number | string;
8
12
  minWidth?: number | string;
9
13
  onAutosize?: (newWidth: number) => void;
10
- preAutosize?: (newWidth: number) => void;
11
- transformRef?: (element: HTMLElement) => HTMLElement;
12
- transformWrapperRef?: (element: HTMLElement) => HTMLElement;
13
- handleInputRef?: (element: HTMLInputElement, originalElement: HTMLElement) => HTMLElement;
14
- children: React.ReactNode;
15
- } & React.RefAttributes<AutosizeInputRefType>>;
14
+ } & React.RefAttributes<HTMLInputElement>>;
16
15
  export default AutosizeInput;
@@ -1,128 +1,51 @@
1
1
  import { jsxs, jsx } from "react/jsx-runtime";
2
- import { forwardRef, useRef, useMemo, useCallback, useLayoutEffect, useEffect, useImperativeHandle } from "react";
3
- import { useResizeObserver } from "@synerise/ds-utils";
4
- import calculateInputWidth from "./utils/calculateInputWidth.js";
5
- const sizerStyle = {
6
- position: "absolute",
7
- top: 0,
8
- left: 0,
9
- visibility: "hidden",
10
- height: 0,
11
- overflow: "scroll",
12
- whiteSpace: "pre"
2
+ import { forwardRef, useRef } from "react";
3
+ import { useMergeRefs } from "@floating-ui/react";
4
+ import { useAutosizeWidth, SIZER_STYLE } from "./useAutosizeWidth.js";
5
+ const wrapperBaseStyle = {
6
+ position: "relative",
7
+ display: "inline-block"
8
+ };
9
+ const inputBaseStyle = {
10
+ boxSizing: "content-box"
13
11
  };
14
- const FONT_CSS_PROPS = ["fontSize", "fontFamily", "fontWeight", "fontFeatureSettings", "fontStyle", "letterSpacing", "fontStretch", "textTransform"];
15
12
  const AutosizeInput = forwardRef(({
16
- extraWidth = 16,
13
+ minWidth,
14
+ extraWidth,
15
+ placeholderIsMinWidth,
17
16
  wrapperClassName,
18
- wrapperStyle: wrapperStyleProp,
17
+ wrapperStyle,
19
18
  onAutosize,
20
- preAutosize,
21
- placeholderIsMinWidth,
22
- minWidth = 0,
23
- handleInputRef,
24
- children,
25
- transformWrapperRef,
26
- transformRef,
27
- placeholder,
19
+ className,
20
+ style,
28
21
  value,
29
- defaultValue,
30
- style
22
+ placeholder,
23
+ ...inputProps
31
24
  }, forwardedRef) => {
32
- const usedValue = value ?? defaultValue ?? "";
33
25
  const inputRef = useRef(null);
34
- const inputWrapperRef = useRef(null);
35
- const sizerRef = useRef(null);
36
- const placeholderSizerRef = useRef(null);
37
- const wrapperRef = useRef(null);
38
- const transformWrapper = useMemo(() => {
39
- return transformWrapperRef || ((element) => element);
40
- }, [transformWrapperRef]);
41
- const transformElement = useMemo(() => {
42
- return transformRef || ((element) => element);
43
- }, [transformRef]);
44
- const contentWidth = useRef();
45
- const placeholderWidth = useRef();
46
- const applyNewWidth = useCallback(() => {
47
- const calculatedWidth = calculateInputWidth({
48
- sizerWidth: contentWidth.current,
49
- hasValue: !!sizerRef.current?.textContent,
50
- placeholderIsMinWidth,
51
- placeholderWidth: placeholderWidth.current ?? placeholderSizerRef.current?.scrollWidth,
52
- minWidth: +minWidth,
53
- placeholder
54
- });
55
- if (inputWrapperRef.current) {
56
- const finalWidth = calculatedWidth + +extraWidth;
57
- if (finalWidth !== parseFloat(inputWrapperRef.current.style.width)) {
58
- preAutosize && preAutosize(calculatedWidth);
59
- inputWrapperRef.current.style.width = `${finalWidth}px`;
60
- onAutosize && onAutosize(calculatedWidth);
61
- }
62
- }
63
- }, [extraWidth, minWidth, onAutosize, placeholder, placeholderIsMinWidth, preAutosize]);
64
- const resizeContentHandler = useCallback((newWidth) => {
65
- contentWidth.current = newWidth.width;
66
- applyNewWidth();
67
- }, [applyNewWidth]);
68
- const resizePlaceholderHandler = useCallback((newWidth) => {
69
- placeholderWidth.current = newWidth.width;
70
- applyNewWidth();
71
- }, [applyNewWidth]);
72
- useResizeObserver(sizerRef, resizeContentHandler);
73
- useResizeObserver(placeholderSizerRef, resizePlaceholderHandler);
74
- const copyInputStyles = () => {
75
- if (inputRef.current) {
76
- const computedStyle = window.getComputedStyle(inputRef.current);
77
- FONT_CSS_PROPS.forEach((cssProperty) => {
78
- if (sizerRef.current) {
79
- sizerRef.current.style[cssProperty] = computedStyle[cssProperty];
80
- }
81
- if (placeholderSizerRef.current) {
82
- placeholderSizerRef.current.style[cssProperty] = computedStyle[cssProperty];
83
- }
84
- });
85
- }
86
- };
87
- useLayoutEffect(() => {
88
- copyInputStyles();
89
- }, []);
90
- useEffect(() => {
91
- if (sizerRef.current) {
92
- const sizerSibling = sizerRef.current.nextElementSibling;
93
- const inputElement = sizerSibling instanceof HTMLElement && transformElement(sizerSibling);
94
- const inputWrapperElement = sizerSibling instanceof HTMLElement && transformWrapper(sizerSibling);
95
- if (inputWrapperElement && inputWrapperElement instanceof HTMLElement) {
96
- inputWrapperRef.current = inputWrapperElement;
97
- }
98
- if (inputElement && !inputRef.current) {
99
- if (inputElement && inputElement instanceof HTMLInputElement) {
100
- inputRef.current = inputElement;
101
- inputRef.current.style.boxSizing = "content-box";
102
- handleInputRef && handleInputRef(inputElement, sizerSibling);
103
- copyInputStyles();
104
- }
105
- }
106
- }
107
- }, [handleInputRef, transformElement, transformWrapper]);
108
- useImperativeHandle(forwardedRef, () => ({
109
- inputRef,
26
+ const measurableValue = typeof value === "string" || typeof value === "number" ? value : void 0;
27
+ const {
110
28
  sizerRef,
111
- wrapperRef,
112
- inputWrapperRef,
113
- placeholderSizerRef,
114
- copyInputStyles,
115
- updateInputWidth: applyNewWidth
116
- }));
117
- const wrapperStyle = {
118
- ...wrapperStyleProp,
119
- position: "relative",
120
- display: style?.display ?? "inline-block"
121
- };
122
- return /* @__PURE__ */ jsxs("div", { ref: wrapperRef, className: wrapperClassName, style: wrapperStyle, "data-testid": "wrapper", children: [
123
- /* @__PURE__ */ jsx("div", { style: sizerStyle, ref: sizerRef, "data-testid": "sizer", children: usedValue }),
124
- children,
125
- placeholder && /* @__PURE__ */ jsx("div", { ref: placeholderSizerRef, style: sizerStyle, "data-testid": "placeholder-sizer", children: placeholder })
29
+ containerRef
30
+ } = useAutosizeWidth({
31
+ value: measurableValue,
32
+ placeholder,
33
+ minWidth,
34
+ extraWidth,
35
+ placeholderIsMinWidth,
36
+ inputRef,
37
+ onAutosize
38
+ });
39
+ const mergedInputRef = useMergeRefs([inputRef, containerRef, forwardedRef]);
40
+ return /* @__PURE__ */ jsxs("div", { className: wrapperClassName, style: {
41
+ ...wrapperBaseStyle,
42
+ ...wrapperStyle
43
+ }, children: [
44
+ /* @__PURE__ */ jsx("input", { ...inputProps, value, placeholder, ref: mergedInputRef, className, style: {
45
+ ...inputBaseStyle,
46
+ ...style
47
+ } }),
48
+ /* @__PURE__ */ jsx("span", { ref: sizerRef, style: SIZER_STYLE, "aria-hidden": true })
126
49
  ] });
127
50
  });
128
51
  export {
@@ -1,4 +1,8 @@
1
- import { CSSProperties, InputHTMLAttributes, MutableRefObject, ReactNode, RefObject } from 'react';
1
+ import { CSSProperties, InputHTMLAttributes } from 'react';
2
+ /**
3
+ * Native auto-width input props. The component renders its own `<input>` and a
4
+ * hidden sizer; the forwarded ref points at the `<input>` element.
5
+ */
2
6
  export type AutosizeInputProps = Omit<InputHTMLAttributes<HTMLInputElement>, 'size'> & {
3
7
  placeholderIsMinWidth?: boolean;
4
8
  wrapperClassName?: string;
@@ -6,21 +10,4 @@ export type AutosizeInputProps = Omit<InputHTMLAttributes<HTMLInputElement>, 'si
6
10
  extraWidth?: number | string;
7
11
  minWidth?: number | string;
8
12
  onAutosize?: (newWidth: number) => void;
9
- preAutosize?: (newWidth: number) => void;
10
- transformRef?: (element: HTMLElement) => HTMLElement;
11
- transformWrapperRef?: (element: HTMLElement) => HTMLElement;
12
- handleInputRef?: (element: HTMLInputElement, originalElement: HTMLElement) => HTMLElement;
13
- children: ReactNode;
14
- };
15
- export type AutosizeWrapperProps = AutosizeInputProps & {
16
- autoResize?: boolean;
17
- };
18
- export type AutosizeInputRefType = {
19
- inputRef: MutableRefObject<HTMLInputElement | null>;
20
- sizerRef: RefObject<HTMLDivElement>;
21
- wrapperRef: RefObject<HTMLDivElement>;
22
- inputWrapperRef: RefObject<HTMLElement>;
23
- placeholderSizerRef: RefObject<HTMLDivElement>;
24
- copyInputStyles: () => void;
25
- updateInputWidth: () => void;
26
13
  };
@@ -0,0 +1,18 @@
1
+ import { CSSProperties } from 'react';
2
+ import { UseAutosizeWidthParams, UseAutosizeWidthResult } from './useAutosizeWidth.types';
3
+ /**
4
+ * Styles for the hidden measuring `<span>`. It is taken out of layout flow
5
+ * (`position: absolute`, `height: 0`, `visibility: hidden`) so it never affects
6
+ * the surrounding layout, and `white-space: pre` preserves spacing exactly as
7
+ * typed. Font properties inherit by default; when the consuming input declares
8
+ * its font on the element itself (not an ancestor), pass `inputRef` to copy it.
9
+ */
10
+ export declare const SIZER_STYLE: CSSProperties;
11
+ /**
12
+ * Headless auto-width measurement. The consumer renders a single hidden sizer
13
+ * `<span>` and an inline-block wrapper; this hook measures the text width
14
+ * synchronously in `useLayoutEffect` (before paint) and writes the resulting
15
+ * width to the wrapper imperatively — no ResizeObserver and no per-keystroke
16
+ * state update, so typing does not trigger extra React renders.
17
+ */
18
+ export declare const useAutosizeWidth: <C extends HTMLElement = HTMLDivElement>({ value, placeholder, minWidth, extraWidth, placeholderIsMinWidth, inputRef, onAutosize, }: UseAutosizeWidthParams) => UseAutosizeWidthResult<C>;