Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #205 from asigloo/bugfix/add-optionvalue-and-optio…
Browse files Browse the repository at this point in the history
…nkey-properties

feat(select): optionValue and optionKey
  • Loading branch information
alvarosabu authored Dec 14, 2020
2 parents b7550af + b349b8b commit bdce974
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
25 changes: 13 additions & 12 deletions dev/typescript/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,23 @@ export default defineComponent({
value: 'the-last-of-us',
options: [
{
key: 'the-last-of-us',
value: 'The Last of Us II',
value: 'the-last-of-us',
label: 'The Last of Us II',
},
{
key: 'death-stranding',
value: 'Death Stranding',
value: 'death-stranding',
label: 'Death Stranding',
},
{
key: 'nier-automata',
value: 'Nier Automata',
value: 'nier-automata',
label: 'Nier Automata',
},
],
}),
console: SelectField({
label: 'Console (Async Options)',
customClass: 'w-1/2 pr-4',
optionValue: 'console',
options: consoleOptions.value,
}),
steps: NumberField({
Expand Down Expand Up @@ -256,16 +257,16 @@ export default defineComponent({
try {
consoleOptions.value = await mockAsync(true, 4000, [
{
key: 'playstation',
value: 'Playstation',
console: 'playstation',
label: 'Playstation',
},
{
key: 'nintendo',
value: 'Nintendo',
console: 'nintendo',
label: 'Nintendo',
},
{
key: 'xbox',
value: 'Xbox',
console: 'xbox',
label: 'Xbox',
},
]);
} catch (e) {
Expand Down
12 changes: 10 additions & 2 deletions src/components/select-input/SelectInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ export default defineComponent({
return props?.control?.options;
});
const options = formattedOptions.value.map(({ key, value, disabled }) =>
h('option', { key, value: key, disabled }, value),
const options = formattedOptions.value.map(option =>
h(
'option',
{
key: option[props.control.optionValue],
value: option[props.control.optionValue],
disabled: option.disabled,
},
option[props.control.optionLabel],
),
);
return [
h(
Expand Down
4 changes: 4 additions & 0 deletions src/core/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,15 @@ export const NumberField = ({
export const SelectField = ({
options = [],
value,
optionValue = 'value',
optionLabel = 'label',
...rest
}: Partial<SelectInput>): SelectInput => ({
...FieldBase(rest),
value,
options,
optionValue,
optionLabel,
type: FieldTypes.SELECT,
});

Expand Down
4 changes: 3 additions & 1 deletion src/core/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ export type NumberInput = InputBase & {
export type SelectInput<T = boolean | string> = InputBase & {
type: FieldTypes.SELECT;
value: T;
options?: { key: string; value: string; disabled?: boolean }[];
optionValue: string;
optionLabel: string;
options?: { label: string; value: string; disabled?: boolean }[];
};

export type TextAreaInput = InputBase & {
Expand Down

0 comments on commit bdce974

Please sign in to comment.