-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from PhlexUI/ch/add-form-component
Adding form Component
- Loading branch information
Showing
25 changed files
with
205 additions
and
205 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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,81 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
export default class extends Controller { | ||
static targets = ['input', 'message'] | ||
static values = { shouldValidate: false } | ||
|
||
|
||
connect() { | ||
if (this.messageTarget.textContent) { | ||
this.shouldValidateValue = true; | ||
} | ||
} | ||
|
||
onInvalid(error) { | ||
error.preventDefault(); | ||
|
||
this.shouldValidateValue = true; | ||
this.#setErrorMessage(); | ||
} | ||
|
||
onInput() { | ||
this.#setErrorMessage(); | ||
} | ||
|
||
onChange() { | ||
this.#setErrorMessage(); | ||
} | ||
|
||
#setErrorMessage() { | ||
if (!this.shouldValidateValue) return; | ||
|
||
if (this.inputTarget.validity.valid) { | ||
this.messageTarget.textContent = ''; | ||
} else { | ||
this.messageTarget.textContent = this.#getValidationMessage(); | ||
} | ||
} | ||
|
||
#getValidationMessage() { | ||
const input = this.inputTarget; | ||
const defaultMessage = this.inputTarget.validationMessage; | ||
|
||
if (input.validity.valueMissing) { | ||
return input.dataset.valueMissing || defaultMessage; | ||
} | ||
|
||
if (input.validity.badInput) { | ||
return input.dataset.badInput || defaultMessage; | ||
} | ||
|
||
if (input.validity.patternMismatch) { | ||
return input.dataset.patternMismatch || defaultMessage; | ||
} | ||
|
||
if (input.validity.rangeOverflow) { | ||
return input.dataset.rangeOverflow || defaultMessage; | ||
} | ||
|
||
if (input.validity.rangeUnderflow) { | ||
return input.dataset.rangeUnderflow || defaultMessage; | ||
} | ||
|
||
if (input.validity.stepMismatch) { | ||
return input.dataset.stepMismatch || defaultMessage; | ||
} | ||
|
||
if (input.validity.tooLong) { | ||
return input.dataset.tooLong || defaultMessage; | ||
} | ||
|
||
if (input.validity.tooShort) { | ||
return input.dataset.tooShort || defaultMessage; | ||
} | ||
|
||
if (input.validity.typeMismatch) { | ||
return input.dataset.typeMismatch || defaultMessage; | ||
} | ||
|
||
return defaultMessage; | ||
} | ||
} |
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,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module RBUI | ||
class FormFieldError < Base | ||
def view_template(&) | ||
p(**attrs, &) | ||
end | ||
|
||
private | ||
|
||
def default_attrs | ||
{ | ||
data: { | ||
rbui__form_field_target: "message" | ||
}, | ||
class: "text-sm font-medium text-destructive" | ||
} | ||
end | ||
end | ||
end |
8 changes: 3 additions & 5 deletions
8
lib/phlex_ui/input_error.rb → lib/rbui/form/form_field_hint.rb
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 |
---|---|---|
@@ -1,17 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module PhlexUI | ||
class InputError < Base | ||
module RBUI | ||
class FormFieldHint < Base | ||
def view_template(&) | ||
p(**attrs, &) | ||
end | ||
|
||
private | ||
|
||
def default_attrs | ||
{ | ||
class: "mt-2 text-sm text-destructive" | ||
} | ||
{class: "text-sm text-muted-foreground"} | ||
end | ||
end | ||
end |
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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module RBUI | ||
class FormFieldLabel < Base | ||
def view_template(&) | ||
label(**attrs, &) | ||
end | ||
|
||
private | ||
|
||
def default_attrs | ||
{class: "text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"} | ||
end | ||
end | ||
end |
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,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module RBUI | ||
class Input < Base | ||
def initialize(type: :string, **attrs) | ||
@type = type.to_sym | ||
super(**attrs) | ||
end | ||
|
||
def view_template | ||
input(type: @type, **attrs) | ||
end | ||
|
||
private | ||
|
||
def default_attrs | ||
{ | ||
data: { | ||
rbui__form_field_target: "input", | ||
action: "input->rbui--form-field#onInput invalid->rbui--form-field#onInvalid" | ||
}, | ||
class: "flex h-9 w-full rounded-md border bg-background px-3 py-1 text-sm shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium focus-visible:outline-none focus-visible:ring-1 disabled:cursor-not-allowed disabled:opacity-50" | ||
} | ||
end | ||
end | ||
end |
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
Oops, something went wrong.