Skip to content

Commit

Permalink
Merge pull request #4 from unsplash/refactor/pr1-feedback
Browse files Browse the repository at this point in the history
Refactor: PR #1 feedback
  • Loading branch information
samijaber committed Jun 11, 2018
2 parents c415342 + 48bc0f9 commit ac027e6
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 41 deletions.
13 changes: 7 additions & 6 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Note: prettier inherits from `indent_style`, `indent_size`/`tab_width`, and `max_line_length`
# https://github.com/prettier/prettier/blob/cecf0657a521fa265b713274ed67ca39be4142cf/docs/api.md#prettierresolveconfigfilepath--options

root = true

[*]
Expand All @@ -6,16 +9,14 @@ indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true

[patches/**.patch]
trim_trailing_whitespace = false

[.vscode/settings.json]
indent_size = 4

[*.snap]
trim_trailing_whitespace = false

# Git is sensitive to whitespace in diff files
# https://stackoverflow.com/questions/50258565/git-editing-hunks-fails-when-file-has-other-hunks/50275053#50275053
[*.diff]
trim_trailing_whitespace = false

[*.{ts,tsx}]
# https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#max_line_length
max_line_length = 100
25 changes: 3 additions & 22 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,31 +1,12 @@
# OS X
.DS_Store

# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history
# Yarn
yarn-error.log

# typescript dist
dist/
dist/
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
tsconfig.json
src
.vscode
.github
.editorconfig
.prettierrc
2 changes: 0 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"trailingComma": "all",
"printWidth": 100,
"tabWidth": 2,
"singleQuote": true
}
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"dependencies": {
"react": "^16.4.0"
"react": "^16.4.0",
"@types/react": "^16.3.17"
},
"devDependencies": {
"@types/react": "^16.3.17",
"typescript": "^2.9.1"
},
"scripts": {
"compile": "tsc",
"prepublishOnly": "npm run compile",
"test": "echo \"Error: no test specified\" && exit 0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/unsplash/react-progressive-enhancement.git"
},
"contributors": [
{
"name": "Sami Jaber",
"email": "jabersami@gmail.com"
}
],
"engines": {
"yarn": "1.5.1"
},
Expand Down
13 changes: 7 additions & 6 deletions src/consumer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import * as React from 'react';
import { Consumer, ProgressiveEnhancementProp } from './context';
import { ObjectOmit, getDisplayName } from './helpers';

export const withIsEnhanced = function<OwnProps extends ProgressiveEnhancementProp>(
export const withIsEnhanced = <OwnProps extends ProgressiveEnhancementProp>(
ComposedComponent: React.ComponentType<OwnProps>,
) {
type OwnPropsWithoutProgressiveEnhancementProp = ObjectOmit<OwnProps, ProgressiveEnhancementProp>;

const ComponentWithIsEnhanced: React.SFC<OwnPropsWithoutProgressiveEnhancementProp> = props => (
) => {
type ComponentWithIsEnhancedType = React.SFC<ObjectOmit<OwnProps, ProgressiveEnhancementProp>>;
const ComponentWithIsEnhanced: ComponentWithIsEnhancedType = props => (
<Consumer>
{({ isEnhanced }) => <ComposedComponent isEnhanced={isEnhanced} {...props} />}
</Consumer>
Expand All @@ -19,7 +18,9 @@ export const withIsEnhanced = function<OwnProps extends ProgressiveEnhancementPr
return ComponentWithIsEnhanced;
};

export const progressivelyEnhance = function<Props>(ComposedComponent: React.ComponentType<Props>) {
export const progressivelyEnhance = <Props extends {}>(
ComposedComponent: React.ComponentType<Props>,
) => {
const ProgressivelyEnhance: React.SFC<Props> = props => (
<Consumer>
{({ isEnhanced }) => (isEnhanced ? <ComposedComponent {...props} /> : null)}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ComponentType } from 'react';

export const getDisplayName = (ComposedComponent: ComponentType<any>) =>
ComposedComponent.displayName || 'Component';
ComposedComponent.displayName !== undefined ? ComposedComponent.displayName : 'Component';

export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
export type ObjectOmit<T extends K, K> = Omit<T, keyof K>;
4 changes: 2 additions & 2 deletions src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { ProgressiveEnhancementProp, Provider, defaultValue } from './context';

import { getDisplayName } from './helpers';

export const enableProgressiveEnhancementsOnMount = function<Props>(
export const enableProgressiveEnhancementsOnMount = <Props extends {}>(
ComposedComponent: React.ComponentType<Props>,
): React.ComponentType<Props> {
): React.ComponentType<Props> => {
class ProgressiveEnhancementProvider extends React.Component<Props, ProgressiveEnhancementProp> {
static displayName = `ProgressiveEnhancementProvider(${getDisplayName(ComposedComponent)})`;

Expand Down

0 comments on commit ac027e6

Please sign in to comment.