Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement basic component API #14

Merged
merged 35 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
4ec94bf
Add form component stub, setup Glint in test-app
simonihmig Jan 13, 2023
ea808d8
Add form test
simonihmig Jan 13, 2023
c1d3f69
form.field contextual component
simonihmig Jan 13, 2023
0d53e2c
field.label contextual component
simonihmig Jan 13, 2023
12a2eac
field.input contextual component
simonihmig Jan 13, 2023
3283734
Fix type linting in tests
simonihmig Jan 13, 2023
9daa0da
Support only Ember 4.8+
simonihmig Jan 16, 2023
62f5fbd
Bring back Ember 4.4 support
simonihmig Jan 16, 2023
c523a90
Use native unique-id helper
simonihmig Jan 16, 2023
bed86e3
Remove classic scenario
simonihmig Jan 16, 2023
3e93824
Use ensure-safe-component for strict Embroider compatibility
simonihmig Jan 16, 2023
07edccb
Add ensure-safe-component Glint type
simonihmig Jan 16, 2023
9b3eafe
Fix Glint errors
simonihmig Jan 16, 2023
9278915
Pass data down
simonihmig Jan 16, 2023
ed1f6c1
Fix Embroider tests
simonihmig Jan 16, 2023
38718aa
Pass value as string to input, fixes Glint
simonihmig Jan 16, 2023
b478d4d
Switch test-app to template-imports
simonihmig Jan 16, 2023
1f5cadd
Add prettier-plugin-ember-template-tag
simonihmig Jan 16, 2023
760fe2d
Add reactivity test
simonihmig Jan 16, 2023
87f43ae
submit handler
simonihmig Jan 17, 2023
865c834
yield value
simonihmig Jan 17, 2023
0ba351f
yield field id
simonihmig Jan 17, 2023
0f3c84d
yield setValue
simonihmig Jan 17, 2023
81979df
Fix broken Glint declarations by rolling back public entrypoint changes
simonihmig Jan 17, 2023
a418ef6
Fix Glint types
simonihmig Jan 17, 2023
b63d5d2
Fix ESLint issues
simonihmig Jan 18, 2023
19729e7
Disable no-implicit-this template-lint rule
simonihmig Jan 18, 2023
093ad97
Tweak `@data` types, add Glint tests
simonihmig Jan 18, 2023
eaee023
Update ember-sinon-qunit, cleanup
simonihmig Jan 18, 2023
c094820
Tweak test message
simonihmig Jan 18, 2023
e77cba1
Add comment to VSCode prettier workaround
simonihmig Jan 18, 2023
cb2b675
Use `@nullvoxpopuli/eslint-configs` for addon
simonihmig Jan 18, 2023
089fce2
Update to stable Ember 4.10.0
simonihmig Jan 19, 2023
568199c
Try running Embroider tests with 1.x
simonihmig Jan 19, 2023
8cc04a0
Remove ember-source from addon's devDependencies to "fix" Embroider
simonihmig Jan 20, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,11 @@ jobs:
fail-fast: false
matrix:
try-scenario:
- ember-lts-3.28
Copy link
Contributor

@nicolechung nicolechung Jan 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question, just curious: We aren't supporting 3.28 anymore?

Copy link
Contributor Author

@simonihmig simonihmig Jan 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this is the very start of everything, so we never supported anything before! 😆
But you are right, we are starting by not supporting Ember 3.28, at least that's the outcome of some back and forth between Preston and me. But we can revisit this decision, if you think there is a good reason to still support Ember 3.28.

Having Ember 3.28 support is good for existing addons, as that supports apps that still have trouble upgrading to 4. And it's quite unfortunate if addons don't have this overlap, and maybe only the newer version of an addon supports Ember 4, but not Ember 3.28. In this way users are forces to upgrade Ember and the addon at the same time.

But none of this applies here, as this is a green field project, and we don't have any existing Ember 3.28 users, so we can just support what is reasonable for us I think...

- ember-lts-4.4
- ember-lts-4.8
- ember-release
- ember-beta
- ember-canary
- ember-classic
- embroider-safe
- embroider-optimized

Expand Down
2 changes: 2 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

module.exports = {
plugins: ['prettier-plugin-ember-template-tag'],
singleQuote: true,
templateSingleQuote: false,
};
27 changes: 8 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
ember-headless-form
==============================================================================
# ember-headless-form

[Short description of the addon.]

## Compatibility

Compatibility
------------------------------------------------------------------------------
- Ember.js v4.8 or above
- Embroider or ember-auto-import v2

* Ember.js v3.28 or above
* Embroider or ember-auto-import v2


Installation
------------------------------------------------------------------------------
## Installation

```
ember install ember-headless-form
```


Usage
------------------------------------------------------------------------------
## Usage

[Longer description of how to use the addon in apps.]


Contributing
------------------------------------------------------------------------------
## Contributing

See the [Contributing](CONTRIBUTING.md) guide for details.


License
------------------------------------------------------------------------------
## License

This project is licensed under the [MIT License](LICENSE.md).
52 changes: 4 additions & 48 deletions ember-headless-form/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,6 @@
'use strict';

module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 'latest',
},
plugins: ['ember'],
extends: [
'eslint:recommended',
'plugin:ember/recommended',
'plugin:prettier/recommended',
],
env: {
browser: true,
},
rules: {},
overrides: [
// ts files
{
files: ['**/*.ts'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
],
rules: {
// Add any custom rules here
},
},
// node files
{
files: [
'./.eslintrc.js',
'./.prettierrc.js',
'./.template-lintrc.js',
'./addon-main.js',
],
parserOptions: {
sourceType: 'script',
},
env: {
browser: false,
node: true,
},
plugins: ['node'],
extends: ['plugin:node/recommended'],
},
],
};
const { configs } = require('@nullvoxpopuli/eslint-configs');

// accommodates: JS, TS, App, and Addon
module.exports = configs.ember();
1 change: 1 addition & 0 deletions ember-headless-form/addon-main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

const { addonV1Shim } = require('@embroider/addon-shim');

module.exports = addonV1Shim(__dirname);
43 changes: 27 additions & 16 deletions ember-headless-form/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,45 +26,54 @@
"prepack": "rollup --config"
},
"dependencies": {
"@embroider/addon-shim": "^1.0.0"
"@babel/runtime": "^7.20.7",
"@embroider/addon-shim": "^1.0.0",
"@embroider/util": "^1.9.0",
"tracked-built-ins": "^3.1.0"
},
"peerDependencies": {
"@glimmer/component": "^1.1.2"
},
"devDependencies": {
"@babel/core": "^7.17.0",
"@babel/preset-typescript": "^7.18.6",
"@babel/plugin-proposal-class-properties": "^7.16.7",
"@babel/plugin-proposal-decorators": "^7.17.0",
"@babel/plugin-syntax-decorators": "^7.17.0",
"@babel/preset-typescript": "^7.18.6",
"@embroider/addon-dev": "^3.0.0",
"@glimmer/component": "^1.1.2",
"@glint/core": "^0.9.7",
"@glint/environment-ember-loose": "^0.9.7",
"@glint/template": "^0.9.7",
"@nullvoxpopuli/eslint-configs": "^3.0.2",
"@tsconfig/ember": "^1.0.0",
"@types/ember": "^4.0.0",
"@types/ember__object": "^4.0.0",
"@types/ember__service": "^4.0.0",
"@types/ember__application": "^4.0.0",
"@types/ember__array": "^4.0.0",
"@types/ember__component": "^4.0.0",
"@types/ember__controller": "^4.0.0",
"@types/ember__string": "^3.16.0",
"@types/ember__template": "^4.0.0",
"@types/ember__polyfills": "^4.0.0",
"@types/ember__utils": "^4.0.0",
"@types/ember__runloop": "^4.0.0",
"@types/ember__debug": "^4.0.0",
"@types/ember__engine": "^4.0.0",
"@types/ember__application": "^4.0.0",
"@types/ember__test": "^4.0.0",
"@types/ember__array": "^4.0.0",
"@types/ember__error": "^4.0.0",
"@types/ember__component": "^4.0.0",
"@types/ember__object": "^4.0.0",
"@types/ember__polyfills": "^4.0.0",
"@types/ember__routing": "^4.0.0",
"@types/ember__runloop": "^4.0.0",
"@types/ember__service": "^4.0.0",
"@types/ember__string": "^3.16.0",
"@types/ember__template": "^4.0.0",
"@types/ember__test": "^4.0.0",
"@types/ember__utils": "^4.0.0",
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
"concurrently": "^7.2.1",
"ember-template-lint": "^4.0.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-ember": "^10.5.8",
"eslint-plugin-ember": "^11.4.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"prettier": "^2.5.1",
"prettier": "^2.8.3",
"rollup": "^2.67.0",
"rollup-plugin-copy": "^3.4.0",
"rollup-plugin-ts": "^3.0.2",
Expand All @@ -80,7 +89,9 @@
"version": 2,
"type": "addon",
"main": "addon-main.js",
"app-js": {}
"app-js": {
"./components/headless-form.js": "./dist/_app_/components/headless-form.js"
}
},
"exports": {
".": "./dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion ember-headless-form/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default {
// These are the modules that should get reexported into the traditional
// "app" tree. Things in here should also be in publicEntrypoints above, but
// not everything in publicEntrypoints necessarily needs to go here.
addon.appReexports(['components/**/*.js']),
addon.appReexports(['components/*.js']),
NullVoxPopuli marked this conversation as resolved.
Show resolved Hide resolved

// Follow the V2 Addon rules about dependencies. Your code can import from
// `dependencies` and `peerDependencies` as well as standard Ember-provided
Expand Down
7 changes: 7 additions & 0 deletions ember-headless-form/src/components/-private/control/input.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<input
type={{@type}}
value={{this.valueAsString}}
id={{@fieldId}}
...attributes
{{on 'input' this.handleInput}}
/>
62 changes: 62 additions & 0 deletions ember-headless-form/src/components/-private/control/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Component from '@glimmer/component';
import { assert } from '@ember/debug';
import { action } from '@ember/object';

// Possible values for the input type, see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#input_types
// for the sake ok completeness, we list all here, with some commented out that are better handled elsewhere, or not at all...
export type InputType =
simonihmig marked this conversation as resolved.
Show resolved Hide resolved
// | 'button' - not useful as a control component
// | 'checkbox' - handled separately, for handling `checked` correctly and operating with true boolean values
| 'color'
| 'date'
| 'datetime-local'
| 'email'
// | 'file' - would need special handling
| 'hidden'
// | 'image' - not useful as a control component
| 'month'
| 'number'
| 'password'
// | 'radio' - handled separately, for handling groups or radio buttons
| 'range'
// | 'reset' - would need special handling
| 'search'
// | 'submit' - not useful as a control component
| 'tel'
| 'text'
| 'time'
| 'url'
| 'week';

export interface HeadlessFormControlInputComponentSignature<VALUE> {
Element: HTMLInputElement;
Args: {
value: VALUE;
type?: InputType;
fieldId: string;
setValue: (value: VALUE) => void;
};
NullVoxPopuli marked this conversation as resolved.
Show resolved Hide resolved
}

export default class HeadlessFormControlInputComponent<VALUE> extends Component<
HeadlessFormControlInputComponentSignature<VALUE>
> {
get type(): InputType {
return this.args.type ?? 'text';
}

get valueAsString(): string {
assert(
`input can only handle string values, but you passed ${typeof this.args
.value}`,
typeof this.args.value === 'string'
);

return this.args.value;
}

@action
handleInput(e: Event | InputEvent): void {
this.args.setValue((e.target as HTMLInputElement).value as VALUE);
}
}
16 changes: 16 additions & 0 deletions ember-headless-form/src/components/-private/field.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{#let (unique-id) (fn @set @name) as |uuid setValue|}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: how come these field files are just index files inside of the field folder?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean here with index files?

{{yield
(hash
label=(component (ensure-safe-component this.LabelComponent) fieldId=uuid)
input=(component
(ensure-safe-component this.InputComponent)
fieldId=uuid
value=this.value
setValue=setValue
)
value=this.value
id=uuid
setValue=setValue
)
}}
{{/let}}
53 changes: 53 additions & 0 deletions ember-headless-form/src/components/-private/field.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import Component from '@glimmer/component';

import InputComponent from './control/input';
import LabelComponent from './field/label';

import type { HeadlessFormData } from '../headless-form';
import type {
HeadlessFormControlInputComponentSignature,
} from './control/input';
import type {
HeadlessFormFieldLabelComponentSignature,
} from './field/label';
import type { ComponentLike,WithBoundArgs } from '@glint/template';

export interface HeadlessFormFieldComponentSignature<
DATA extends HeadlessFormData,
KEY extends keyof DATA = keyof DATA
> {
Args: {
data: DATA;
name: KEY;
set: (key: KEY, value: DATA[KEY]) => void;
};
Blocks: {
default: [
{
label: WithBoundArgs<typeof LabelComponent, 'fieldId'>;
input: WithBoundArgs<
typeof InputComponent<DATA[KEY]>,
'fieldId' | 'value' | 'setValue'
>;
value: DATA[KEY];
id: string;
setValue: (value: DATA[KEY]) => void;
}
];
};
}

export default class HeadlessFormFieldComponent<
DATA extends HeadlessFormData,
KEY extends keyof DATA = keyof DATA
> extends Component<HeadlessFormFieldComponentSignature<DATA, KEY>> {
LabelComponent: ComponentLike<HeadlessFormFieldLabelComponentSignature> =
LabelComponent;
InputComponent: ComponentLike<
HeadlessFormControlInputComponentSignature<DATA[KEY]>
> = InputComponent;

get value(): DATA[KEY] {
return this.args.data[this.args.name];
}
}
3 changes: 3 additions & 0 deletions ember-headless-form/src/components/-private/field/label.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<label for={{@fieldId}} ...attributes>
{{yield}}
</label>
13 changes: 13 additions & 0 deletions ember-headless-form/src/components/-private/field/label.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import templateOnlyComponent from '@ember/component/template-only';

export interface HeadlessFormFieldLabelComponentSignature {
Element: HTMLLabelElement;
Args: {
fieldId: string;
};
Blocks: {
default: [];
};
}

export default templateOnlyComponent<HeadlessFormFieldLabelComponentSignature>();
11 changes: 11 additions & 0 deletions ember-headless-form/src/components/headless-form.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<form ...attributes {{on 'submit' this.onSubmit}}>
{{yield
(hash
field=(component
(ensure-safe-component this.FieldComponent)
data=this.internalData
set=this.set
)
)
}}
</form>
Loading