Skip to content

Commit

Permalink
refactor(model-form): first 5 components to Glimmer
Browse files Browse the repository at this point in the history
  • Loading branch information
guidojw committed Oct 15, 2023
1 parent b3189f7 commit 5c0cca7
Show file tree
Hide file tree
Showing 31 changed files with 513 additions and 245 deletions.
34 changes: 24 additions & 10 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

module.exports = {
root: true,
parser: 'babel-eslint',
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
legacyDecorators: true,
},
ecmaVersion: 'latest',
},
plugins: ['ember'],
plugins: ['ember', '@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:ember/recommended',
Expand All @@ -37,6 +33,15 @@ module.exports = {
],
},
overrides: [
// ts files
{
files: ['**/*.ts'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
],
rules: {},
},
// node files
{
files: [
Expand All @@ -52,9 +57,6 @@ module.exports = {
'./.stylelintrc.js',
'./.stylelintrc.order.js',
],
parserOptions: {
sourceType: 'script',
},
env: {
browser: false,
node: true,
Expand All @@ -72,5 +74,17 @@ module.exports = {
files: ['tests/**/*-test.{js,ts}'],
extends: ['plugin:qunit/recommended'],
},
// js files // TODO: remove when fully over to ts
{
files: ['**/*.js'],
parser: 'babel-eslint',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
legacyDecorators: true,
},
},
},
],
};
34 changes: 18 additions & 16 deletions app/components/model-form/checkbox-input.hbs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<div class='{{labelClass}}'></div>
<div class='{{inputWrapperClass}}'>
<div class='form-check'>
<Input
data-test-input={{property}}
@type='checkbox'
id={{inputIdentifier}}
@checked={{mut (get model property)}}
class={{concat 'form-check-input ' inputValidityClass}}
required={{required}}
disabled={{disabled}}
/>
<label class='form-check-label'>
{{label}}{{if required ' *'}}
{{form-control-feedback (get @model.errors property)}}
</label>
<div class='mb-3 {{if this.usesGrid 'row'}}'>
<div class='{{this.labelClass}}'></div>
<div class='{{this.inputWrapperClass}}'>
<div class='form-check'>
<Input
data-test-input={{@property}}
@type='checkbox'
id={{this.inputIdentifier}}
@checked={{mut (get @model @property)}}
class={{concat 'form-check-input ' this.inputValidityClass}}
required={{@required}}
disabled={{@disabled}}
/>
<label class='form-check-label'>
{{@label}}{{if @required ' *'}}
{{form-control-feedback (get @model.errors @property)}}
</label>
</div>
</div>
</div>
3 changes: 0 additions & 3 deletions app/components/model-form/checkbox-input.js

This file was deleted.

3 changes: 3 additions & 0 deletions app/components/model-form/checkbox-input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import TextInput from './text-input';

export default class CheckboxInput extends TextInput {}
36 changes: 19 additions & 17 deletions app/components/model-form/datetime-input.hbs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<label
class='col-sm-2 col-form-label form-label'
for={{inputIdentifier}}
>
{{label}}{{if required ' *'}}
</label>
<div class='col-sm-10'>
<Input::DatetimeInput
@inputId={{inputIdentifier}}
@dateValue={{mut (get model property)}}
@inputClass={{concat 'form-control ' inputValidityClass}}
@placeholder={{placeholder}}
@required={{required}}
@disabled={{disabled}}
@yearRange={{yearRange}}
/>
<div class='mb-3 {{if this.usesGrid 'row'}}'>
<label
class='col-sm-2 col-form-label form-label'
for={{this.inputIdentifier}}
>
{{@label}}{{if @required ' *'}}
</label>
<div class='col-sm-10'>
<Input::DatetimeInput
@inputId={{this.inputIdentifier}}
@dateValue={{mut (get @model @property)}}
@inputClass={{concat 'form-control ' this.inputValidityClass}}
@placeholder={{this.placeholder}}
@required={{@required}}
@disabled={{@disabled}}
@yearRange={{@yearRange}}
/>

{{form-control-feedback (get @model.errors property)}}
{{form-control-feedback (get @model.errors @property)}}
</div>
</div>
3 changes: 0 additions & 3 deletions app/components/model-form/datetime-input.js

This file was deleted.

3 changes: 3 additions & 0 deletions app/components/model-form/datetime-input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import TextInput from './text-input';

export default class DatetimeInput extends TextInput {}
95 changes: 61 additions & 34 deletions app/components/model-form/file-input.hbs
Original file line number Diff line number Diff line change
@@ -1,36 +1,63 @@
<label class='col-form-label form-label {{labelClass}}'>
{{label}}{{if required ' *'}}
</label>
<div class='{{inputWrapperClass}}'>
{{#let (file-queue name="fileUpload" onFileAdded=(action 'fileLoaded')) as |queue|}}
<label for="upload-file" class="form-label {{if not (get model property) "border rounded p-2 w-100"}}">
{{#if (get model property)}}
<img id={{inputIdentifier}} src={{mut (get model property)}} alt={{label}} role="button" tabindex="0"
aria-controls="upload-file" />
<br />
{{else}}
<span class='image-upload-text' role="button" tabindex="0" aria-controls="upload-file">
Sleep hier een nieuwe bestand naartoe of klik om een bestand te kiezen
</span>
{{/if}}
</label>
<input type="file" id="upload-file" class="" hidden accept={{validMimetypes}} {{queue.selectFile}}>
{{/let}}
{{#if fileToBig}}
<br />
<div class='alert alert-danger'>
Dit bestand is te groot om te uploaden, gebruik een bestand kleiner dan
{{maxFileSize}}MB
</div>
{{/if}}
{{#if extensionNotPermitted}}
<br />
<div class='alert alert-danger'>
Deze bestandsextensie wordt niet ondersteund. Geaccepteerde extensies
zijn:
<b>{{validExtensionsString}}</b>
</div>
{{/if}}
<div class='mb-3 {{if this.usesGrid "row"}}'>
<label class='col-form-label form-label {{this.labelClass}}'>
{{@label}}{{if @required ' *'}}
</label>
<div class={{this.inputWrapperClass}}>
{{#let
(file-queue name='fileUpload' onFileAdded=this.fileLoaded)
as |queue|
}}
<label
for='upload-file'
class='form-label
{{if not (get @model @property) "border rounded p-2 w-100"}}'
>
{{#if (get @model @property)}}
<img
id={{this.inputIdentifier}}
src={{mut (get @model @property)}}
alt={{@label}}
role='button'
tabindex='0'
aria-controls='upload-file'
/>
<br />
{{else}}
<span
class='image-upload-text'
role='button'
tabindex='0'
aria-controls='upload-file'
>
Sleep hier een nieuwe bestand naartoe of klik om een bestand te
kiezen
</span>
{{/if}}
</label>
<input
type='file'
id='upload-file'
hidden
accept={{this.validMimeTypes}}
{{queue.selectFile}}
/>
{{/let}}
{{#if this.fileTooBig}}
<br />
<div class='alert alert-danger'>
Dit bestand is te groot om te uploaden, gebruik een bestand kleiner dan
{{this.maxFilesize}}
</div>
{{/if}}
{{#if this.extensionNotPermitted}}
<br />
<div class='alert alert-danger'>
Deze bestandsextensie wordt niet ondersteund. Geaccepteerde extensies
zijn:
<b>{{join ', ' this.validExtensions}}</b>
</div>
{{/if}}

{{form-control-feedback (get @model.errors property)}}
{{form-control-feedback (get @model.errors @property)}}
</div>
</div>
38 changes: 0 additions & 38 deletions app/components/model-form/file-input.js

This file was deleted.

51 changes: 51 additions & 0 deletions app/components/model-form/file-input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import TextInput, { type TextInputSignature } from './text-input';
import ENV from 'amber-ui/config/environment';
import type { UploadFile } from 'ember-file-upload';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';

export interface FileInputSignature extends TextInputSignature {
validMimeTypes?: string[];
validExtensions?: string[];
loadedCallback: (
file: UploadFile,
data: Awaited<ReturnType<UploadFile['readAsDataURL']>>
) => void;
}

export default class FileInput extends TextInput<FileInputSignature> {
@tracked fileTooBig = false;
@tracked extensionNotPermitted = false;

maxFilesize = `${ENV.maxFilesize}MB`;

get validMimeTypes() {
return this.args.validMimeTypes ?? ['image/jpeg', 'image/png'];
}

get validExtensions() {
return this.args.validExtensions ?? ['jpeg', 'jpg', 'png'];
}

extensionInvalid(file: UploadFile): boolean {
const fileExtension = file.name.split('.').slice(-1)[0]?.toLowerCase();
if (typeof fileExtension === 'undefined') {
throw new Error('Could not parse file extension');
}
return !this.validExtensions.includes(fileExtension);
}

@action async fileLoaded(file: UploadFile): Promise<void> {
this.fileTooBig = false;
this.extensionNotPermitted = false;

// File.size is in bytes
if (file.size > ENV.maxFilesize * 1048576) {
this.fileTooBig = true;
} else if (this.extensionInvalid(file)) {
this.extensionNotPermitted = true;
} else {
this.args.loadedCallback(file, await file.readAsDataURL());
}
}
}
38 changes: 20 additions & 18 deletions app/components/model-form/select-input.hbs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<label
class='col-form-label form-label {{labelClass}}'
for={{inputIdentifier}}
>
{{label}}{{if required ' *'}}
</label>
<div class='{{inputWrapperClass}}'>
<Input::SelectInput
@data-test-input={{property}}
@class={{concat 'form-control ' inputValidityClass}}
@elementId={{inputIdentifier}}
@required={{required}}
@disabled={{disabled}}
@options={{options}}
@placeholder={{placeholder}}
@value={{mut (get model property)}}
/>
<div class='mb-3 {{if this.usesGrid 'row'}}'>
<label
class='col-form-label form-label {{this.labelClass}}'
for={{this.inputIdentifier}}
>
{{@label}}{{if @required ' *'}}
</label>
<div class='{{this.inputWrapperClass}}'>
<Input::SelectInput
@data-test-input={{@property}}
@class={{concat 'form-control ' this.inputValidityClass}}
@elementId={{this.inputIdentifier}}
@required={{@required}}
@disabled={{@disabled}}
@options={{@options}}
@placeholder={{this.placeholder}}
@value={{mut (get @model @property)}}
/>

{{form-control-feedback (get @model.errors property)}}
{{form-control-feedback (get @model.errors @property)}}
</div>
</div>
5 changes: 0 additions & 5 deletions app/components/model-form/select-input.js

This file was deleted.

3 changes: 3 additions & 0 deletions app/components/model-form/select-input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import TextInput from './text-input';

export default class SelectInput extends TextInput {}
Loading

0 comments on commit 5c0cca7

Please sign in to comment.