Skip to content

Commit 0e99c1a

Browse files
committed
first commit
0 parents  commit 0e99c1a

File tree

13 files changed

+20426
-0
lines changed

13 files changed

+20426
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules/
2+
*.map
3+
*.yml
4+
.npmignore

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# A simple React inline editing component.

build/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare function testMethod(param: string): string;

build/index.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 19954 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "react-inline-input-edit",
3+
"version": "1.0.0",
4+
"description": "a simple inline input editor for react",
5+
"main": "build/index.js",
6+
"module": "build/index.es.js",
7+
"jsnext:main": "build/index.es.js",
8+
"scripts": {
9+
"build": "rollup -c",
10+
"test": "jest -u"
11+
},
12+
"keywords": [
13+
"react",
14+
"inline",
15+
"editor",
16+
"input"
17+
],
18+
"author": "William Pei Yuan",
19+
"license": "ISC",
20+
"peerDependencies": {
21+
"react": "^16.13.1",
22+
"react-dom": "^16.13.1"
23+
},
24+
"devDependencies": {
25+
"@testing-library/react": "^10.4.7",
26+
"@types/jest": "^26.0.7",
27+
"@types/react": "^16.9.43",
28+
"@types/react-dom": "^16.9.8",
29+
"@types/react-test-renderer": "^16.9.2",
30+
"jest": "^26.1.0",
31+
"react": "^16.13.1",
32+
"react-dom": "^16.13.1",
33+
"react-scripts-ts": "^3.1.0",
34+
"react-test-renderer": "^16.13.1",
35+
"rollup": "^2.23.0",
36+
"rollup-plugin-commonjs": "^10.1.0",
37+
"rollup-plugin-node-resolve": "^5.2.0",
38+
"rollup-plugin-peer-deps-external": "^2.2.3",
39+
"rollup-plugin-typescript2": "^0.27.1",
40+
"ts-jest": "^26.1.3",
41+
"typescript": "^3.9.7"
42+
},
43+
"jest": {
44+
"preset": "ts-jest",
45+
"testEnvironment": "jsdom"
46+
},
47+
"files": [
48+
"build"
49+
]
50+
}

react-inline-input-edit-1.0.0.tgz

859 Bytes
Binary file not shown.

rollup.config.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import typescript from 'rollup-plugin-typescript2';
2+
import commonjs from 'rollup-plugin-commonjs';
3+
import external from 'rollup-plugin-peer-deps-external';
4+
import resolve from 'rollup-plugin-node-resolve';
5+
6+
import pkg from './package.json';
7+
8+
export default {
9+
input: 'src/index.tsx',
10+
output: [
11+
{
12+
file: pkg.main,
13+
format: 'cjs',
14+
exports: 'named',
15+
sourcemap: true,
16+
},
17+
{
18+
file: pkg.module,
19+
format: 'es',
20+
exports: 'named',
21+
sourcemap: true,
22+
},
23+
],
24+
plugins: [
25+
external(),
26+
resolve(),
27+
typescript({
28+
rollupCommonJSResolveHack: true,
29+
exclude: '**/__tests__/**',
30+
clean: true,
31+
}),
32+
commonjs({
33+
include: ['node_modules/**'],
34+
namedExports: {
35+
'node_modules/react/react.js': [
36+
'Children',
37+
'Component',
38+
'PropTypes',
39+
'createElement',
40+
],
41+
'node_modules/react-dom/index.js': ['render'],
42+
},
43+
}),
44+
],
45+
};
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Inline Input edit component. should render a default "Click to edit" label when no text prop or labelPlaceHolder prop is provided. 1`] = `
4+
<label
5+
data-testid="label-component"
6+
onClick={[Function]}
7+
style={
8+
Object {
9+
"cursor": "auto",
10+
"fontSize": undefined,
11+
"fontWeight": undefined,
12+
}
13+
}
14+
>
15+
Click to edit
16+
</label>
17+
`;
18+
19+
exports[`Inline Input edit component. should render a label when a labelPlaceHolder prop is provided. 1`] = `
20+
<label
21+
data-testid="label-component"
22+
onClick={[Function]}
23+
style={
24+
Object {
25+
"cursor": "auto",
26+
"fontSize": undefined,
27+
"fontWeight": undefined,
28+
}
29+
}
30+
>
31+
hey there
32+
</label>
33+
`;
34+
35+
exports[`Inline Input edit component. should render a text label given a text prop. 1`] = `
36+
<label
37+
data-testid="label-component"
38+
onClick={[Function]}
39+
style={
40+
Object {
41+
"cursor": "auto",
42+
"fontSize": undefined,
43+
"fontWeight": undefined,
44+
}
45+
}
46+
>
47+
Chuck Norris’ tears cure cancer. Too bad he has never cried.
48+
</label>
49+
`;
50+
51+
exports[`Inline Input edit component. should render an input when label is clicked, and should update label with new input text. 1`] = `
52+
<DocumentFragment>
53+
<input
54+
data-testid="input-component"
55+
type="text"
56+
value=""
57+
/>
58+
</DocumentFragment>
59+
`;
60+
61+
exports[`Inline Input edit component. should render an input when label is clicked, and should update label with new input text. 2`] = `
62+
<DocumentFragment>
63+
<label
64+
data-testid="label-component"
65+
style="cursor: auto;"
66+
>
67+
check me out
68+
</label>
69+
</DocumentFragment>
70+
`;
71+
72+
exports[`Inline Input edit component. should render optional props provided by user. 1`] = `
73+
<DocumentFragment>
74+
<label
75+
class="red"
76+
data-testid="label-component"
77+
style="cursor: pointer; font-size: 20px; font-weight: 500;"
78+
>
79+
Chuck Norris
80+
</label>
81+
</DocumentFragment>
82+
`;
83+
84+
exports[`Inline Input edit component. should render optional props provided by user. 2`] = `
85+
<DocumentFragment>
86+
<input
87+
class="the-one"
88+
data-testid="input-component"
89+
maxlength="20"
90+
placeholder="the Chuck Norris"
91+
style="width: 500px; height: 20px; font-size: 30px; font-weight: 500; border-width: 20px;"
92+
tabindex="1"
93+
type="text"
94+
value="Chuck Norris"
95+
/>
96+
</DocumentFragment>
97+
`;

src/__tests__/component.test.tsx

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import React from 'react';
2+
import { render, fireEvent, getByTestId } from '@testing-library/react';
3+
import renderer from 'react-test-renderer';
4+
5+
import { InlineInputEdit } from '..';
6+
7+
describe('Inline input edit component', () => {
8+
test('should render a text label given a text prop.', () => {
9+
const text = 'Chuck Norris’ tears cure cancer. Too bad he has never cried.';
10+
const component = renderer.create(<InlineInputEdit text={text} />);
11+
const testInstance = component.root;
12+
13+
const actual = testInstance.findByType(InlineInputEdit).props.text;
14+
const expected =
15+
'Chuck Norris’ tears cure cancer. Too bad he has never cried.';
16+
17+
expect(actual).toBe(expected);
18+
19+
let tree = component.toJSON();
20+
expect(tree).toMatchSnapshot();
21+
});
22+
23+
test('should render a default "Click to edit" label when no text prop or labelPlaceHolder prop is provided.', () => {
24+
const text = '';
25+
const component = renderer.create(<InlineInputEdit text={text} />);
26+
const testInstance = component.root;
27+
28+
const actual = testInstance.findByType(InlineInputEdit).props.text;
29+
const expected = '';
30+
31+
expect(actual).toBe(expected);
32+
33+
let tree = component.toJSON();
34+
expect(tree).toMatchSnapshot();
35+
});
36+
37+
test('should render a label when a labelPlaceHolder prop is provided.', () => {
38+
const text = '';
39+
const labelPlaceHolder = 'hey there';
40+
const component = renderer.create(
41+
<InlineInputEdit text={text} labelPlaceHolder={labelPlaceHolder} />
42+
);
43+
const testInstance = component.root;
44+
45+
const actualText = testInstance.findByType(InlineInputEdit).props.text;
46+
const expectedText = '';
47+
48+
const actualPlaceholder = testInstance.findByType(InlineInputEdit).props
49+
.labelPlaceHolder;
50+
const expectedPlaceholder = 'hey there';
51+
52+
expect(actualText).toBe(expectedText);
53+
expect(actualPlaceholder).toBe(expectedPlaceholder);
54+
55+
let tree = component.toJSON();
56+
expect(tree).toMatchSnapshot();
57+
});
58+
59+
test('should render an input when label is clicked, and should update label with new input text.', () => {
60+
const text = '';
61+
const { container, asFragment } = render(<InlineInputEdit text={text} />);
62+
let labelComp = getByTestId(container, 'label-component');
63+
fireEvent.click(labelComp);
64+
expect(asFragment()).toMatchSnapshot();
65+
66+
const inputComp = getByTestId(container, 'input-component');
67+
fireEvent.change(inputComp, { target: { value: 'check me out' } });
68+
fireEvent.blur(inputComp);
69+
70+
labelComp = getByTestId(container, 'label-component');
71+
expect(labelComp.textContent).toBe('check me out');
72+
expect(asFragment()).toMatchSnapshot();
73+
});
74+
75+
test('should render optional props provided by user.', () => {
76+
const optionalProps = {
77+
text: 'Chuck Norris',
78+
labelCursor: 'pointer',
79+
labelClassName: 'red',
80+
labelFontSize: '20px',
81+
labelFontWeight: '500',
82+
83+
inputMaxLength: 20,
84+
inputPlaceHolder: 'the Chuck Norris',
85+
inputTabIndex: 1,
86+
inputWidth: 500,
87+
inputHeight: 20,
88+
inputFontSize: 30,
89+
inputFontWeight: 500,
90+
inputClassName: 'the-one',
91+
inputBorderWidth: 20,
92+
93+
onFocus: (text: any) => text,
94+
onFocusOut: (text: any) => text,
95+
};
96+
const { container, asFragment } = render(
97+
<InlineInputEdit {...optionalProps} />
98+
);
99+
expect(asFragment()).toMatchSnapshot();
100+
101+
let labelComp = getByTestId(container, 'label-component');
102+
fireEvent.click(labelComp);
103+
expect(asFragment()).toMatchSnapshot();
104+
});
105+
});

0 commit comments

Comments
 (0)