-
Notifications
You must be signed in to change notification settings - Fork 32
chore: 🤖 use hds keyvalue component #3027
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: llb/key-value-migration
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| {{! | ||
| Copyright (c) HashiCorp, Inc. | ||
| SPDX-License-Identifier: BUSL-1.1 | ||
| }} | ||
| <Hds::Form::KeyValueInputs | ||
| @data={{this.data}} | ||
| @isRequired={{@isRequired}} | ||
| @isOptional={{@isOptional}} | ||
| disabled={{@disabled}} | ||
| > | ||
| <:header as |H|> | ||
| {{#if @legend}} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be optional? It seems we should always have a legend to be accessible
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have a use cases for not having a legend? I also lean towards this not being optional. |
||
| <H.Legend data-test-legend>{{@legend}}</H.Legend> | ||
| {{/if}} | ||
| {{#if @helperText}} | ||
| <H.HelperText data-test-helper-text>{{@helperText}}</H.HelperText> | ||
| {{/if}} | ||
| </:header> | ||
| <:row as |R|> | ||
| <R.Field as |F|> | ||
| <F.Label>{{or @keyLabel (t 'form.key.label')}}</F.Label> | ||
| {{#if (eq @keyFieldType 'select')}} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if this component is trying too hard to simplify the HDS component without adding enough convenience for the limitations it now builds in. It seems the Would it make more sense to cut down and remove the baked in fields (as well as the switches for
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking over this and Zed's comment, I think I would lean towards allowing the consumer to handle this by yielding back. That should give us the flexibility without this complexity I think. |
||
| <F.Select | ||
| name={{@name}} | ||
| disabled={{@disabled}} | ||
| data-test-key-input | ||
| @isInvalid={{@errors}} | ||
| {{on 'change' (fn this.updateKey R.rowData)}} | ||
| > | ||
| <option disabled hidden selected value=''> | ||
| {{t 'titles.choose-an-option'}} | ||
| </option> | ||
| {{#each @keyFieldOptions as |selectOption|}} | ||
| <option | ||
| value={{selectOption}} | ||
| selected={{eq R.rowData.key selectOption}} | ||
| > | ||
| {{selectOption}} | ||
| </option> | ||
| {{/each}} | ||
| </F.Select> | ||
| {{else if (eq @keyFieldType 'textarea')}} | ||
| <F.Textarea | ||
| name={{@name}} | ||
| @value={{R.rowData.key}} | ||
DhariniJeeva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| disabled={{@disabled}} | ||
| data-test-key-input | ||
| @isInvalid={{@errors}} | ||
| {{on 'input' (fn this.updateKey R.rowData)}} | ||
| /> | ||
| {{else}} | ||
| {{! Default to text input }} | ||
| <F.TextInput | ||
| name={{@name}} | ||
| @value={{R.rowData.key}} | ||
| disabled={{@disabled}} | ||
| data-test-key-input | ||
| @isInvalid={{@errors}} | ||
| {{on 'input' (fn this.updateKey R.rowData)}} | ||
| /> | ||
| {{/if}} | ||
| </R.Field> | ||
|
|
||
| {{#unless this.hideValueField}} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be hiding the key field instead of the value field if we just want one field? Though should this even be necessary? |
||
| <R.Field as |F|> | ||
| <F.Label>{{or @valueLabel (t 'form.value.label')}}</F.Label> | ||
| {{#if (eq @valueFieldType 'select')}} | ||
| <F.Select | ||
| name={{@name}} | ||
| disabled={{@disabled}} | ||
| data-test-value-input | ||
| @isInvalid={{@errors}} | ||
| {{on 'change' (fn this.updateValue R.rowData)}} | ||
| > | ||
| <option disabled hidden selected value=''> | ||
| {{t 'titles.choose-an-option'}} | ||
| </option> | ||
| {{#each @valueFieldOptions as |selectOption|}} | ||
| <option | ||
| value={{selectOption}} | ||
| selected={{eq R.rowData.value selectOption}} | ||
| > | ||
| {{selectOption}} | ||
| </option> | ||
| {{/each}} | ||
| </F.Select> | ||
| {{else if (eq @valueFieldType 'textarea')}} | ||
| <F.Textarea | ||
| name={{@name}} | ||
| @value={{R.rowData.value}} | ||
| disabled={{@disabled}} | ||
| data-test-value-input | ||
| @isInvalid={{@errors}} | ||
| {{on 'input' (fn this.updateValue R.rowData)}} | ||
| /> | ||
| {{else}} | ||
| {{! Default to text input }} | ||
| <F.TextInput | ||
| name={{@name}} | ||
| @value={{R.rowData.value}} | ||
| disabled={{@disabled}} | ||
| data-test-value-input | ||
| @isInvalid={{@errors}} | ||
| {{on 'input' (fn this.updateValue R.rowData)}} | ||
| /> | ||
| {{/if}} | ||
| </R.Field> | ||
| {{/unless}} | ||
|
|
||
| <R.DeleteRowButton | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like we need to hide this if there's no input yet according to HDS suggestions. |
||
| data-test-delete-button | ||
| onclick={{fn this.removeRow R.rowData}} | ||
| /> | ||
| </:row> | ||
| <:footer as |F|> | ||
|
|
||
| {{#if @errors}} | ||
| <F.Error as |E|> | ||
| {{#each @errors as |error|}} | ||
| <E.Message data-test-error-message>{{error.message}}</E.Message> | ||
| {{/each}} | ||
| </F.Error> | ||
| {{/if}} | ||
| <F.AddRowButton | ||
| @text={{t 'actions.add'}} | ||
| data-test-add-button | ||
| onclick={{this.addNewRow}} | ||
| /> | ||
| </:footer> | ||
| </Hds::Form::KeyValueInputs> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /** | ||
| * Copyright (c) HashiCorp, Inc. | ||
| * SPDX-License-Identifier: BUSL-1.1 | ||
| */ | ||
|
|
||
| import Component from '@glimmer/component'; | ||
| import { tracked } from '@glimmer/tracking'; | ||
| import { action } from '@ember/object'; | ||
|
|
||
| export default class FormFieldKeyValueComponent extends Component { | ||
| // =properties | ||
|
|
||
| @tracked data = this.args.data?.length | ||
| ? [...this.args.data] | ||
| : [this.createEmptyRow()]; | ||
|
|
||
| /** | ||
| * Determine if the value field should be shown | ||
| * Defaults to false (show the value field by default) | ||
| * @type {boolean} | ||
| */ | ||
| get hideValueField() { | ||
| return this.args.hideValueField ?? false; | ||
| } | ||
|
|
||
| createEmptyRow() { | ||
| const row = { key: '' }; | ||
| if (!this.hideValueField) { | ||
| row.value = ''; | ||
| } | ||
| return row; | ||
| } | ||
|
|
||
| // =actions | ||
|
|
||
| /** | ||
| * Updates the key value for a specific row and triggers change notification | ||
| * @param {Object} rowData | ||
| * @param {Event} event | ||
| */ | ||
| @action | ||
| updateKey(rowData, { target: { value } }) { | ||
| this.updateRowProperty(rowData, 'key', value); | ||
| } | ||
|
|
||
| /** | ||
| * Updates the value field for a specific row and triggers change notification | ||
| * @param {Object} rowData | ||
| * @param {Event} event | ||
| */ | ||
| @action | ||
| updateValue(rowData, { target: { value } }) { | ||
| this.updateRowProperty(rowData, 'value', value); | ||
| } | ||
|
|
||
| @action | ||
| addNewRow() { | ||
| this.data = [...this.data, this.createEmptyRow()]; | ||
| this.notifyChange(); | ||
| } | ||
|
|
||
| @action | ||
| removeRow(rowData) { | ||
| this.data = this.data.filter((item) => item !== rowData); | ||
|
|
||
| // Ensure at least one row exists | ||
| if (this.data.length === 0) { | ||
| this.data = [this.createEmptyRow()]; | ||
| } | ||
| this.notifyChange(); | ||
| } | ||
|
|
||
| /** | ||
| * Helper method to update a property on a row | ||
| * @param {Object} rowData - The row object to update | ||
| * @param {string} property - The property name to update | ||
| * @param {string} value - The new value | ||
| */ | ||
| updateRowProperty(rowData, property, value) { | ||
| rowData[property] = value; | ||
| this.data = [...this.data]; | ||
| this.notifyChange(); | ||
| } | ||
|
|
||
| /** | ||
| * Notifies parent component of data changes with filtered results | ||
| */ | ||
| notifyChange() { | ||
| if (this.args.onChange) { | ||
| // Filter out entries with empty/whitespace keys | ||
| const filteredData = this.data.filter((item) => item.key?.trim()); | ||
| this.args.onChange(filteredData); | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.