|
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>@modal-sh/numerica-core</title>
- <style>
- .main-form {
- position: fixed;
- top: 0;
- left: 0;
- width: 100%;
- height: 100%;
- display: flex;
- flex-direction: column;
- align-items: stretch;
- }
-
- .main-form > fieldset {
- display: contents;
- }
-
- .main-form > fieldset > div {
- padding: 1rem;
- box-sizing: border-box;
- display: flex;
- gap: 1rem;
- }
-
- .main-form > fieldset > legend {
- position: absolute;
- left: -999999px;
- }
-
- .main-form > div {
- flex-direction: column;
- display: flex;
- flex: auto;
- }
-
- .main-form > div > textarea {
- flex: auto;
- width: 100%;
- height: 0;
- font-size: 2rem;
- padding: 1rem;
- border: 0;
- outline: 0;
- resize: none;
- box-sizing: border-box;
- }
-
- .checkbox > input {
- position: absolute;
- left: -999999px;
- }
-
- .checkbox > input + span {
- display: inline-flex;
- align-items: center;
- justify-content: center;
- height: 3rem;
- padding: 0 1rem;
- border: 1px solid #ccc;
- border-radius: 0.25rem;
- cursor: pointer;
- }
-
- .checkbox > input:checked + span {
- border-color: #000;
- background-color: #000;
- color: #fff;
- }
-
- @media (min-width: 1080px) {
- .main-form > div {
- flex-direction: row;
- }
-
- .main-form > div > textarea {
- width: 0;
- height: 100%;
- }
- }
- </style>
- </head>
- <body>
- <form id="mainForm" class="main-form">
- <fieldset>
- <legend>
- Options
- </legend>
- <div>
- <label class="checkbox">
- <input type="checkbox" name="shortenMillia">
- <span>Shorten millia</span>
- </label><label class="checkbox">
- <input type="checkbox" name="addTensDashes" checked>
- <span>Add dashes to tens</span>
- </label>
- </div>
- </fieldset>
- <div>
- <textarea aria-label="Number" name="number" placeholder="123"></textarea>
- <textarea aria-label="Name" name="name" placeholder="one hundred twenty three"></textarea>
- </div>
- </form>
- <script type="module">
- import { stringify, parse } from './numerica.js';
-
- function MainForm(el) {
- const numberInput = el.querySelector('[name="number"]');
- const nameInput = el.querySelector('[name="name"]');
- const addTensDashesCheckbox = el.querySelector('[name="addTensDashes"]');
- const shortenMilliaCheckbox = el.querySelector('[name="shortenMillia"]');
-
- const options = {
- stringify: {
- makeGroupOptions: {
- shortenMillia: false,
- addTensDashes: true,
- },
- },
- parse: {
- type: 'bigint',
- }
- };
-
- numberInput.addEventListener('input', (e) => {
- nameInput.value = '';
- if (e.currentTarget.value.trim() === '') {
- return;
- }
- nameInput.value = stringify(e.currentTarget.value, options.stringify);
- });
-
- nameInput.addEventListener('input', (e) => {
- numberInput.value = '';
- if (e.currentTarget.value.trim() === '') {
- return;
- }
- numberInput.value = parse(e.currentTarget.value, options.parse).toString();
- });
-
- addTensDashesCheckbox.addEventListener('change', (e) => {
- options.stringify.makeGroupOptions.addTensDashes = e.currentTarget.checked;
- numberInput.dispatchEvent(new Event('input'));
- });
-
- shortenMilliaCheckbox.addEventListener('change', (e) => {
- options.stringify.makeGroupOptions.shortenMillia = e.currentTarget.checked;
- numberInput.dispatchEvent(new Event('input'));
- });
- }
- const mainForm = window.document.getElementById('mainForm');
- new MainForm(mainForm);
- </script>
- </body>
- </html>
|