Skip to content

Commit

Permalink
Merge pull request #20 from AegisJSProject/patch/updates
Browse files Browse the repository at this point in the history
Update to latest dependencies
  • Loading branch information
shgysk8zer0 authored Mar 10, 2024
2 parents a3d1ebc + 12a558a commit e4de24c
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 65 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [v0.1.2] - 2024-03-10

### Added
- Add WIP Aegis Checkbox base class/component
- Add Input Errors as static properties to base AegisInput

### Changed
- Misc updates

## [v0.1.1] - 2024-03-03

### Changed
Expand Down
52 changes: 52 additions & 0 deletions checkbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { AegisInput } from './input.js';
import { SYMBOLS } from './consts.js';

const protectedData = new WeakMap();

function updateChecked(el) {
const internals = protectedData.get(el).internals;

if (el.checked) {
internals.ariaChecked = 'true';
} else {
internals.ariaChecked = 'false';

if (el.required) {
internals.setValidity({
valueMissing: true,
}, 'This input is required');
}
}
}

export class AegisCheckbox extends AegisInput {
constructor(opts = {}) {
super({ ...opts, role: 'checkbox' });
console.warn(new Error('Aegis Checkbox components are still in development and should not be used.'));
}

async [SYMBOLS.initialize](opts) {
const { internals } = await super[SYMBOLS.initialize](opts);
protectedData.set(this, { internals });
}

attributeChangedCallback(name, oldValue, newValue) {
if (name === 'checked') {
updateChecked(this);
} else {
super.addtributeChangedCallback(name, oldValue, newValue);
}
}

get checked() {
return this.hasAttribute('checked');
}

set checked(val) {
this.toggleAttribute('checked', val);
}

static get observedAttributes() {
return [...AegisInput.observedAttributes, 'checked'];
}
}
64 changes: 49 additions & 15 deletions input.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ import {
StepMismatchError, BadInputError,
} from './errors.js';

export const ERRORS = {
AegisInputError, ValueMissingError, TypeMismatchError, PatternMismatchError,
TooLongError, TooShortError, RangeUnderflowError, RangeOverflowError,
StepMismatchError, BadInputError,
};

/**
* @see https://web.dev/articles/more-capable-form-controls
*/
Expand All @@ -31,9 +25,7 @@ export class AegisInput extends AegisComponent {
role = 'textbox',
...rest
} = {}) {
if (! (this[SYMBOLS.sanitizeValue] instanceof Function)) {
throw new Error(`${this.tagName.toLowerCase()} does not have a [${SYMBOLS.setValue.toString()}] method.`);
} else if (! this[SYMBOLS.initialized]) {
if (! this[SYMBOLS.initialized]) {
const { shadow: s, internals: i } = await super[SYMBOLS.initialize]({
role, ...rest,
});
Expand Down Expand Up @@ -77,17 +69,19 @@ export class AegisInput extends AegisComponent {
const { internals, shadow } = protectedData.get(this);

try {
const result = await this[SYMBOLS.sanitizeValue]({ value, internals, shadow });
this.#value = result;
internals.setFormValue(result);
const state = this[SYMBOLS.sanitizeValue] instanceof Function
? await this[SYMBOLS.sanitizeValue]({ value, internals, shadow })
: value;
this.#value = state;
internals.setFormValue(value, state);
internals.setValidity({}, '');
internals.ariaInvalid = 'false';
internals.states.delete(STATES.invalid);
internals.states.add(STATES.valid);
this.removeAttribute('aria-errormessage');

if (typeof result === 'string') {
internals.ariaValueNow = result;
if (typeof value === 'string') {
internals.ariaValueNow = value;
}

this.dispatchEvent(new Event(EVENTS.valid));
Expand Down Expand Up @@ -204,4 +198,44 @@ export class AegisInput extends AegisComponent {
static get observedAttributes() {
return [...AegisComponent.observedAttributes, 'required', 'disabled', 'readonly'];
}
}

static get AegisInputError() {
return AegisInputError;
}

static get ValueMissingError() {
return ValueMissingError;
}

static get TypeMismatchError() {
return TypeMismatchError;
}

static get PatternMismatchError() {
return PatternMismatchError;
}

static get TooLongError() {
return TooLongError;
}

static get TooShortError() {
return TooShortError;
}

static get RangeUnderflowError() {
return RangeUnderflowError;
}

static get RangeOverflowError() {
return RangeOverflowError;
}

static get StepMismatchError() {
return StepMismatchError;
}

static get BadInputError() {
return BadInputError;
}
}
18 changes: 9 additions & 9 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aegisjsproject/component",
"version": "0.1.1",
"version": "0.1.2",
"description": "Base component using `@aegisjsproject/core` & `@aegisjsproject/styles`",
"keywords": [
"aegis",
Expand Down Expand Up @@ -96,7 +96,7 @@
"rollup": "^4.9.6"
},
"dependencies": {
"@aegisjsproject/core": "^0.1.2",
"@aegisjsproject/core": "^0.1.4",
"@aegisjsproject/styles": "^0.1.1"
}
}
52 changes: 52 additions & 0 deletions test/components/checkbox-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { AegisCheckbox } from '@aegisjsproject/component/checkbox.js';
import { SYMBOLS } from '@aegisjsproject/component/consts.js';
import { html } from '@aegisjsproject/core/parsers/html.js';
import { css } from '@aegisjsproject/core/parsers/css.js';

const template = html`<slot name="tick"><span part="tick" class="tick"></span></slot>`;
const styles = css`
:host {
display: inline-block;
width: 24px;
height: 24px;
border: 6px solid black;
border-radius: 4px;
background-color: white;
cursor: pointer;
}
:host([disabled]), :host([readonly]) {
cursor: auto;
}
:host(:not([checked])) ::slotted([slot="tick"]), :host(:not([checked])) .tick {
display: none;
}
:host .tick {
background-color: gray;
display: inline-block;
width: 100%;
height: 100%;
}`;

function toggleChecked({ type, key, currentTarget }) {
if (! (currentTarget.disabled || currentTarget.readOnly || (type === 'keydown' && key !== ' '))) {
currentTarget.checked = ! currentTarget.checked;
}
}

export class CheckboxTest extends AegisCheckbox {
constructor() {
super({ template, styles });
this.addEventListener('click', toggleChecked);
this.addEventListener('keydown', toggleChecked);
this.tabIndex = '0';
}

async [SYMBOLS.render](type, data) {
console.log({ el: this, type, ...data });
}
}

CheckboxTest.register('checkbox-test');
4 changes: 2 additions & 2 deletions test/components/dad-joke-html.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { html } from '@aegisjsproject/core/parsers/html.js';
import { EVENTS, AEGIS_EVENT_HANDLER_CLASS } from '@aegisjsproject/core/events.js';
import { EVENTS } from '@aegisjsproject/core/events.js';
import { registerCallback } from '@aegisjsproject/core/callbackRegistry.js';
import { updateIcon } from '../icons.js';

Expand All @@ -9,7 +9,7 @@ const template = html`<div part="container">
<div part="joke">
<slot name="joke">Loading...</slot>
</div>
<button type="button" id="update-btn" ${EVENTS.onClick}="${update}" class="btn btn-primary ${AEGIS_EVENT_HANDLER_CLASS}" part="btn" autofocus="">
<button type="button" id="update-btn" ${EVENTS.onClick}="${update}" class="btn btn-primary" part="btn" autofocus="">
<span>Get new Dad Joke</span>
</button>
</div>`;
Expand Down
22 changes: 11 additions & 11 deletions test/components/input-test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { AegisInput } from '@aegisjsproject/component/input.js';
import {
ValueMissingError, TypeMismatchError, TooLongError,
TooShortError,
} from '@aegisjsproject/component/errors.js';
import { TRIGGERS, SYMBOLS } from '@aegisjsproject/component/consts.js';
import { getInt, setInt } from '@aegisjsproject/component/attrs.js';
import { html } from '@aegisjsproject/core/parsers/html.js';
import { registerCallback } from '@aegisjsproject/core/callbackRegistry.js';
import { EVENTS, AEGIS_EVENT_HANDLER_CLASS } from '@aegisjsproject/core/events.js';
import { EVENTS } from '@aegisjsproject/core/events.js';

const inputHandler = registerCallback(
'test-input:input',
event => event.target.getRootNode().host.value = event.target.innerHTML.trim(),
);

const template = html`<div contenteditable="true" class="${AEGIS_EVENT_HANDLER_CLASS}" ${EVENTS.onInput}="${inputHandler}">Enter Text</div>`;
const template = html`<div contenteditable="true" ${EVENTS.onInput}="${inputHandler}"></div>`;

class TestInput extends AegisInput {
constructor() {
Expand All @@ -25,22 +21,26 @@ class TestInput extends AegisInput {
const anchor = shadow.getElementById('content');

if (typeof value !== 'string') {
throw new TypeMismatchError('Value must be a string.', { anchor });
throw new AegisInput.TypeMismatchError('Value must be a string.', { anchor });
} else if (this.required && value.length === 0) {
throw new ValueMissingError('Value is required.');
throw new AegisInput.ValueMissingError('Value is required.');
} else if (value.length < this.minLength) {
throw new TooShortError(`Value must be at least ${this.minLength} chars.`, { anchor });
throw new AegisInput.TooShortError(`Value must be at least ${this.minLength} chars.`, { anchor });
} else if (value.length > this.maxLength) {
throw new TooLongError(`Value must be fewer than ${this.maxLength} chars.`, { anchor });
throw new AegisInput.TooLongError(`Value must be fewer than ${this.maxLength} chars.`, { anchor });
} else {
const el = document.createElement('div');
el.setHTML(value);
return el.innerHTML;
}
}

async [SYMBOLS.render](type, { shadow, name, disabled }) {
async [SYMBOLS.render](type, { shadow, name, disabled }) {
switch(type) {
case TRIGGERS.constructed:
this.value = 'Enter Some Text!';
break;

case TRIGGERS.formReset:
shadow.getElementById('content').textContent = ' ';
break;
Expand Down
6 changes: 3 additions & 3 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<meta name="color-scheme" content="light dark" />
<meta http-equiv="Content-Security-Policy"
content="default-src 'none';
script-src 'self' https://unpkg.com/@shgysk8zer0/ https://unpkg.com/@aegisjsproject/ 'sha384-sEZHY5s3EVOtSOk2on2ohawJgDJCF2syHikVzJwj47nzEf5gsziUWZ2QMcmzli7C' 'nonce-fgdfg';
script-src 'self' https://unpkg.com/@shgysk8zer0/ https://unpkg.com/@aegisjsproject/ 'sha384-9r2le7Ki5JTkpwyzHCNRePSsl26daK+kcsjjkgwVx08e85iRuzIj97aZdO83g4Mg';
style-src 'self' blob: data:;
img-src 'self';
connect-src https://icanhazdadjoke.com https://unpkg.com/@shgysk8zer0/;
trusted-types empty#html empty#script sanitizer-raw#html default;
require-trusted-types-for 'script';" />
<title>Aegis Component Test</title>
<script type="importmap" nonce="fgdfg" integrity="sha384-sEZHY5s3EVOtSOk2on2ohawJgDJCF2syHikVzJwj47nzEf5gsziUWZ2QMcmzli7C">
<script type="importmap" integrity="sha384-9r2le7Ki5JTkpwyzHCNRePSsl26daK+kcsjjkgwVx08e85iRuzIj97aZdO83g4Mg">
{
"imports": {
"@aegisjsproject/core": "../node_modules/@aegisjsproject/core/core.js",
Expand All @@ -27,7 +27,7 @@
}
}
</script>
<script type="application/javascript" defer="" referrerpolicy="no-referrer" crossorigin="anonymous" integrity="sha384-Shkrmxly5RI9mCU8DQr6l4VLVJzjPzgx9KP/f5i7pEcl7ZUt0wHiAweGjbpjU2d5" src="https://unpkg.com/@shgysk8zer0/polyfills@0.3.1/all.min.js" fetchpriority="auto"></script>
<script type="application/javascript" defer="" referrerpolicy="no-referrer" crossorigin="anonymous" integrity="sha384-da+idpfYh2JjIk81mlgAuhkV0bSAuVW1TA9XLcjht0WOsiSQgwi+uzPM4a2t6A9i" src="https://unpkg.com/@shgysk8zer0/polyfills@0.3.2/all.min.js" fetchpriority="auto"></script>
<script type="application/javascript" defer="" referrerpolicy="no-referrer" crossorigin="anonymous" integrity="sha384-55L/wO9o0uIVTeubRIDQB4bewfNqyxrj4OXuxlW24NMEk+ioZwMHVw/tFV78mM+k" src="https://unpkg.com/@shgysk8zer0/kazoo@0.3.1/harden.js" fetchpriority="auto"></script>
<script type="module" referrerpolicy="no-referrer" src="./index.js"></script>
</head>
Expand Down
Loading

0 comments on commit e4de24c

Please sign in to comment.