@charcoal-ui/react 5.10.0-beta.0 → 5.11.0-beta.0

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@charcoal-ui/react",
3
- "version": "5.10.0-beta.0",
3
+ "version": "5.11.0-beta.0",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -54,10 +54,10 @@
54
54
  "react-compiler-runtime": "1.0.0",
55
55
  "react-stately": "^3.26.0",
56
56
  "warning": "^4.0.3",
57
- "@charcoal-ui/theme": "5.10.0-beta.0",
58
- "@charcoal-ui/icons": "5.10.0-beta.0",
59
- "@charcoal-ui/utils": "5.10.0-beta.0",
60
- "@charcoal-ui/foundation": "5.10.0-beta.0"
57
+ "@charcoal-ui/foundation": "5.11.0-beta.0",
58
+ "@charcoal-ui/icons": "5.11.0-beta.0",
59
+ "@charcoal-ui/theme": "5.11.0-beta.0",
60
+ "@charcoal-ui/utils": "5.11.0-beta.0"
61
61
  },
62
62
  "peerDependencies": {
63
63
  "react": ">=17.0.0"
@@ -94,3 +94,29 @@ export const AutoHeight: StoryObj<typeof TextArea> = {
94
94
  return <TextArea autoHeight label="Label" />
95
95
  },
96
96
  }
97
+
98
+ export const AutoHeightAndRows: StoryObj<typeof TextArea> = {
99
+ render: function Render() {
100
+ return <TextArea rows={3} autoHeight label="label" />
101
+ },
102
+ }
103
+
104
+ export const MaxRows: StoryObj<typeof TextArea> = {
105
+ render: function Render() {
106
+ return <TextArea maxRows={6} label="label" showCount />
107
+ },
108
+ }
109
+
110
+ export const MaxRowsAndRows: StoryObj<typeof TextArea> = {
111
+ render: function Render() {
112
+ return <TextArea rows={3} maxRows={6} label="label" showCount />
113
+ },
114
+ }
115
+
116
+ export const DefaultValue: StoryObj<typeof TextArea> = {
117
+ render: function Render() {
118
+ return (
119
+ <TextArea label="Label" defaultValue={'テスト用テキスト'} showCount />
120
+ )
121
+ },
122
+ }
@@ -1,7 +1,14 @@
1
1
  import './index.css'
2
2
 
3
3
  import { useVisuallyHidden } from '@react-aria/visually-hidden'
4
- import { forwardRef, useCallback, useEffect, useRef, useState } from 'react'
4
+ import {
5
+ forwardRef,
6
+ useCallback,
7
+ useEffect,
8
+ useMemo,
9
+ useRef,
10
+ useState,
11
+ } from 'react'
5
12
  import FieldLabel from '../FieldLabel'
6
13
  import { countCodePointsInString, mergeRefs } from '../../_lib'
7
14
  import { useFocusWithClick } from '../TextField/useFocusWithClick'
@@ -24,6 +31,8 @@ export type TextAreaProps = {
24
31
  subLabel?: React.ReactNode
25
32
  autoHeight?: boolean
26
33
 
34
+ maxRows?: number
35
+
27
36
  getCount?: (value: string) => number
28
37
  } & Omit<React.ComponentPropsWithoutRef<'textarea'>, 'onChange'>
29
38
 
@@ -44,23 +53,40 @@ const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
44
53
  maxLength,
45
54
  autoHeight = false,
46
55
  rows: initialRows = 4,
56
+ maxRows,
47
57
  invalid,
48
58
  getCount = countCodePointsInString,
59
+ defaultValue,
49
60
  ...props
50
61
  },
51
62
  forwardRef,
52
63
  ) {
53
64
  const { visuallyHiddenProps } = useVisuallyHidden()
54
65
  const textareaRef = useRef<HTMLTextAreaElement>(null)
55
- const [count, setCount] = useState(getCount(value ?? ''))
66
+ const [count, setCount] = useState(
67
+ getCount(value ?? defaultValue?.toString() ?? ''),
68
+ )
56
69
  const [rows, setRows] = useState(initialRows)
70
+ const isEnableAutoHeight = useMemo(
71
+ () => autoHeight || (maxRows && maxRows >= 0),
72
+ [autoHeight, maxRows],
73
+ )
57
74
 
58
75
  const syncHeight = useCallback(
59
76
  (textarea: HTMLTextAreaElement) => {
60
- const rows = (`${textarea.value}\n`.match(/\n/gu)?.length ?? 0) || 1
61
- setRows(initialRows <= rows ? rows : initialRows)
77
+ const currentRows =
78
+ (`${textarea.value}\n`.match(/\n/gu)?.length ?? 0) || 1
79
+ const hasValidMaxRows = maxRows !== undefined && maxRows >= 1
80
+ const nextRows = initialRows <= currentRows ? currentRows : initialRows
81
+
82
+ if (!hasValidMaxRows || maxRows <= initialRows) {
83
+ setRows(nextRows)
84
+ return
85
+ }
86
+
87
+ setRows(nextRows <= maxRows ? nextRows : maxRows)
62
88
  },
63
- [initialRows],
89
+ [initialRows, maxRows],
64
90
  )
65
91
 
66
92
  const nonControlled = value === undefined
@@ -74,23 +100,30 @@ const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
74
100
  if (nonControlled) {
75
101
  setCount(count)
76
102
  }
77
- if (autoHeight && textareaRef.current !== null) {
103
+ if (isEnableAutoHeight && textareaRef.current !== null) {
78
104
  syncHeight(textareaRef.current)
79
105
  }
80
106
  onChange?.(value)
81
107
  },
82
- [autoHeight, getCount, maxLength, nonControlled, onChange, syncHeight],
108
+ [
109
+ isEnableAutoHeight,
110
+ getCount,
111
+ maxLength,
112
+ nonControlled,
113
+ onChange,
114
+ syncHeight,
115
+ ],
83
116
  )
84
117
 
85
118
  useEffect(() => {
86
- setCount(getCount(value ?? ''))
87
- }, [getCount, value])
119
+ setCount(getCount(value ?? defaultValue?.toString() ?? ''))
120
+ }, [getCount, value, defaultValue])
88
121
 
89
122
  useEffect(() => {
90
- if (autoHeight && textareaRef.current !== null) {
123
+ if (isEnableAutoHeight && textareaRef.current !== null) {
91
124
  syncHeight(textareaRef.current)
92
125
  }
93
- }, [autoHeight, syncHeight])
126
+ }, [isEnableAutoHeight, syncHeight])
94
127
 
95
128
  const containerRef = useRef(null)
96
129
 
@@ -138,6 +171,7 @@ const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
138
171
  rows={rows}
139
172
  value={value}
140
173
  disabled={disabled}
174
+ defaultValue={defaultValue}
141
175
  {...props}
142
176
  />
143
177
  {showCount && (