Skip to content

Commit

Permalink
- reorganise project to split out languages
Browse files Browse the repository at this point in the history
- JS now supports registering events against something other than `document`
  • Loading branch information
TomK committed Aug 25, 2021
1 parent 00fb19d commit 6d41026
Show file tree
Hide file tree
Showing 72 changed files with 126 additions and 106 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
/node_modules/
/yarn.lock

.yarn/*
!.yarn/releases
!.yarn/plugins
.pnp.*
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
},
"autoload": {
"psr-4": {
"Packaged\\Form\\": "src"
"Packaged\\Form\\": "php/src"
}
},
"autoload-dev": {
"psr-4": {
"Packaged\\Tests\\Form\\": "tests"
"Packaged\\Tests\\Form\\": "php/tests"
}
}
}
11 changes: 6 additions & 5 deletions demo/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ protected function _initDataHandlers()
}

?>
<html>
<html lang="en">
<head>
<link type="text/css" rel="stylesheet" href="../resources/form.min.css"/>
<script type="module" src="../resources/form.min.js"></script>
<title>Form Demo</title>
<link type="text/css" rel="stylesheet" href="../js/dist/form.min.css"/>
<script type="module" src="../js/dist/form.min.js"></script>
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 13px;
}

Expand Down Expand Up @@ -161,7 +162,7 @@ protected function _initDataHandlers()
<?php if(substr($form->profilePicture->getFileType(), 0, 5) == 'image'): ?>
<img src="data:<?= $form->profilePicture->getFileType() ?>;base64,<?= base64_encode($content); ?>">
<?php else: ?>
<textarea><?= $content; ?></textarea>
<pre><?= $content; ?></pre>
<?php endif; ?>
</td>
</tr>
Expand Down
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
import './resources_src/index';
import './resources_src/css/styles'
import './js/src/css/styles.js';
import {init} from './js/src/index.js';

export * from './js/src/index.js';

init();
1 change: 1 addition & 0 deletions js/dist/form.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions js/dist/form.min.js

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions js/src/css/styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import './base/base.js';
import './inputs/inputs.js';
import './errors/errors.js';
import './button/button.js';
import './guidance/guidance.js';
86 changes: 86 additions & 0 deletions js/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {addErrors, clearErrors, validateForm, validateHandler} from './validation.js';

export * from './validation.js';

let _init = new WeakSet();

export function init(rootElement = document)
{
if(_init.has(rootElement))
{
return;
}
_init.add(rootElement);

rootElement.addEventListener(
'submit', e =>
{
/**
* @type {HTMLFormElement}
*/
const form = e.path && e.path[0] || e.target;
const results = validateForm(form);
results.forEach(
(result, handlerName) =>
{
// show errors if necessary
const errContainer = form.querySelector(`.p-form__field[handler-name="${handlerName}"] .p-form__errors`);
if(errContainer)
{
errContainer.classList.toggle('p-form__errors--hidden', result.errors.length === 0);
}

if(result.errors.length > 0)
{
clearErrors(form, handlerName);
addErrors(form, handlerName, result.errors);
e.preventDefault();
e.stopImmediatePropagation();
}
},
);
},
);

rootElement.addEventListener(
'reset', e =>
{
/**
* @type {HTMLFormElement}
*/
const form = e.path && e.path[0] || e.target;
form.querySelectorAll('.p-form__field[handler-name]')
.forEach((ele) => clearErrors(form, ele.getAttribute('handler-name')));
}
);

rootElement.addEventListener('input', e =>
{
const inputEle = e.path && e.path[0] || e.target;
const handlerContainer = inputEle.closest('.p-form__field');
if(!handlerContainer || !handlerContainer.hasAttribute('handler-name'))
{
return;
}
const handlerName = handlerContainer.getAttribute('handler-name');

const form = inputEle.closest('form');
if(!form)
{
return;
}

const result = validateHandler(form, handlerName);
const errContainer = handlerContainer.querySelector(`.p-form__errors`);
if(errContainer && result.errors.length === 0)
{
clearErrors(form, handlerName);
errContainer.classList.add('p-form__errors--hidden');
}
else if(!result.potentiallyValid)
{
clearErrors(form, handlerName);
addErrors(form, handlerName, result.errors);
}
});
}
File renamed without changes.
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{
"name": "@packaged/form",
"version": "3.6.9",
"main": "resources_src/index.js",
"version": "4.0.0",
"main": "/index.js",
"repository": "[email protected]:packaged/form",
"author": "Tom Kay <[email protected]>",
"license": "MIT",
"files": [
"resources",
"resources_src",
"js",
"index.js"
],
"dependencies": {
"@packaged/validate": "^2.7.1",
"base-64": "^1.0.0"
},
"devDependencies": {
"@babel/core": "^7.11.1",
"@babel/preset-env": "^7.11.0",
"@rollup/plugin-babel": "^5.2.1",
"@rollup/plugin-commonjs": "^13.0.0",
"@rollup/plugin-node-resolve": "^8.0.1",
"@babel/core": "^7.15.0",
"@babel/preset-env": "^7.15.0",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "^20.0.0",
"@rollup/plugin-node-resolve": "^13.0.4",
"core-js": "3",
"postcss": "^8.3.6",
"postcss-preset-env": "^6.7.0",
"rollup": "^2.15.0",
"rollup-plugin-postcss": "^3.1.8",
"rollup-plugin-terser": "^6.1.0"
"rollup": "^2.56.3",
"rollup-plugin-postcss": "^4.0.1",
"rollup-plugin-terser": "^7.0.2"
},
"scripts": {
"build": "rollup -c rollup.config.js -w"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use Packaged\Form\DataHandlers\ReadOnlyDataHandler;
use Packaged\Form\DataHandlers\TextDataHandler;
use Packaged\Form\Decorators\InputDecorator;
use Packaged\Tests\Form\Supporting\DataHandlers\TestIntegerDataHandler;
use PHPUnit\Framework\TestCase;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion tests/FormTest.php → php/tests/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Packaged\Tests\Form;

use Packaged\Form\DataHandlers\TextDataHandler;
use Packaged\Form\Decorators\ReadOnlyDecorator;
use Packaged\Tests\Form\Supporting\CustomDecoratorForm;
use Packaged\Tests\Form\Supporting\EmptyForm;
use Packaged\Tests\Form\Supporting\TestForm;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion resources/form.min.css

This file was deleted.

1 change: 0 additions & 1 deletion resources/form.min.js

This file was deleted.

5 changes: 0 additions & 5 deletions resources_src/css/styles.js

This file was deleted.

73 changes: 0 additions & 73 deletions resources_src/index.js

This file was deleted.

6 changes: 3 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ process.chdir(__dirname);
module.exports = {
input: './index.js',
output: {
file: './resources/form.min.js',
name: 'form',
format: 'iife',
file: './js/dist/form.min.js',
name: 'Form',
format: 'umd'
},
plugins: [
postcss(
Expand Down

0 comments on commit 6d41026

Please sign in to comment.