Skip to content
This repository has been archived by the owner on Jun 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #42 from rpearce/typescript
Browse files Browse the repository at this point in the history
convert to typescript
  • Loading branch information
rpearce committed Mar 14, 2020
2 parents 72e71c8 + 89226d1 commit fea8a29
Show file tree
Hide file tree
Showing 40 changed files with 5,759 additions and 34,366 deletions.
6 changes: 4 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
coverage/
docs/
!.eslintrc.js
!.prettierrc.js
!.storybook/
dist/
docs/
37 changes: 22 additions & 15 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,41 @@ module.exports = {
node: true
},
extends: [
'eslint:recommended',
'plugin:jsx-a11y/recommended',
'plugin:react/recommended',
'plugin:@typescript-eslint/recommended',
'prettier/@typescript-eslint',
'plugin:prettier/recommended',
'plugin:react/recommended'
'plugin:jsx-a11y/recommended'
],
parserOptions: {
ecmaVersion: 2019,
ecmaVersion: 2020,
sourceType: 'module'
},
plugins: [
'jsx-a11y',
'react',
'react-hooks'
],
parser: '@typescript-eslint/parser',
plugins: ['jsx-a11y', 'react', 'react-hooks', '@typescript-eslint'],
rules: {
'indent': ['error', 2, { 'SwitchCase': 1 }],
'jsx-quotes': ['error', 'prefer-double'],
'jsx-a11y/no-onchange': 0,
'no-trailing-spaces': 'error',
'object-curly-spacing': ['error', 'always'],
'quotes': ['error', 'single', { 'allowTemplateLiterals': true }],
'react-hooks/exhaustive-deps': 'warn',
quotes: ['error', 'single', { allowTemplateLiterals: true }],
'react-hooks/exhaustive-deps': 'error',
'react-hooks/rules-of-hooks': 'error',
'semi': ['error', 'never']
'react/prop-types': 0,
semi: ['error', 'never']
},
settings: {
react: {
version: '16'
version: 'detect'
}
},
overrides: [
{
files: ['*.js', '*.jsx'],
rules: {
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-var-requires': 'off'
}
}
}
]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ typings/

dist/
*.tmp
.DS_Store
1 change: 1 addition & 0 deletions .storybook/addons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@storybook/addon-a11y/register'
37 changes: 37 additions & 0 deletions .storybook/base.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
html {
font-size: 16px;
box-sizing: border-box;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
}
html,
body {
min-height: 100%;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
margin: 2em;
font-size: 1rem;
font-family: monospace;
color: #333;
}
body,
input,
button {
-webkit-font-smoothing: antialiased;
}
.textarea {
width: 100%;
outline: 0;
margin: 1rem 0;
padding: 0.5rem 1rem;
border: none;
background-color: #FFF;
border-left: 4px solid #005C9A;
resize: none;
font: inherit;
}
7 changes: 7 additions & 0 deletions .storybook/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { configure } from '@storybook/react'

function loadStories() {
require('../stories.js')
}

configure(loadStories, module)
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
language: node_js
node_js:
- "12"
- "13"
before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash
- export PATH="$HOME/.yarn/bin:$PATH"
cache: yarn
script:
- yarn ci
after_success:
- npm run coverage
- yarn run coverage
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [2.2.0] - 2020-03-14

### Added
* typescript support

### Changed
* license from ISC to BSD-3

### Fixed
* issue where `line-height` is `normal` and calculation breaks by falling back
to `fontSize * 1.2`
* upgrades to fix vulnerabilities

## [2.1.2] - 2020-02-04

### Added
Expand Down
31 changes: 27 additions & 4 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
ISC License (ISC)
Copyright (c) 2020, Robert Pearce

Copyright 2019 Robert Pearce
All rights reserved.

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of Robert Pearce nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# react-expanding-textarea
[![All Contributors](https://img.shields.io/badge/all_contributors-9-orange.svg?style=flat-square)](#contributors-)
[![npm version](https://img.shields.io/npm/v/react-expanding-textarea.svg)](https://www.npmjs.com/package/react-expanding-textarea) [![npm downloads](https://img.shields.io/npm/dm/react-expanding-textarea.svg)](https://www.npmjs.com/package/react-expanding-textarea) [![Build Status](https://travis-ci.org/rpearce/react-expanding-textarea.svg?branch=master)](https://travis-ci.org/rpearce/react-expanding-textarea) [![Coverage Status](https://coveralls.io/repos/github/rpearce/react-expanding-textarea/badge.svg?branch=master)](https://coveralls.io/github/rpearce/react-expanding-textarea?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/f1395be9796a63b3d9a2/maintainability)](https://codeclimate.com/github/rpearce/react-expanding-textarea/maintainability)
[![npm version](https://img.shields.io/npm/v/react-expanding-textarea.svg)](https://www.npmjs.com/package/react-expanding-textarea) [![npm downloads](https://img.shields.io/npm/dm/react-expanding-textarea.svg)](https://www.npmjs.com/package/react-expanding-textarea) [![bundlephobia size](https://badgen.net/bundlephobia/minzip/react-expanding-textarea)](bundlephobia.com/result?p=react-expanding-textarea) [![Build Status](https://travis-ci.org/rpearce/react-expanding-textarea.svg?branch=master)](https://travis-ci.org/rpearce/react-expanding-textarea) [![Coverage Status](https://coveralls.io/repos/github/rpearce/react-expanding-textarea/badge.svg?branch=master)](https://coveralls.io/github/rpearce/react-expanding-textarea?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/f1395be9796a63b3d9a2/maintainability)](https://codeclimate.com/github/rpearce/react-expanding-textarea/maintainability)

React textarea component to automatically expand and contract your textareas.

Expand Down
11 changes: 11 additions & 0 deletions __tests__/__snapshots__/index.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`continues to work if no onChange and no onInput 1`] = `
<DocumentFragment>
<textarea
style="height: 2px; overflow-y: hidden;"
>
Some text
</textarea>
</DocumentFragment>
`;
50 changes: 34 additions & 16 deletions __tests__/index.js → __tests__/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React, { createRef } from 'react'
import React, { CSSProperties, createRef } from 'react'
import { fireEvent, render } from '@testing-library/react'
import '@testing-library/jest-dom/extend-expect'

import ExpandingTextarea, { getHeight, resize } from '../source'

test('renders with a minimum number of rows', () => {
const wrapStyles = {
const wrapStyles: React.CSSProperties = {
boxSizing: 'border-box',
maxWidth: '500px'
}
Expand Down Expand Up @@ -48,67 +47,86 @@ test('calls onChange & onInput when text changes', () => {
expect(onInput).toBeCalled()
})

test('continues to work if no onChange and no onInput', () => {
const onChange = jest.fn()
const onInput = jest.fn()
const { asFragment, getByText } = render(
<ExpandingTextarea value="Some text" />
)

fireEvent.input(getByText('Some text'), {
target: { value: 'Some text is here' }
})

expect(asFragment()).toMatchSnapshot()
})

test('getHeight: returns scroll height when no rows', () => {
global.window.getComputedStyle = jest.fn().mockImplementation(() => ({
window.getComputedStyle = jest.fn().mockImplementation(() => ({
borderBottomWidth: '4px',
borderTopWidth: '4px',
lineHeight: '27.2px',
paddingBottom: '10px',
paddingTop: '10px'
}))
const el = { scrollHeight: 129 }
const el = document.createElement('textarea')
jest.spyOn(el, 'scrollHeight', 'get').mockImplementation(() => 129)

expect(getHeight(undefined, el)).toEqual(137)
expect(getHeight(0, el)).toEqual(137)
})

test('getHeight: returns scroll height when larger than row height', () => {
global.window.getComputedStyle = jest.fn().mockImplementation(() => ({
window.getComputedStyle = jest.fn().mockImplementation(() => ({
borderBottomWidth: '4px',
borderTopWidth: '4px',
lineHeight: '27.2px',
paddingBottom: '10px',
paddingTop: '10px'
}))
const el = { scrollHeight: 129 }
const el = document.createElement('textarea')
jest.spyOn(el, 'scrollHeight', 'get').mockImplementation(() => 129)

expect(getHeight(3, el)).toEqual(137)
})

test('getHeight: returns row height when larger than scroll height', () => {
global.window.getComputedStyle = jest.fn().mockImplementation(() => ({
window.getComputedStyle = jest.fn().mockImplementation(() => ({
borderBottomWidth: '4px',
borderTopWidth: '4px',
lineHeight: '27.2px',
paddingBottom: '10px',
paddingTop: '10px'
}))
const el = { scrollHeight: 129 }
const el = document.createElement('textarea')
jest.spyOn(el, 'scrollHeight', 'get').mockImplementation(() => 129)

expect(getHeight(5, el)).toEqual(164)
})

test('resize: sets style height when present', () => {
global.window.getComputedStyle = jest.fn().mockImplementation(() => ({
window.getComputedStyle = jest.fn().mockImplementation(() => ({
borderBottomWidth: '4px',
borderTopWidth: '4px',
lineHeight: '27.2px',
paddingBottom: '10px',
paddingTop: '10px'
}))

const el = { scrollHeight: 129, style: { height: '100px' } }
const el = document.createElement('textarea')
jest.spyOn(el, 'scrollHeight', 'get').mockImplementation(() => 129)
el.style.height = '100px'

resize(undefined, null)
resize(0, null)

expect(el.style.height).toEqual('100px')

resize(undefined, el)
resize(0, el)

expect(el.style.height).toEqual('137px')
})

test('accepts ref', () => {
const ref = createRef(null)
const ref = createRef<HTMLTextAreaElement>()
render(<ExpandingTextarea defaultValue="Some text" ref={ref} />)
expect(ref.current.tagName).toEqual('TEXTAREA')
expect(ref.current?.tagName).toEqual('TEXTAREA')
})
5 changes: 0 additions & 5 deletions babel.config.js

This file was deleted.

Loading

0 comments on commit fea8a29

Please sign in to comment.