Skip to content

Commit

Permalink
added dynamic text field
Browse files Browse the repository at this point in the history
  • Loading branch information
guestDI committed Sep 28, 2024
1 parent d376be0 commit e42da9e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 20 deletions.
35 changes: 23 additions & 12 deletions app/components/radio-button.hbs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
<div class="form-group">
<label>{{this.args.element.label}}</label>
{{#each this.args.element.options as |option|}}
<div class="radio-option">
<div class="field">
{{#each @element.options as |option|}}
<div class="control">
<label class="radio">
<input
type="radio"
name={{@element.id}}
value={{option.value}}
checked={{eq @element.value option.value}}
{{on "change" this.updateSelection}}
>
{{option.label}}
</label>
</div>
{{/each}}

{{#if this.showOtherInput}}
<div class="control mt-2">
<input
type="radio"
id={{option.value}}
name={{this.args.element.id}}
value={{option.value}}
{{on "change" this.updateSelection}}
checked={{eq this.args.element.value option.value}}
type="text"
class="input"
placeholder="Please specify"
{{on "input" this.updateOtherValue}}
>
<label for={{option.value}}>{{option.label}}</label>
</div>
{{/each}}
{{/if}}
</div>
25 changes: 20 additions & 5 deletions app/components/radio-button.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';

export default class RadioButtonComponent extends Component {
@tracked showOtherInput = false;

@action
updateSelection(event) {
// Проверим, что event и event.target существуют
if (event && event.target) {
const selectedValue = event.target.value; // Получаем значение из event
this.args.updateValue(selectedValue); // Передаём выбранное значение
const selectedValue = event.target.value;

if (selectedValue === 'other') {
this.showOtherInput = true; // Показываем текстовое поле
} else {
console.error('Event or event.target is undefined');
this.showOtherInput = false; // Скрываем текстовое поле
}

if (this.args.updateValue) {
this.args.updateValue(selectedValue); // Обновляем выбранное значение
}
}

@action
updateOtherValue(event) {
const otherValue = event.target.value;
if (this.args.updateValue) {
this.args.updateValue(otherValue); // Передаем значение "Other"
}
}
}
7 changes: 4 additions & 3 deletions app/controllers/survey.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default class SurveyPageController extends Controller {
{ label: 'Red', value: 'red' },
{ label: 'Green', value: 'green' },
{ label: 'Blue', value: 'blue' },
{ value: "other", label: "Other" }
],
),
],
Expand Down Expand Up @@ -72,9 +73,9 @@ export default class SurveyPageController extends Controller {
'plans',
true,
[
{ label: 'Technologies', cheked: false },
{ label: 'People', cheked: false },
{ label: 'Other', cheked: false },
{ label: 'Technologies', checked: false },
{ label: 'People', checked: false },
{ label: 'Other', checked: false },
],
),
],
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/lookup-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export default helper(function lookupComponent([type]) {
switch (type) {
case 'text':
return 'text-field';
case 'checkbox':
return 'checkbox-field';
case 'email':
return 'email-field';
case 'button':
Expand Down
2 changes: 2 additions & 0 deletions app/templates/survey.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
</div>
{{/if}}
{{#if (eq element.type "checkbox")}}
<div class="field">
<Checkbox @element={{element}} @updateValue={{fn this.updateElementValue element}} />
</div>
{{/if}}
{{/each}}
</div>
Expand Down

0 comments on commit e42da9e

Please sign in to comment.