Skip to content

Commit

Permalink
Fix types, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
simonihmig committed Mar 28, 2023
1 parent fdaa81e commit dea25b0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
13 changes: 10 additions & 3 deletions packages/ember-headless-form/src/components/headless-form.gts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import { assert, warn } from '@ember/debug';
import { hash, modifier } from '@ember/helper';
import { on } from '@ember/modifier';
import { action, set } from '@ember/object';
import { EnsureSafeComponentHelper } from '@embroider/util';

import { TrackedAsyncData } from 'ember-async-data';
import { modifier as elementModifier } from 'ember-modifier';
import { TrackedObject } from 'tracked-built-ins';

import HeadlessFormFieldComponent from '../-private/components/field';
import FieldComponent from '../-private/components/field';
import { mergeErrorRecord } from '../-private/utils';

import type {
Expand All @@ -25,10 +26,14 @@ import type {
UserData,
ValidationError,
} from '../-private/types';
import type { EnsureSafeComponentHelperFixed } from '@embroider/util';
import type { ModifierLike, WithBoundArgs } from '@glint/template';

type ValidateOn = 'change' | 'focusout' | 'submit' | 'input';

const ensureSafeComponent =
EnsureSafeComponentHelper as typeof EnsureSafeComponentHelperFixed;

export interface HeadlessFormComponentSignature<
DATA extends UserData,
SUBMISSION_VALUE
Expand Down Expand Up @@ -95,7 +100,7 @@ export interface HeadlessFormComponentSignature<
* Field component to define the fields of your form. It yields the further components for the form control, label and validation error.
*/
Field: WithBoundArgs<
typeof HeadlessFormFieldComponent<DATA>,
typeof FieldComponent<DATA>,
| 'data'
| 'set'
| 'errors'
Expand Down Expand Up @@ -190,6 +195,8 @@ export default class HeadlessFormComponent<
DATA extends UserData,
SUBMISSION_VALUE
> extends Component<HeadlessFormComponentSignature<DATA, SUBMISSION_VALUE>> {
FieldComponent = FieldComponent<DATA>;

// we cannot use (modifier "on") directly in the template due to https://github.com/emberjs/ember.js/issues/19869
on = on;
formElement?: HTMLFormElement;
Expand Down Expand Up @@ -520,7 +527,7 @@ export default class HeadlessFormComponent<
{{yield
(hash
Field=(component
HeadlessFormFieldComponent
(ensureSafeComponent this.FieldComponent)
data=this.internalData
set=this.set
errors=this.visibleErrors
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,33 @@ import Helper from '@ember/component/helper';
import { ComponentLike } from '@glint/template';

import type HeadlessFormRegistry from '../src/template-registry';
import type { EnsureSafeComponentHelperFixed } from '@embroider/util';
import type TemplateRegistry from '@glint/environment-ember-loose/registry';

// importing this directly from the published types (https://github.com/embroider-build/embroider/blob/main/packages/util/index.d.ts) does not work,
// see point 3 in Dan's comment here: https://github.com/typed-ember/glint/issues/518#issuecomment-1400306133
declare class EnsureSafeComponentHelper<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
C extends string | ComponentLike<any>
> extends Helper<{
Args: {
Positional: [component: C];
};
Return: C extends keyof TemplateRegistry
? TemplateRegistry[C]
: C extends string
? ComponentLike<unknown>
: C;
}> {}
declare module '@embroider/util' {
// importing this directly from the published types (https://github.com/embroider-build/embroider/blob/main/packages/util/index.d.ts) does not work,
// see point 3 in Dan's comment here: https://github.com/typed-ember/glint/issues/518#issuecomment-1400306133
export class EnsureSafeComponentHelperFixed<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
C extends string | ComponentLike<any>
> extends Helper<{
Args: {
Positional: [component: C];
};
Return: C extends keyof TemplateRegistry
? TemplateRegistry[C]
: C extends string
? ComponentLike<unknown>
: C;
}> {}
}

declare module '@glint/environment-ember-loose/registry' {
// Remove this once entries have been added! 👇
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export default interface Registry extends HeadlessFormRegistry {
// Add any registry entries from other addons here that your addon itself uses (in non-strict mode templates)
// See https://typed-ember.gitbook.io/glint/using-glint/ember/using-addons

'ensure-safe-component': typeof EnsureSafeComponentHelper;
'ensure-safe-component': typeof EnsureSafeComponentHelperFixed;

// eslint-disable-next-line @typescript-eslint/no-explicit-any -- effectively skipping type checks until https://github.com/typed-ember/glint/issues/410 is resolved
modifier: any;
Expand Down

0 comments on commit dea25b0

Please sign in to comment.