Skip to content

Commit 030573f

Browse files
authored
🔀 Merge pull request #68 from codermarcos/fix/#63/readonly-inputs
🐛 Fix readonly inputs
2 parents a87ea75 + 9f990a3 commit 030573f

File tree

8 files changed

+208
-13
lines changed

8 files changed

+208
-13
lines changed

CONTRIBUTING.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ Here are some guides to names and patterns which helps everybody understand cont
3434

3535
Take a look at some examples:
3636

37-
* `refactor/CF-321/remove-bad-code`
38-
* `feat/CF-666/add-path-to-hell`
39-
* `fix/CF-999/rollback-path-hell`
40-
* `doc/CF-123/add-lessons-learned`
37+
* `refactor/#666/remove-bad-code`
38+
* `feat/#999/add-path-to-hell`
39+
* `fix/#666/rollback-path-hell`
40+
* `doc/#123/add-lessons-learned`
4141

4242
### 💬 Git Commit Messages
4343

README.md

+76
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,79 @@ Read the [docs](docs/) or chose your implementation to check an example:
175175
* Allow lock the carret bar on end
176176
* Allow choose how many fractionalDigits
177177
* Allow choose decimal and thousands separators
178+
179+
## 🦾 Contribute to this project
180+
181+
This project needs your help, all contributions are welcome!
182+
183+
For it please take a look for some patterns in [this documentation](https://github.com/codermarcos/simple-mask-money/blob/main/CONTRIBUTING.md).
184+
185+
186+
### Requirements to run this project
187+
188+
This repository uses:
189+
190+
| Dependencies | Description | Docs |
191+
| -------------------------------------------- | ------------------ | ---------------- |
192+
| [![node-version]][node-download] | Javascript Runtime | [📚][node-doc] |
193+
194+
> You can use [NVM][nvm-download] to select correct node version based on file `.nvmrc`.
195+
196+
### Developing in this project
197+
198+
1. Clone it:
199+
200+
```bash
201+
git clone [email protected]:codermarcos/simple-mask-money.git
202+
```
203+
204+
Or just [make a fork](https://docs.github.com/en/get-started/quickstart/fork-a-repo).
205+
206+
2. Install the dependecies whitout change the lock file:
207+
208+
```bash
209+
npm ci
210+
```
211+
212+
> Just change the lock file if necessary.
213+
214+
3. Run build to generate a build:
215+
216+
```bash
217+
npm run build
218+
```
219+
220+
### Before open a PR
221+
222+
Remember of write unit and e2e tests, and make it works!
223+
224+
1. Run unit tests
225+
226+
```bash
227+
npm run test
228+
```
229+
230+
2. Run e2e tests
231+
232+
```bash
233+
npx cypress open
234+
```
235+
236+
## 📖 Learn More
237+
238+
To learn more about this project, take a look at the following resources:
239+
240+
* [Typescript][ts-doc] - understand about typescript.
241+
* [Rollup][rollup-doc] - understand about the bundler used by this project.
242+
* [Jest][jest-doc] - understand about the tool that we use for write unit tests.
243+
* [Cypress][cypress-doc] - understand about the tool that we use for write e2e tests.
244+
245+
[node-download]: https://nodejs.org/dist/v16.9.1/
246+
[node-doc]: https://nodejs.org/dist/latest-v16.x/docs/api/
247+
248+
[nvm-download]: https://github.com/nvm-sh/nvm
249+
250+
[ts-doc]: https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html
251+
[rollup-doc]: https://rollupjs.org/configuration-options/
252+
[jest-doc]: https://jestjs.io/pt-BR/docs/using-matchers
253+
[cypress-doc]: https://docs.cypress.io/guides/overview/why-cypress#Writing-tests

cypress/e2e/v4.x.x/set-mask.cy.ts

+80-1
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,95 @@
22

33
import type { SimpleMaskMoneyConfiguration } from '../../../src/types';
44

5-
const getUrl = (configuration: Partial<SimpleMaskMoneyConfiguration>, initalValue?: string) => {
5+
const getUrl = (configuration: Partial<SimpleMaskMoneyConfiguration>, initalValue?: string, attr?: Array<string>) => {
66
const options = new Array<string>();
77

88
Object.keys(configuration).forEach((key) => options.push(`${key}=${encodeURIComponent(configuration[key])}`));
99

1010
if (initalValue) options.push(`initialValue=${initalValue}`);
11+
if (attr) options.push(`attributes=${attr.join(',')}`);
1112

1213
return `./cypress/file/?${options.join('&')}`;
1314
};
1415

16+
17+
describe(
18+
'input behaviour',
19+
() => {
20+
describe(
21+
'readonly',
22+
() => {
23+
24+
beforeEach(
25+
() => {
26+
cy.visit(getUrl({ prefix: '$', suffix: 'CAD' }, '6.66', ['readonly']));
27+
cy.reload();
28+
}
29+
);
30+
31+
it(
32+
'should format when input was created',
33+
() => {
34+
cy.get('input').should('have.value', '$6,66CAD');
35+
}
36+
);
37+
38+
it(
39+
'shouldn\'t allow type when is readonly',
40+
(done) => {
41+
const prevent = async (error) => {
42+
const passed = error.message.includes('readonly');
43+
cy.off('fail', prevent);
44+
if (passed) done();
45+
return !passed;
46+
};
47+
48+
cy.on('fail', prevent);
49+
cy.get('input').type('123', { timeout: 100 });
50+
}
51+
);
52+
}
53+
);
54+
55+
describe(
56+
'readonly',
57+
() => {
58+
59+
beforeEach(
60+
() => {
61+
cy.visit(getUrl({ prefix: '$', suffix: 'CAD' }, '6.66', ['disabled']));
62+
cy.reload();
63+
}
64+
);
65+
66+
it(
67+
'should format when input was created',
68+
() => {
69+
cy.get('input').should('have.value', '$6,66CAD');
70+
}
71+
);
72+
73+
it(
74+
'shouldn\'t allow type when is disabled',
75+
(done) => {
76+
const prevent = async (error) => {
77+
const passed = error.message.includes('disabled');
78+
cy.off('fail', prevent);
79+
if (passed) done();
80+
return !passed;
81+
};
82+
83+
cy.on('fail', prevent);
84+
85+
cy.get('input').type('123', { timeout: 100 });
86+
}
87+
);
88+
}
89+
);
90+
91+
}
92+
);
93+
1594
describe(
1695
'cursor',
1796
() => {

cypress/file/index.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
<script src="/lib/simple-mask-money.umd.js"></script>
1616

17-
<input type="text" />
17+
<form>
18+
<input type="text" />
19+
</form>
1820

1921
<script>
2022
const options = {};
@@ -50,6 +52,9 @@
5052

5153
if (q.has('initialValue'))
5254
document.querySelector('input').value = q.get('initialValue');
55+
56+
if (q.has('attributes'))
57+
q.get('attributes').split(',').forEach(prop => document.querySelector('input').setAttribute(prop, ''));
5358

5459
const removeMask = SimpleMaskMoney.setMask('input', options);
5560
</script>

package-lock.json

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simple-mask-money",
3-
"version": "4.1.0",
3+
"version": "4.1.1",
44
"private": false,
55
"description": "Simple money mask developed with pure JavaScript. To run on Client Side and Server Side",
66
"types": "./lib/simple-mask-money.d.ts",

src/set-mask.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,16 @@ function setMask(
312312
setCaretPosition(position);
313313
};
314314

315-
element.addEventListener('keydown', onKeyDown);
316-
document.addEventListener('selectionchange', onSelectionChange);
317-
318315
if (allowEmpty && initialValue === `${prefix}0`) {
319316
triggerInputChanges('');
320317
} else {
321318
triggerInputChanges(initialValue);
322319
}
320+
321+
if (element.hasAttribute('readonly') || element.hasAttribute('disabled')) return () => void 0;
322+
323+
element.addEventListener('keydown', onKeyDown);
324+
document.addEventListener('selectionchange', onSelectionChange);
323325

324326
const removeMask = (): void => {
325327
element.removeEventListener('keydown', onKeyDown);

tests/set-mask.test.ts

+34
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,40 @@ describe(
235235
}
236236
);
237237

238+
describe(
239+
'shouldn\'t write when is readonly or disabled',
240+
() => {
241+
it(
242+
'should format when apply mask and not allow write when is readonly',
243+
() => {
244+
input.value = '0,50';
245+
input.setAttribute('readonly', '');
246+
247+
clear = setMask(input, { prefix: 'R$', suffix: 'BRL' });
248+
249+
write('69');
250+
251+
expect(input.value).toBe('R$0,50BRL');
252+
},
253+
);
254+
255+
it(
256+
'should format when apply mask and not allow write when is disabled',
257+
() => {
258+
input.value = '0,50';
259+
input.setAttribute('disabled', '');
260+
261+
clear = setMask(input, { prefix: 'R$', suffix: 'BRL' });
262+
263+
write('69');
264+
265+
expect(input.value).toBe('R$0,50BRL');
266+
},
267+
);
268+
}
269+
);
270+
271+
238272
// Only basic tests because jsdom doesn't work to simulate user actions
239273
// Real tests at cypress
240274
describe(

0 commit comments

Comments
 (0)