-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert packages/react storybook to typescript. Convert checkbox story to tsx #12000
Changes from 11 commits
33c1b71
38dd38d
ec89107
1566a5a
b79c8f1
d896f6a
18c8b76
056660d
a10ab9a
d034edd
760ef50
1e2fb9a
cb16af7
551d6ac
1af0750
3f69616
43e3966
0bffe0d
d188dd6
871d4d1
b3a4665
af3541b
6270409
c05f566
1bbdc25
51765ae
a55f812
cfef450
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import React from 'react'; | ||
import { Button } from '@carbon/react'; | ||
|
||
function App() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import React from 'react'; | ||
import { Button } from '@carbon/react'; | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,19 +4,16 @@ | |
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import fs from 'fs'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh cool I didnt know you could reference these like this. good to know! |
||
import glob from 'fast-glob'; | ||
import MiniCssExtractPlugin from 'mini-css-extract-plugin'; | ||
import path from 'path'; | ||
|
||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const glob = require('fast-glob'); | ||
const MiniCssExtractPlugin = require('mini-css-extract-plugin'); | ||
const path = require('path'); | ||
|
||
const stories = glob | ||
const stories: string[] = glob | ||
.sync( | ||
[ | ||
'./Welcome/Welcome.stories.js', | ||
'../src/**/*.stories.js', | ||
'../src/**/*.stories.@(js|tsx|ts)', | ||
'../src/**/*.stories.mdx', | ||
'../src/**/next/*.stories.js', | ||
'../src/**/next/**/*.stories.js', | ||
|
@@ -30,7 +27,7 @@ const stories = glob | |
// Filters the stories by finding the paths that have a story file that ends | ||
// in `-story.js` and checks to see if they also have a `.stories.js`, | ||
// if so then defer to the `.stories.js` | ||
.filter((match) => { | ||
.filter((match: string) => { | ||
const filepath = path.resolve(__dirname, match); | ||
const basename = path.basename(match, '.js'); | ||
const denylist = new Set([ | ||
|
@@ -92,9 +89,10 @@ module.exports = { | |
}, | ||
framework: '@storybook/react', | ||
stories, | ||
webpack(config) { | ||
const babelLoader = config.module.rules.find((rule) => { | ||
return rule.use.some(({ loader }) => { | ||
// TODO: Fix typings for this function | ||
mbarrer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
webpack(config: any) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If statically typing this is desired, there are two options:
import type { Configuration } from 'webpack';
...
webpack(config: Configuration) {
import type { StorybookConfig } from '@storybook/core-common';
const config: StorybookConfig = { /** */ }
module.exports = config; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep nice call, there was a few other changes needed in the body of the config to account for the union types used in the |
||
const babelLoader = config.module.rules.find((rule: any) => { | ||
return rule.use.some(({ loader }: { loader: string }) => { | ||
return loader.includes('babel-loader'); | ||
}); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { FunctionComponent, HTMLAttributes } from 'react'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing copyright statement |
||
|
||
declare const CheckboxSkeleton: FunctionComponent< | ||
HTMLAttributes<HTMLDivElement> | ||
>; | ||
|
||
export default CheckboxSkeleton; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as React from 'react'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing copyright statement |
||
import { ForwardRefReturn, ReactInputAttr } from '../../typings/shared'; | ||
|
||
type ExcludedAttributes = 'id' | 'onChange' | 'onClick' | 'type'; | ||
|
||
export interface CheckboxProps | ||
extends Omit<ReactInputAttr, ExcludedAttributes> { | ||
/** | ||
* Provide an `id` to uniquely identify the Checkbox input | ||
*/ | ||
id: string; | ||
|
||
/** | ||
* Provide a label to provide a description of the Checkbox input that you are | ||
* exposing to the user | ||
*/ | ||
labelText: NonNullable<React.ReactNode>; | ||
|
||
/** | ||
* Specify whether the underlying input should be checked by default | ||
*/ | ||
defaultChecked?: boolean; | ||
|
||
/** | ||
* Specify whether the Checkbox should be disabled | ||
*/ | ||
disabled?: boolean; | ||
|
||
/** | ||
* Specify whether the label should be hidden, or not | ||
*/ | ||
hideLabel?: boolean; | ||
|
||
/** | ||
* Specify whether the Checkbox is in an indeterminate state | ||
*/ | ||
indeterminate?: boolean; | ||
|
||
/** | ||
* Provide an optional handler that is called when the internal state of | ||
* Checkbox changes. This handler is called with event and state info. | ||
* `(event, { checked, id }) => void` | ||
*/ | ||
onChange?: ( | ||
evt: React.ChangeEvent<HTMLInputElement>, | ||
data: { checked: boolean; id: string } | ||
) => void; | ||
} | ||
|
||
declare const Checkbox: ForwardRefReturn<HTMLInputElement, CheckboxProps>; | ||
|
||
export default Checkbox; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Checkbox from './Checkbox'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing copyright statement |
||
import CheckboxSkeleton from './Checkbox.Skeleton'; | ||
|
||
export { Checkbox, CheckboxSkeleton }; | ||
export default Checkbox; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/* eslint-disable jest/no-standalone-expect */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which part of this file is violating this rule? Should it instead be updated to conform to it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm not sure why this was added, removing does not seem to cause any warnings to show |
||
/** | ||
* Copyright IBM Corp. 2016, 2018 | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Ambient module to allow the importing of .mdx files in storybook .tsx files | ||
declare module '*.mdx' { | ||
const content: any; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should definitely aim for zero warnings. This should either be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, i should have changed this to |
||
export default content; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import * as React from 'react'; | ||
|
||
export type ReactAttr<T = HTMLElement> = React.HTMLAttributes<T>; | ||
export type ReactAnchorAttr<T = HTMLAnchorElement> = | ||
React.AnchorHTMLAttributes<T>; | ||
export type ReactButtonAttr<T = HTMLButtonElement> = | ||
React.ButtonHTMLAttributes<T>; | ||
export type ReactDivAttr = ReactAttr<HTMLDivElement>; | ||
export type ReactInputAttr<T = HTMLInputElement> = React.InputHTMLAttributes<T>; | ||
export type ReactLabelAttr<T = HTMLLabelElement> = React.LabelHTMLAttributes<T>; | ||
export type ReactLIAttr<T = HTMLLIElement> = React.LiHTMLAttributes<T>; | ||
|
||
// export type ShapeOf< | ||
mbarrer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// B extends object, | ||
// E extends object = { [key: string]: any } | ||
// > = (E extends never ? {} : E) & B; | ||
// export type Overwrite<T, U> = [T] extends [never] ? U : Omit<T, keyof U> & U; | ||
|
||
export type VerticalDirection = 'bottom' | 'top'; | ||
export type HorizontalDirection = 'left' | 'right'; | ||
export type Direction = HorizontalDirection | VerticalDirection; | ||
export type ListBoxBaseItemType = object | string; | ||
export type TooltipAlignment = 'center' | 'end' | 'start'; | ||
export type TooltipPosition = Direction; | ||
export type CarbonSize = 'lg' | 'sm' | 'xs'; | ||
export type CarbonInputSize = 'sm' | 'lg' | 'xl'; | ||
|
||
// | ||
// In retrospect, it may not always be a good idea to lump shared props into a common reused interface. | ||
// There's no real relation between components that share these types and they could diverge causing painful refactors. | ||
// This approach should probably be left for more complicated types such as those that involve generics. | ||
// | ||
|
||
export interface DownshiftTypedProps<ItemType> { | ||
itemToString?(item: ItemType): string; | ||
} | ||
|
||
export interface InternationalProps< | ||
MID = string, | ||
ARGS = Record<string, unknown> | ||
> { | ||
translateWithId?(messageId: MID, args?: ARGS): string; | ||
} | ||
|
||
export interface MenuOffsetData { | ||
left?: number | undefined; | ||
top?: number | undefined; | ||
} | ||
|
||
export interface RenderIconProps<P = any> { | ||
renderIcon?: React.ComponentType<P> | undefined; | ||
} | ||
|
||
export interface RequiresChildrenProps<T = React.ReactNode> { | ||
children: NonNullable<T>; | ||
} | ||
|
||
export interface RequiresIdProps<T = ReactAttr['id']> { | ||
id: NonNullable<T>; | ||
} | ||
|
||
export interface SizingProps { | ||
small?: boolean | undefined; | ||
} | ||
|
||
export interface SideNavSharedProps { | ||
isSideNavExpanded?: boolean | undefined; | ||
} | ||
|
||
export interface SideNavSizingProps { | ||
large?: boolean | undefined; | ||
} | ||
|
||
// | ||
// aliases for some React types that it doesn't export directly. They are needed to make sure we match the signatures | ||
// as close as possible. | ||
// | ||
// reference patterns: | ||
// function component with no generics: export declare const Comp: React.FC<PropsInterface>; | ||
// function component with generics: export declare function Comp<T extends SomeType>(props: FCProps<PropsInterface<T>>): FCReturn; | ||
// forwardRef component with no generics: export declare const Comp: ForwardRefReturn<HTMLElement, PropsInterface>; | ||
// forwardRef component with generics: export declare function Comp<T extends SomeType>(props: ForwardRefProps<HTMLElement, PropsInterface<T>>): FCReturn; | ||
// | ||
export type FCProps<P = unknown> = Parameters<React.FC<P>>[0]; | ||
export type FCReturn = ReturnType<React.FC>; | ||
export type ForwardRefProps<T, P = unknown> = React.PropsWithoutRef< | ||
React.PropsWithChildren<P> | ||
> & | ||
React.RefAttributes<T>; | ||
export type ForwardRefReturn<T, P = unknown> = React.ForwardRefExoticComponent< | ||
ForwardRefProps<T, P> | ||
>; | ||
|
||
export type JSXIntrinsicElementProps< | ||
K extends keyof JSX.IntrinsicElements, | ||
REF extends boolean = false | ||
> = REF extends true | ||
? JSX.IntrinsicElements[K] | ||
: Omit<JSX.IntrinsicElements[K], 'ref'>; | ||
|
||
// for "as" props | ||
export type ReactComponentConstructor<P> = | ||
| ((props: P) => FCReturn) | ||
| (new (props: P) => React.Component<unknown, any>); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"compilerOptions": { | ||
"composite": true, | ||
"target": "es5", | ||
"lib": ["dom", "dom.iterable", "esnext"], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"esModuleInterop": true, | ||
"allowSyntheticDefaultImports": true, | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"isolatedModules": true, | ||
"resolveJsonModule": true, | ||
"noEmit": true, | ||
"jsx": "react-jsx", | ||
"sourceMap": true, | ||
"declaration": true, | ||
// use linting instead | ||
"noUnusedLocals": false, | ||
"noUnusedParameters": false, | ||
"incremental": true, | ||
"noFallthroughCasesInSwitch": true | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["node_modules", "build"] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming carbon is using the
no-unused-vars
JS rule, you'll want to turn that off in favor of the typescript-eslint one. Here's an example