-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Baylee Swenson
committed
Jul 24, 2019
1 parent
37f6e85
commit 9ed3151
Showing
16 changed files
with
229 additions
and
1 deletion.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
lib/handbook/addon/docs/components/form-controls/controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { computed } from '@ember-decorators/object'; | ||
import { reads } from '@ember-decorators/object/computed'; | ||
import { service } from '@ember-decorators/service'; | ||
import Controller from '@ember/controller'; | ||
import { action } from '@ember/object'; | ||
// import Changeset from 'ember-changeset'; | ||
// import lookupValidator from 'ember-changeset-validations'; | ||
import { ChangesetDef } from 'ember-changeset/types'; | ||
import DS from 'ember-data'; | ||
import Toast from 'ember-toastr/services/toast'; | ||
|
||
import Node from 'ember-osf-web/models/node'; | ||
|
||
import { nodeValidation } from './validation'; | ||
|
||
export default class FormController extends Controller { | ||
@service store!: DS.Store; | ||
@service toast!: Toast; | ||
|
||
@reads('model.taskInstance.value') | ||
existingNode?: Node; | ||
createDirt = false; | ||
editDirt = false; | ||
validation = nodeValidation; | ||
node = Node; | ||
changeset!: ChangesetDef; | ||
|
||
@computed('createDirt', 'editDirt') | ||
get isDirty() { | ||
return this.createDirt || this.editDirt; | ||
} | ||
|
||
@action | ||
submit() { | ||
this.changeset.save({}); | ||
this.toast.success('Saved!'); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
lib/handbook/addon/docs/components/form-controls/demo-default/template.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<DocsDemo as |demo|> | ||
<demo.example @name='form-controls.demo-default.hbs'> | ||
<form {{action this.submit on='submit'}}> | ||
<FormControls | ||
@changeset={{changeset this.node this.validation}} | ||
as |form| | ||
> | ||
<form.text | ||
@label='Title' | ||
@valuePath='title' | ||
/> | ||
<form.text | ||
@label='Description' | ||
@valuePath='description' | ||
/> | ||
</FormControls> | ||
<OsfButton | ||
@type='primary' | ||
@buttonType='submit' | ||
> | ||
Submit | ||
</OsfButton> | ||
</form> | ||
</demo.example> | ||
|
||
<demo.snippet @name='form-controls.demo-default.hbs' /> | ||
</DocsDemo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { computed } from '@ember-decorators/object'; | ||
import Route from '@ember/routing/route'; | ||
import Changeset from 'ember-changeset'; | ||
import { task } from 'ember-concurrency'; | ||
import ConfirmationMixin from 'ember-onbeforeunload/mixins/confirmation'; | ||
|
||
import FormController from './controller'; | ||
|
||
export default class FormControls extends Route.extend(ConfirmationMixin, { | ||
modelTask: task(function *(this: FormControls) { | ||
return yield this.store.findRecord('node', 'extng'); | ||
}), | ||
}) { | ||
model() { | ||
return { | ||
taskInstance: this.modelTask.perform(), | ||
}; | ||
} | ||
|
||
setupController(controller: FormController) { | ||
const changeset = new Changeset(controller.node, controller.validation); | ||
controller.set('changeset', changeset); | ||
} | ||
|
||
@computed('controller.isDirty') | ||
get isPageDirty() { | ||
const controller = this.controller as FormController; | ||
return () => controller.get('isDirty'); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
lib/handbook/addon/docs/components/form-controls/template.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# validated-form-controls | ||
Takes a changeset and yields validated form components | ||
|
||
### Params | ||
The only param that `<FormControls />` takes is a changeset for the current form being created | ||
|
||
### Yielded hash | ||
When invoked in a form, `<ValidatedFormControls />` yields a hash with the following keys: | ||
|
||
* Several `validated-model-form/*` components with common arguments (`model`, `changeset`, `shouldShowMessages`, `disabled`) already bound: | ||
* `checkbox` | ||
* `checkboxes` | ||
* `date` | ||
* `recaptcha` | ||
* `text` | ||
* `textarea` | ||
|
||
## Demo: Default | ||
{{docs/components/form-controls/demo-default | ||
submit=(action this.submit) | ||
node=this.node | ||
validation=this.validation | ||
}} |
9 changes: 9 additions & 0 deletions
9
lib/handbook/addon/docs/components/form-controls/validation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ValidationObject } from 'ember-changeset-validations'; | ||
import { validatePresence } from 'ember-changeset-validations/validators'; | ||
import NodeModel from 'ember-osf-web/models/node'; | ||
|
||
export const nodeValidation: ValidationObject<NodeModel> = { | ||
title: [ | ||
validatePresence(true), | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
lib/osf-components/addon/components/form-controls/component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { layout } from '@ember-decorators/component'; | ||
import Component from '@ember/component'; | ||
|
||
import defaultTo from 'ember-osf-web/utils/default-to'; | ||
|
||
import template from './template'; | ||
|
||
@layout(template) | ||
export default class FormControls extends Component { | ||
// Optional arguments | ||
disabled: boolean = defaultTo(this.disabled, false); | ||
shouldShowMessages: boolean = false; | ||
} |
Empty file.
44 changes: 44 additions & 0 deletions
44
lib/osf-components/addon/components/form-controls/template.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{{yield (hash | ||
checkbox=( | ||
component | ||
'validated-input/checkbox' | ||
changeset=this.changeset | ||
shouldShowMessages=this.shouldShowMessages | ||
disabled=this.disabled | ||
) | ||
date=( | ||
component | ||
'validated-input/date' | ||
changeset=this.changeset | ||
shouldShowMessages=this.shouldShowMessages | ||
disabled=this.disabled | ||
) | ||
text=( | ||
component | ||
'validated-input/text' | ||
changeset=this.changeset | ||
shouldShowMessages=this.shouldShowMessages | ||
disabled=this.disabled | ||
) | ||
textarea=( | ||
component | ||
'validated-input/textarea' | ||
changeset=@changeset | ||
shouldShowMessages=this.shouldShowMessages | ||
disabled=this.disabled | ||
) | ||
select=( | ||
component | ||
'validated-input/power-select' | ||
changeset=this.changeset | ||
shouldShowMessages=this.shouldShowMessages | ||
disabled=this.disabled | ||
) | ||
custom=( | ||
component | ||
'validated-input/custom' | ||
changeset=this.changeset | ||
shouldShowMessages=this.shouldShowMessages | ||
disabled=this.disabled | ||
) | ||
)}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from 'osf-components/components/form-controls/component'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ValidatorFunc } from 'ember-changeset/types'; | ||
import { validator } from 'ember-validations'; | ||
|
||
export type ValidatorFunction = (key: string, result: any) => string | string[] | true; | ||
|
||
export function lookupValidator(validator: validatorObject): ValidatorFunc; | ||
|
||
// FIXME: Below doesn't actually work, but setting it aside for now to get actual | ||
// functionality implemented. James is upgrading types a bit, and the following | ||
// will make more sense after. | ||
|
||
export type ValidationObject<M> = { | ||
[F in keyof M]?: any; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
declare module 'ember-changeset-validations/validators' { | ||
import { ValidatorFunction } from 'ember-changeset-validations'; | ||
import { ConfirmationOptions } from 'ember-validators/validators/confirmation'; | ||
import { ExclusionOptions } from 'ember-validators/validators/exclusion'; | ||
import { InclusionOptions } from 'ember-validators/validators/inclusion'; | ||
import { LengthOptions } from 'ember-validators/validators/length'; | ||
import { NumberOptions } from 'ember-validators/validators/number'; | ||
import { PresenceOptions } from 'ember-validators/validators/presence'; | ||
|
||
export function validatePresence(options: PresenceOptions | boolean): ValidatorFunction; | ||
export function validateLength(options: LengthOptions): ValidatorFunction; | ||
export function validateNumber(options: NumberOptions): ValidatorFunction; | ||
export function validateInclusion(options: InclusionOptions): ValidatorFunction; | ||
export function validateExclusion(options: ExclusionOptions): ValidatorFunction; | ||
export function validateFormat(options: FormatOptions): ValidatorFunction; | ||
export function validateConfirmation(options: ConfirmationOptions): ValidatorFunction; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ValidationObject } from 'ember-changeset-validations'; | ||
|
||
declare module '*/validations' { | ||
export const UserSettingValidation: ValidationObject<UserSettingModel>; | ||
} |