Skip to content

Commit 66ba2f1

Browse files
committed
feat: override form input values within update hook
1 parent eb48315 commit 66ba2f1

File tree

7 files changed

+43
-25
lines changed

7 files changed

+43
-25
lines changed

packages/common-helpers/src/form/input.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class FormInput<V extends iFormValue = iFormValue>
139139
*/
140140
constructor(
141141
formInput: iFormInput<V>,
142-
private _onUpdatedValues?: (updatedValues: (V | V[])[]) => void,
142+
private _onUpdatedValues?: (updatedValues: (V | V[])[]) => (V | V[])[] | undefined | void,
143143
rerender?: (fi?: Partial<iFormInput<V>>) => void
144144
) {
145145
super(formInput, rerender);
@@ -162,9 +162,8 @@ export class FormInput<V extends iFormValue = iFormValue>
162162
// set defaults
163163
this._values = Array(this.min).fill(getDefault(this.type, this.defaults));
164164
} else {
165-
this._values = updatedValues;
166165
// run hook on values change
167-
this._onUpdatedValues?.(this._values);
166+
this._values = this._onUpdatedValues?.(updatedValues) ?? updatedValues;
168167
}
169168
}
170169

@@ -215,7 +214,7 @@ export class FormInput<V extends iFormValue = iFormValue>
215214
*/
216215
public clone(
217216
overrides?: Omit<iFormInput<V>, "name"> & { name?: string },
218-
onUpdatedValues: ((updatedValues: (V | V[])[]) => void) | undefined = this._onUpdatedValues
217+
onUpdatedValues?: (updatedValues: (V | V[])[]) => (V | V[])[] | undefined | void
219218
) {
220219
const oldFormInput: iFormInput<V> = {
221220
...this,
@@ -224,7 +223,11 @@ export class FormInput<V extends iFormValue = iFormValue>
224223
defaults: this.defaults,
225224
};
226225

227-
return new FormInput({ ...oldFormInput, ...overrides }, onUpdatedValues, this.rerender);
226+
return new FormInput(
227+
{ ...oldFormInput, ...overrides },
228+
onUpdatedValues || this._onUpdatedValues,
229+
this.rerender
230+
);
228231
}
229232

230233
/**

packages/components-vue/src/components/form/Input.vue

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,40 @@
1111
>
1212
<component
1313
:is="input.multiple ? ActionButtonToggle : ActionButton"
14-
v-for="(option, optionIndex) in options"
15-
:key="optionIndex"
14+
v-for="option in options"
15+
:key="`choice-${_.snakeCase(String(option.value))}-${input.options.length}`"
1616
:theme="theme"
1717
:aria-label="option.alias || option.value"
1818
:active="modelValue.includes(option.value)"
1919
:title="modelValue.includes(option.value) ? t('select_selected') : ''"
2020
:disabled="readonly || (!input.multiple && modelValue.includes(option.value))"
21+
:round="!!option.icon || (!!option.pattern && !option.alias)"
22+
:tooltip="option.icon ? option.alias : ''"
23+
tooltip-as-text
24+
tooltip-position="bottom"
2125
@click="choose(option.value)"
2226
>
23-
<span>{{ option.alias || option.value }}</span>
2427
<template v-if="option.icon">
2528
<IconFa :name="option.icon" />
2629
<IconFa v-if="input.multiple" :name="option.icon" regular />
2730
</template>
28-
<figure
29-
v-else-if="option.pattern"
30-
class="avatar --size-xs --bdr --bdrColor-light"
31-
:style="
32-
validator.isURL(option.pattern)
33-
? { backgroundImage: `url('${option.pattern}')` }
34-
: { backgroundColor: option.pattern }
35-
"
36-
></figure>
31+
<template v-else-if="option.pattern">
32+
<span v-if="option.alias">{{ option.alias }}</span>
33+
<figure
34+
class="avatar --size-xs --bdr"
35+
:class="`--bdrColor-${
36+
themeValues[
37+
!input.multiple && modelValue.includes(option.value) ? 0 : 1
38+
]
39+
}`"
40+
:style="
41+
validator.isURL(option.pattern)
42+
? { backgroundImage: `url('${option.pattern}')` }
43+
: { backgroundColor: option.pattern }
44+
"
45+
></figure>
46+
</template>
47+
<span v-else>{{ option.alias || option.value }}</span>
3748
</component>
3849
</div>
3950
</FormInputOptions>
@@ -318,6 +329,7 @@
318329
import useInput from "../../composables/input";
319330
import useCountries from "../../composables/countries";
320331
import { useHelpers } from "../../composables/utils";
332+
import useTheme from "../../composables/theme";
321333
322334
interface iFormInputProps extends iUseThemeProps {
323335
modelValue: any[];
@@ -344,6 +356,7 @@
344356
const { isValidFormInputValue, notEmptyValue } = useHelpers(useForm).utils;
345357
const { getInputPlaceholder, getInputAutocomplete, getInputTextType } = useInput(props);
346358
const { defaultCountry } = useCountries();
359+
const { themeValues } = useTheme(props);
347360
348361
const countriesArr = computed(() => {
349362
return (props.countries || []).map(({ code, name }) => ({ value: code, alias: name }));

packages/components-vue/src/components/form/Simple.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
{{ getSuggestedTitle(input) }}
2323
</p>
2424
<FormInput
25+
:key="`simple-${input.name}-${String(input.values[0])}-${input.options.length}`"
2526
:readonly="readonly"
2627
:theme="theme"
2728
:input="input"

packages/components-vue/src/components/input/Text.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</BaseWrapper>
2222
<template v-if="type === 'number' && (Number.isInteger(min) || Number.isInteger(max))">
2323
<ActionButtonToggle
24-
:disabled="Number(model) <= minValue"
24+
:disabled="disabled || Number(model) <= minValue"
2525
:size="size"
2626
:theme="invalid ? eColors.DANGER : theme"
2727
:aria-label="t('decrease')"
@@ -35,7 +35,7 @@
3535
<IconFa name="minus" regular />
3636
</ActionButtonToggle>
3737
<ActionButtonToggle
38-
:disabled="Number(model) >= maxValue"
38+
:disabled="disabled || Number(model) >= maxValue"
3939
:size="size"
4040
:theme="invalid ? eColors.DANGER : theme"
4141
:tooltip="t('increase')"

packages/styles/src/components/action/_box.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
@include module.extend-maps(
120120
module.$element-sizes,
121121
meta.get-function("box-square-size-selector"),
122-
md,
122+
lg,
123123
".--square"
124124
)
125125
using($name, $value, $is-default) {

packages/styles/src/components/avatar/_avatar.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
@include module.extend-maps(
2020
module.$element-sizes,
2121
meta.get-function("avatar-size-selector"),
22-
sm
22+
md
2323
)
2424
using($name, $value, $is-default) {
2525
width: $value;

packages/styles/src/utils/_variables.scss

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,9 @@ $viewports: (
214214

215215
// element sizes
216216
$element-sizes: (
217-
xs: $font-size-md * 2,
218-
sm: $font-size-md * 3,
219-
md: $font-size-md * 6,
220-
lg: $font-size-md * 9,
217+
xs: $font-size-md * 1.5,
218+
sm: $font-size-md * 2,
219+
md: $font-size-md * 3,
220+
lg: $font-size-md * 6,
221+
xl: $font-size-md * 9,
221222
);

0 commit comments

Comments
 (0)