@charcoal-ui/react 2.0.0-alpha.0 → 2.0.0-alpha.3

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 (34) hide show
  1. package/dist/_lib/compat.d.ts +2 -3
  2. package/dist/_lib/compat.d.ts.map +1 -1
  3. package/dist/components/Radio/index.story.d.ts +4 -2
  4. package/dist/components/Radio/index.story.d.ts.map +1 -1
  5. package/dist/components/Select/context.d.ts +14 -0
  6. package/dist/components/Select/context.d.ts.map +1 -0
  7. package/dist/components/Select/index.d.ts +24 -0
  8. package/dist/components/Select/index.d.ts.map +1 -0
  9. package/dist/components/Select/index.story.d.ts +75 -0
  10. package/dist/components/Select/index.story.d.ts.map +1 -0
  11. package/dist/components/Select/index.test.d.ts +2 -0
  12. package/dist/components/Select/index.test.d.ts.map +1 -0
  13. package/dist/components/TextField/index.d.ts.map +1 -1
  14. package/dist/components/TextField/index.story.d.ts +9 -3
  15. package/dist/components/TextField/index.story.d.ts.map +1 -1
  16. package/dist/index.cjs +1 -1
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.ts +1 -0
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.modern.js +76 -31
  21. package/dist/index.modern.js.map +1 -1
  22. package/dist/index.module.js +1 -1
  23. package/dist/index.module.js.map +1 -1
  24. package/dist/styled.d.ts +10 -0
  25. package/dist/styled.d.ts.map +1 -1
  26. package/package.json +11 -6
  27. package/src/_lib/compat.ts +1 -4
  28. package/src/components/Select/context.ts +23 -0
  29. package/src/components/Select/index.story.tsx +153 -0
  30. package/src/components/Select/index.test.tsx +281 -0
  31. package/src/components/Select/index.tsx +210 -0
  32. package/src/components/TextField/index.tsx +26 -10
  33. package/src/components/a11y.test.tsx +2 -2
  34. package/src/index.ts +6 -0
@@ -60,8 +60,10 @@ function mergeRefs<T>(...refs: React.Ref<T>[]): React.RefCallback<T> {
60
60
  }
61
61
  }
62
62
 
63
- function countStringInCodePoints(string: string) {
64
- return [...string].length
63
+ function countCodePointsInString(string: string) {
64
+ // [...string] とするとproduction buildで動かなくなる
65
+ // cf. https://twitter.com/f_subal/status/1497214727511891972
66
+ return Array.from(string).length
65
67
  }
66
68
 
67
69
  const TextField = React.forwardRef<TextFieldElement, TextFieldProps>(
@@ -100,22 +102,29 @@ const SingleLineTextField = React.forwardRef<
100
102
  const ariaRef = useRef<HTMLInputElement>(null)
101
103
  const prefixRef = useRef<HTMLSpanElement>(null)
102
104
  const suffixRef = useRef<HTMLSpanElement>(null)
103
- const [count, setCount] = useState(countStringInCodePoints(props.value ?? ''))
105
+ const [count, setCount] = useState(countCodePointsInString(props.value ?? ''))
104
106
  const [prefixWidth, setPrefixWidth] = useState(0)
105
107
  const [suffixWidth, setSuffixWidth] = useState(0)
106
108
 
109
+ const nonControlled = props.value === undefined
107
110
  const handleChange = useCallback(
108
111
  (value: string) => {
109
- const count = countStringInCodePoints(value)
112
+ const count = countCodePointsInString(value)
110
113
  if (maxLength !== undefined && count > maxLength) {
111
114
  return
112
115
  }
113
- setCount(count)
116
+ if (nonControlled) {
117
+ setCount(count)
118
+ }
114
119
  onChange?.(value)
115
120
  },
116
- [maxLength, onChange]
121
+ [maxLength, nonControlled, onChange]
117
122
  )
118
123
 
124
+ useEffect(() => {
125
+ setCount(countCodePointsInString(props.value ?? ''))
126
+ }, [props.value])
127
+
119
128
  const { inputProps, labelProps, descriptionProps, errorMessageProps } =
120
129
  useTextField(
121
130
  {
@@ -217,7 +226,7 @@ const MultiLineTextField = React.forwardRef<
217
226
  const { visuallyHiddenProps } = useVisuallyHidden()
218
227
  const textareaRef = useRef<HTMLTextAreaElement>(null)
219
228
  const ariaRef = useRef<HTMLTextAreaElement>(null)
220
- const [count, setCount] = useState(countStringInCodePoints(props.value ?? ''))
229
+ const [count, setCount] = useState(countCodePointsInString(props.value ?? ''))
221
230
  const [rows, setRows] = useState(initialRows)
222
231
 
223
232
  const syncHeight = useCallback(
@@ -230,21 +239,28 @@ const MultiLineTextField = React.forwardRef<
230
239
  [initialRows]
231
240
  )
232
241
 
242
+ const nonControlled = props.value === undefined
233
243
  const handleChange = useCallback(
234
244
  (value: string) => {
235
- const count = countStringInCodePoints(value)
245
+ const count = countCodePointsInString(value)
236
246
  if (maxLength !== undefined && count > maxLength) {
237
247
  return
238
248
  }
239
- setCount(count)
249
+ if (nonControlled) {
250
+ setCount(count)
251
+ }
240
252
  if (autoHeight && textareaRef.current !== null) {
241
253
  syncHeight(textareaRef.current)
242
254
  }
243
255
  onChange?.(value)
244
256
  },
245
- [autoHeight, maxLength, onChange, syncHeight]
257
+ [autoHeight, maxLength, nonControlled, onChange, syncHeight]
246
258
  )
247
259
 
260
+ useEffect(() => {
261
+ setCount(countCodePointsInString(props.value ?? ''))
262
+ }, [props.value])
263
+
248
264
  const { inputProps, labelProps, descriptionProps, errorMessageProps } =
249
265
  useTextField(
250
266
  {
@@ -69,13 +69,13 @@ describe.each(themes)('using %s theme', (_name, theme) => {
69
69
  describe.each(links)('using %s component', (_name, link) => {
70
70
  describe.each(stories)(
71
71
  'storiesOf($filename).add($name)',
72
- ({ story, args }) => {
72
+ ({ story: Story, args }) => {
73
73
  it('has no accessibility violations', async () => {
74
74
  expect(() => {
75
75
  render(
76
76
  <ThemeProvider theme={theme}>
77
77
  <ComponentAbstraction components={{ Link: link }}>
78
- {story(args)}
78
+ <Story {...args} />
79
79
  </ComponentAbstraction>
80
80
  </ThemeProvider>
81
81
  )
package/src/index.ts CHANGED
@@ -19,6 +19,12 @@ export {
19
19
  RadioGroup,
20
20
  type RadioGroupProps,
21
21
  } from './components/Radio'
22
+ export {
23
+ default as Select,
24
+ type SelectProps,
25
+ SelectGroup,
26
+ type SelectGroupProps,
27
+ } from './components/Select'
22
28
  export { default as Switch, type SwitchProps } from './components/Switch'
23
29
  export {
24
30
  default as TextField,