-
Notifications
You must be signed in to change notification settings - Fork 2
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
Typescript conversion #1
base: master
Are you sure you want to change the base?
Changes from all commits
65c7526
b2fd466
1b96a18
7d7cd87
0d9bd37
d79ed5a
ce2e8d5
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type Threshold = 'xs' | 'sm' | 'md' | 'lg' | 'xl'; | ||
export type ThresholdMap = { | ||
[key in Threshold]?: number; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
import { Threshold } from './ThresholdMap'; | ||
import defaultThresholdMap from './defaultThresholdMap'; | ||
|
||
const getCurrentValue = value => (value !== undefined ? value : null); | ||
const getCurrentValue = (value: any) => (value !== undefined ? value : null); | ||
|
||
const responsivePropBuilder = (currentThreshold, props, configuration, thresholdMap = defaultThresholdMap) => { | ||
export interface GenericProps { | ||
[key: string]: any; | ||
} | ||
|
||
export interface ResponsivePropsConfig { | ||
propKeys: Array<string>; | ||
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.
|
||
} | ||
|
||
const responsivePropBuilder = (currentThreshold: Threshold, props: GenericProps, configuration: ResponsivePropsConfig, thresholdMap = defaultThresholdMap) => { | ||
// get the keys from the map, e.g. ['xs', 'sm', 'md', 'lg', 'xl'] | ||
const thresholdKeys = Object.keys(thresholdMap); | ||
|
||
|
@@ -16,10 +25,10 @@ const responsivePropBuilder = (currentThreshold, props, configuration, threshold | |
|
||
// only an object can contain responsive values, null is an object also but that's not valid | ||
// e.g. size={{xs: 'h4', md: 'h3'}} | ||
const propKeys = configuration.propKeys.filter(propKey => typeof props[propKey] === 'object' && props[propKey] !== null); | ||
const propKeys = configuration.propKeys.filter((propKey: string) => typeof props[propKey] === 'object' && props[propKey] !== null); | ||
|
||
// loop through the props that have been found as being responsive and extract an object of name/value pairs | ||
const translatedValues = propKeys.reduce((acc, propKey) => { | ||
const translatedValues = propKeys.reduce((acc: any, propKey: string) => { | ||
let result = null; | ||
// find the first threshold with a value. That is our value because we reversed them above starting at the current threshold and moving to smaller thresholds | ||
for (let i = 0; i < thresholds.length; i++) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ const breakpoints = { | |
sm: 480, | ||
md: 768, | ||
lg: 960, | ||
xl: 1280 | ||
xl: 1280, | ||
}; | ||
|
||
beforeEach(() => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"compilerOptions": { | ||
"outDir": "lib", | ||
"target": "es5", | ||
"module": "commonjs", | ||
"jsx": "react", | ||
"allowJs": true, | ||
"declaration": true, | ||
"emitDeclarationOnly": true, | ||
"sourceMap": true, | ||
"importHelpers": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"moduleResolution": "Node", | ||
"noImplicitThis": true, | ||
"noImplicitReturns": true, | ||
"noImplicitAny": true, | ||
"skipLibCheck": true, | ||
"lib": ["es5", "dom"], | ||
"types": ["node", "jest"], | ||
"noUnusedLocals": true, | ||
"resolveJsonModule": true, | ||
"esModuleInterop": true, | ||
"preserveSymlinks": true, | ||
"baseUrl": ".", | ||
"outDir": "./dist" | ||
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. Does this build to both common-js and es-modules? 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. only cjs |
||
}, | ||
"include": ["src"], | ||
} |
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.
Shouldn't
propTypes
be replaced with interfaces?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.
Technically they are not identical and still have value in non-TS apps. I was trying to change as little functionality as possible in this PR.