-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add basic form elements to forms pkg
- Loading branch information
1 parent
296aead
commit 9986d87
Showing
14 changed files
with
503 additions
and
22 deletions.
There are no files selected for viewing
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
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,61 @@ | ||
import Component from '@glimmer/component'; | ||
import { action } from '@ember/object'; | ||
import { on } from '@ember/modifier'; | ||
import { useStyles } from '@frontile/theme'; | ||
|
||
interface CheckboxSignature { | ||
Args: { | ||
id?: string; | ||
checked?: boolean; | ||
name?: string; | ||
size?: 'sm' | 'md' | 'lg'; | ||
class?: string; | ||
|
||
/* | ||
* Callback when onchange is triggered | ||
*/ | ||
onChange?: (value: boolean, event: Event) => void; | ||
}; | ||
Element: HTMLInputElement; | ||
} | ||
|
||
class Checkbox extends Component<CheckboxSignature> { | ||
get isChecked(): boolean { | ||
return !!this.args.checked; | ||
} | ||
|
||
@action handleChange(event: Event): void { | ||
event.preventDefault(); | ||
|
||
const value = !this.args.checked; | ||
|
||
if (typeof this.args.onChange === 'function') { | ||
this.args.onChange(value, event); | ||
} | ||
} | ||
|
||
get classes() { | ||
const { checkbox } = useStyles(); | ||
|
||
return checkbox({ | ||
size: this.args.size, | ||
class: this.args.class | ||
}); | ||
} | ||
|
||
<template> | ||
<input | ||
{{on "change" this.handleChange}} | ||
id={{@id}} | ||
name={{@name}} | ||
checked={{this.isChecked}} | ||
type="checkbox" | ||
class={{this.classes}} | ||
data-component="checkbox" | ||
...attributes | ||
/> | ||
</template> | ||
} | ||
|
||
export { Checkbox, type CheckboxSignature }; | ||
export default Checkbox; |
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,43 @@ | ||
import Component from '@glimmer/component'; | ||
import { useStyles } from '@frontile/theme'; | ||
|
||
interface FormDescriptionSignature { | ||
Args: { | ||
id?: string; | ||
/* | ||
* @defaultValue 'md' | ||
*/ | ||
size?: 'sm' | 'md' | 'lg'; | ||
|
||
class?: string; | ||
}; | ||
Element: HTMLDivElement; | ||
Blocks: { | ||
default: []; | ||
}; | ||
} | ||
|
||
class FormDescription extends Component<FormDescriptionSignature> { | ||
get classes() { | ||
const { formDescription } = useStyles(); | ||
|
||
return formDescription({ | ||
size: this.args.size || 'md', | ||
class: this.args.class | ||
}); | ||
} | ||
|
||
<template> | ||
<div | ||
id={{@id}} | ||
class={{this.classes}} | ||
data-component="form-description" | ||
...attributes | ||
> | ||
{{yield}} | ||
</div> | ||
</template> | ||
} | ||
|
||
export { FormDescription, type FormDescriptionSignature }; | ||
export default FormDescription; |
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,73 @@ | ||
import Component from '@glimmer/component'; | ||
import { useStyles } from '@frontile/theme'; | ||
|
||
interface FormFeedbackSignature { | ||
Args: { | ||
id?: string; | ||
/* | ||
* The intent of the feedback | ||
* @defaultValue 'danger' | ||
*/ | ||
intent?: 'primary' | 'success' | 'danger' | 'warning'; | ||
|
||
/* | ||
* A list of messages or a single message string | ||
*/ | ||
messages?: string[] | string; | ||
/* | ||
* @defaultValue 'md' | ||
*/ | ||
size?: 'sm' | 'md' | 'lg'; | ||
class?: string; | ||
}; | ||
Element: HTMLDivElement; | ||
Blocks: { | ||
default: []; | ||
}; | ||
} | ||
|
||
class FormFeedback extends Component<FormFeedbackSignature> { | ||
get isError(): boolean { | ||
return ( | ||
typeof this.args.messages !== 'undefined' || | ||
this.args.intent === 'danger' || | ||
typeof this.args.intent === 'undefined' | ||
); | ||
} | ||
|
||
get classes() { | ||
const { formFeedback } = useStyles(); | ||
|
||
return formFeedback({ | ||
size: this.args.size || 'md', | ||
intent: this.args.intent || 'danger', | ||
class: this.args.class | ||
}); | ||
} | ||
|
||
get messageText(): string { | ||
if (!this.args.messages) return ''; | ||
|
||
if (typeof this.args.messages === 'string') { | ||
return this.args.messages; | ||
} else { | ||
return this.args.messages.join('; '); | ||
} | ||
} | ||
|
||
<template> | ||
<div | ||
id={{@id}} | ||
class={{this.classes}} | ||
data-component="form-feedback" | ||
aria-live={{if this.isError "assertive" "polite"}} | ||
...attributes | ||
> | ||
{{this.messageText}} | ||
{{yield}} | ||
</div> | ||
</template> | ||
} | ||
|
||
export { FormFeedback, type FormFeedbackSignature }; | ||
export default FormFeedback; |
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,73 @@ | ||
import Component from '@glimmer/component'; | ||
import { action } from '@ember/object'; | ||
import { on } from '@ember/modifier'; | ||
import { useStyles } from '@frontile/theme'; | ||
|
||
interface InputSignature { | ||
Args: { | ||
id?: string; | ||
type?: string; | ||
size?: 'sm' | 'md' | 'lg'; | ||
class?: string; | ||
value?: string; | ||
|
||
// Callback when oninput is triggered | ||
onInput?: (value: string, event: InputEvent) => void; | ||
|
||
// Callback when onchange is triggered | ||
onChange?: (value: string, event: InputEvent) => void; | ||
}; | ||
Element: HTMLInputElement; | ||
} | ||
|
||
class Input extends Component<InputSignature> { | ||
get type(): string { | ||
if (typeof this.args.type === 'string') { | ||
return this.args.type; | ||
} | ||
return 'text'; | ||
} | ||
|
||
@action handleOnInput(event: Event): void { | ||
if (typeof this.args.onInput === 'function') { | ||
this.args.onInput( | ||
(event.target as HTMLInputElement).value, | ||
event as InputEvent | ||
); | ||
} | ||
} | ||
|
||
@action handleOnChange(event: Event): void { | ||
if (typeof this.args.onChange === 'function') { | ||
this.args.onChange( | ||
(event.target as HTMLInputElement).value, | ||
event as InputEvent | ||
); | ||
} | ||
} | ||
|
||
get classes() { | ||
const { input } = useStyles(); | ||
|
||
return input({ | ||
size: this.args.size, | ||
class: this.args.class | ||
}); | ||
} | ||
|
||
<template> | ||
<input | ||
{{on "input" this.handleOnInput}} | ||
{{on "change" this.handleOnChange}} | ||
id={{@id}} | ||
value={{@value}} | ||
type={{this.type}} | ||
class={{this.classes}} | ||
data-component="input" | ||
...attributes | ||
/> | ||
</template> | ||
} | ||
|
||
export { Input, type InputSignature }; | ||
export default Input; |
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,58 @@ | ||
import Component from '@glimmer/component'; | ||
import { useStyles } from '@frontile/theme'; | ||
|
||
interface LabelSignature { | ||
Args: { | ||
/** | ||
* The 'for' attribute of a <label>. | ||
*/ | ||
for?: string; | ||
|
||
/* | ||
* @defaultValue 'md' | ||
*/ | ||
size?: 'sm' | 'md' | 'lg'; | ||
|
||
/* | ||
* Whether the field is required or not, if true, an asterisk will be added to the label. | ||
* @defaultValue false | ||
*/ | ||
isRequired?: boolean; | ||
class?: string; | ||
}; | ||
Element: HTMLLabelElement; | ||
Blocks: { | ||
default: []; | ||
}; | ||
} | ||
|
||
class Label extends Component<LabelSignature> { | ||
get classes() { | ||
const { label } = useStyles(); | ||
const { base, asterisk } = label({ | ||
size: this.args.size || 'md' | ||
}); | ||
|
||
return { | ||
base: base({ class: this.args.class }), | ||
asterisk: asterisk() | ||
}; | ||
} | ||
|
||
<template> | ||
<label | ||
for={{@for}} | ||
class={{this.classes.base}} | ||
data-component="label" | ||
...attributes | ||
> | ||
{{yield}} | ||
{{#if @isRequired}} | ||
<span class={{this.classes.asterisk}}>*</span> | ||
{{/if}} | ||
</label> | ||
</template> | ||
} | ||
|
||
export { Label, type LabelSignature }; | ||
export default Label; |
Oops, something went wrong.