Skip to content

Commit 9eb8796

Browse files
authored
Type check docs examples (#3710)
1 parent e00dfc4 commit 9eb8796

File tree

74 files changed

+732
-454
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+732
-454
lines changed

.circleci/config.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,16 @@ jobs:
290290
paths:
291291
- 'ts-diff.txt'
292292

293+
typecheck-docs:
294+
executor: rsp-large
295+
steps:
296+
- restore_cache:
297+
key: react-spectrum-{{ .Environment.CACHE_VERSION }}-{{ .Environment.CIRCLE_SHA1 }}
298+
299+
- run:
300+
name: check-examples
301+
command: make check-examples
302+
293303
storybook:
294304
executor: rsp-large
295305
steps:
@@ -498,6 +508,9 @@ workflows:
498508
filters:
499509
branches:
500510
ignore: main
511+
- typecheck-docs:
512+
requires:
513+
- install
501514
- storybook:
502515
requires:
503516
- install

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,7 @@ website:
9696
website-production:
9797
node scripts/buildWebsite.js
9898
cp packages/dev/docs/pages/robots.txt dist/production/docs/robots.txt
99+
100+
check-examples:
101+
node scripts/extractExamples.mjs
102+
yarn tsc --project dist/docs-examples/tsconfig.json

packages/@react-aria/autocomplete/docs/useSearchAutocomplete.mdx

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ function SearchAutocomplete(props) {
140140
let inputRef = React.useRef(null);
141141
let listBoxRef = React.useRef(null);
142142
let popoverRef = React.useRef(null);
143+
let buttonRef = React.useRef(null);
143144

144145
let {inputProps, listBoxProps, labelProps, clearButtonProps} = useSearchAutocomplete(
145146
{
@@ -151,7 +152,7 @@ function SearchAutocomplete(props) {
151152
state
152153
);
153154

154-
let {buttonProps} = useButton(clearButtonProps);
155+
let {buttonProps} = useButton(clearButtonProps, buttonRef);
155156

156157
return (
157158
<div style={{display: 'inline-flex', flexDirection: 'column'}}>
@@ -167,7 +168,7 @@ function SearchAutocomplete(props) {
167168
fontSize: 16
168169
}} />
169170
{state.inputValue !== '' &&
170-
<button {...buttonProps}>❎</button>
171+
<button {...buttonProps} ref={buttonRef}>❎</button>
171172
}
172173
{state.isOpen &&
173174
<Popover state={state} triggerRef={inputRef} popoverRef={popoverRef} isNonModal placement="bottom start">
@@ -203,21 +204,23 @@ See [usePopover](usePopover.html) for more examples of popovers.
203204
<summary style={{fontWeight: 'bold'}}><ChevronRight size="S" /> Show code</summary>
204205

205206
```tsx example export=true render=false
207+
import type {AriaPopoverProps} from 'react-aria';
208+
import type {OverlayTriggerState} from 'react-stately';
206209
import {usePopover, Overlay, DismissButton} from '@react-aria/overlays';
207210

208-
function Popover({children, state, ...props}) {
209-
let ref = React.useRef();
210-
let {popoverRef = ref} = props;
211-
let {popoverProps} = usePopover({
212-
...props,
213-
popoverRef
214-
}, state);
211+
interface PopoverProps extends AriaPopoverProps {
212+
children: React.ReactNode,
213+
state: OverlayTriggerState
214+
}
215+
216+
function Popover({children, state, ...props}: PopoverProps) {
217+
let {popoverProps} = usePopover(props, state);
215218

216219
return (
217220
<Overlay>
218221
<div
219222
{...popoverProps}
220-
ref={popoverRef}
223+
ref={props.popoverRef as React.RefObject<HTMLDivElement>}
221224
style={{
222225
...popoverProps.style,
223226
background: 'lightgray',
@@ -246,7 +249,7 @@ user types in the SearchAutocomplete. They can also be shared with other compone
246249
import {useListBox, useOption} from '@react-aria/listbox';
247250

248251
function ListBox(props) {
249-
let ref = React.useRef();
252+
let ref = React.useRef(null);
250253
let {listBoxRef = ref, state} = props;
251254
let {listBoxProps} = useListBox(props, state, listBoxRef);
252255

@@ -273,7 +276,7 @@ function ListBox(props) {
273276
}
274277

275278
function Option({item, state}) {
276-
let ref = React.useRef();
279+
let ref = React.useRef(null);
277280
let {optionProps, isSelected, isFocused, isDisabled} = useOption({key: item.key}, state, ref);
278281

279282
let backgroundColor;
@@ -350,7 +353,7 @@ function Example() {
350353
{id: 8, name: 'Agricultural'},
351354
{id: 9, name: 'Electrical'}
352355
];
353-
let [major, setMajor] = React.useState();
356+
let [major, setMajor] = React.useState(null);
354357

355358
let onSubmit = (value, key) => {
356359
if (value) {

packages/@react-aria/breadcrumbs/docs/useBreadcrumbs.mdx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,21 @@ import {useBreadcrumbs, useBreadcrumbItem} from '@react-aria/breadcrumbs';
9292

9393
function Breadcrumbs(props) {
9494
let {navProps} = useBreadcrumbs(props);
95-
let children = React.Children.toArray(props.children);
95+
let childCount = React.Children.count(props.children);
9696

9797
return (
9898
<nav {...navProps}>
9999
<ol style={{display: 'flex', listStyle: 'none', margin: 0, padding: 0}}>
100-
{children.map((child, i) =>
101-
React.cloneElement(child, {isCurrent: i === children.length - 1})
100+
{React.Children.map(props.children, (child, i) =>
101+
React.cloneElement(child, {isCurrent: i === childCount - 1})
102102
)}
103103
</ol>
104104
</nav>
105105
)
106106
}
107107

108108
function BreadcrumbItem(props) {
109-
let ref = React.useRef();
109+
let ref = React.useRef(null);
110110
let {itemProps} = useBreadcrumbItem({...props, elementType: 'span'}, ref);
111111
return (
112112
<li>
@@ -144,21 +144,21 @@ for each BreadcrumbItem. This is the default `elementType`, so the option can be
144144
///- begin collapse -///
145145
function Breadcrumbs(props) {
146146
let {navProps} = useBreadcrumbs(props);
147-
let children = React.Children.toArray(props.children);
147+
let childCount = React.Children.count(props.children);
148148

149149
return (
150150
<nav {...navProps}>
151151
<ol style={{display: 'flex', listStyle: 'none', margin: 0, padding: 0}}>
152-
{children.map((child, i) =>
153-
React.cloneElement(child, {isCurrent: i === children.length - 1})
152+
{React.Children.map(props.children, (child, i) =>
153+
React.cloneElement(child, {isCurrent: i === childCount - 1})
154154
)}
155155
</ol>
156156
</nav>
157157
);
158158
}
159159
///- end collapse -///
160160
function BreadcrumbItem(props) {
161-
let ref = React.useRef();
161+
let ref = React.useRef(null);
162162
let {itemProps} = useBreadcrumbItem(props, ref);
163163
return (
164164
<li>
@@ -200,21 +200,21 @@ the current has elementType `h3` and all other breadcrumbs are of type `a`.
200200
///- begin collapse -///
201201
function Breadcrumbs(props) {
202202
let {navProps} = useBreadcrumbs(props);
203-
let children = React.Children.toArray(props.children);
203+
let childCount = React.Children.count(props.children);
204204

205205
return (
206206
<nav {...navProps}>
207207
<ol style={{display: 'flex', listStyle: 'none', margin: 0, padding: 0}}>
208-
{children.map((child, i) =>
209-
React.cloneElement(child, {isCurrent: i === children.length - 1})
208+
{React.Children.map(props.children, (child, i) =>
209+
React.cloneElement(child, {isCurrent: i === childCount - 1})
210210
)}
211211
</ol>
212212
</nav>
213213
)
214214
}
215215
///- end collapse -///
216216
function BreadcrumbItem(props) {
217-
let ref = React.useRef();
217+
let ref = React.useRef(null);
218218
let {itemProps} = useBreadcrumbItem({
219219
...props,
220220
elementType: props.isCurrent ? 'h3' : 'a'

packages/@react-aria/calendar/docs/useCalendar.mdx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,10 @@ function Calendar(props) {
120120
createCalendar
121121
});
122122

123-
let ref = React.useRef();
124-
let {calendarProps, prevButtonProps, nextButtonProps, title} = useCalendar(props, state, ref);
123+
let {calendarProps, prevButtonProps, nextButtonProps, title} = useCalendar(props, state);
125124

126125
return (
127-
<div {...calendarProps} ref={ref} className="calendar">
126+
<div {...calendarProps} className="calendar">
128127
<div className="header">
129128
<h2>{title}</h2>
130129
<Button {...prevButtonProps}>&lt;</Button>
@@ -193,7 +192,7 @@ Finally, the `CalendarCell` component renders an individual cell in a calendar.
193192
import {useCalendarCell} from '@react-aria/calendar';
194193

195194
function CalendarCell({state, date}) {
196-
let ref = React.useRef();
195+
let ref = React.useRef(null);
197196
let {
198197
cellProps,
199198
buttonProps,
@@ -280,7 +279,7 @@ The `Button` component is used in the above example to navigate between months.
280279
import {useButton} from '@react-aria/button';
281280

282281
function Button(props) {
283-
let ref = React.useRef();
282+
let ref = React.useRef(null);
284283
let {buttonProps} = useButton(props, ref);
285284
return <button {...buttonProps} ref={ref}>{props.children}</button>;
286285
}

packages/@react-aria/calendar/docs/useRangeCalendar.mdx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function RangeCalendar(props) {
119119
createCalendar
120120
});
121121

122-
let ref = React.useRef();
122+
let ref = React.useRef(null);
123123
let {calendarProps, prevButtonProps, nextButtonProps, title} = useRangeCalendar(props, state, ref);
124124

125125
return (
@@ -192,7 +192,7 @@ Finally, the `CalendarCell` component renders an individual cell in a calendar.
192192
import {useCalendarCell} from '@react-aria/calendar';
193193

194194
function CalendarCell({state, date}) {
195-
let ref = React.useRef();
195+
let ref = React.useRef(null);
196196
let {
197197
cellProps,
198198
buttonProps,
@@ -279,7 +279,7 @@ The `Button` component is used in the above example to navigate between months.
279279
import {useButton} from '@react-aria/button';
280280

281281
function Button(props) {
282-
let ref = React.useRef();
282+
let ref = React.useRef(null);
283283
let {buttonProps} = useButton(props, ref);
284284
return <button {...buttonProps} ref={ref}>{props.children}</button>;
285285
}
@@ -408,7 +408,6 @@ This example includes multiple unavailable date ranges, e.g. dates when a rental
408408

409409
```tsx example
410410
import {today} from '@internationalized/date';
411-
import {useLocale} from '@react-aria/i18n';
412411

413412
function Example() {
414413
let now = today(getLocalTimeZone());
@@ -418,7 +417,6 @@ function Example() {
418417
[now.add({days: 23}), now.add({days: 24})],
419418
];
420419

421-
let {locale} = useLocale();
422420
let isDateUnavailable = (date) => disabledRanges.some((interval) => date.compare(interval[0]) >= 0 && date.compare(interval[1]) <= 0);
423421

424422
return <RangeCalendar aria-label="Trip dates" minValue={today(getLocalTimeZone())} isDateUnavailable={isDateUnavailable} />

packages/@react-aria/checkbox/docs/useCheckbox.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ import {useToggleState} from '@react-stately/toggle';
8484
function Checkbox(props) {
8585
let {children} = props;
8686
let state = useToggleState(props);
87-
let ref = React.useRef();
87+
let ref = React.useRef(null);
8888
let {inputProps} = useCheckbox(props, state, ref);
8989

9090
return (
@@ -118,7 +118,7 @@ import {useFocusRing} from '@react-aria/focus';
118118

119119
function Checkbox(props) {
120120
let state = useToggleState(props);
121-
let ref = React.useRef();
121+
let ref = React.useRef(null);
122122
let {inputProps} = useCheckbox(props, state, ref);
123123
let {isFocusVisible, focusProps} = useFocusRing();
124124
let isSelected = state.isSelected && !props.isIndeterminate;

packages/@react-aria/checkbox/docs/useCheckboxGroup.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function CheckboxGroup(props) {
126126
function Checkbox(props) {
127127
let {children} = props;
128128
let state = React.useContext(CheckboxGroupContext);
129-
let ref = React.useRef();
129+
let ref = React.useRef(null);
130130
let {inputProps} = useCheckboxGroupItem(props, state, ref);
131131

132132
let isDisabled = state.isDisabled || props.isDisabled;

packages/@react-aria/color/docs/useColorArea.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ It is built using the [useColorSlider](useColorSlider.html) hook, and can be sha
304304
function ColorSlider(props) {
305305
let {locale} = useLocale();
306306
let state = useColorSliderState({...props, locale});
307-
let trackRef = React.useRef();
308-
let inputRef = React.useRef();
307+
let trackRef = React.useRef(null);
308+
let inputRef = React.useRef(null);
309309

310310
// Default label to the channel name in the current locale
311311
let label = props.label || state.value.getChannelName(props.channel, locale);

packages/@react-aria/color/docs/useColorField.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ import {useColorFieldState} from '@react-stately/color';
8282

8383
function ColorField(props) {
8484
let state = useColorFieldState(props);
85-
let inputRef = React.useRef();
85+
let inputRef = React.useRef(null);
8686
let {
8787
labelProps,
8888
inputProps

0 commit comments

Comments
 (0)