Skip to content

Commit

Permalink
feature(#1005): provide a click handler for click on right icon for K…
Browse files Browse the repository at this point in the history
…tField

This is needed for KtPassword to reveal/hide passwords to user
  • Loading branch information
RueRivoli committed Nov 27, 2024
1 parent c961866 commit c3c940e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
15 changes: 14 additions & 1 deletion packages/documentation/pages/usage/components/form-fields.vue
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,16 @@
</template>
</KtFieldSingleSelect>
</div>
<div class="field-row">

Check warning on line 297 in packages/documentation/pages/usage/components/form-fields.vue

View workflow job for this annotation

GitHub Actions / eslint

Delete `↹`
<KtFieldToggle

Check warning on line 298 in packages/documentation/pages/usage/components/form-fields.vue

View workflow job for this annotation

GitHub Actions / eslint

Delete `↹`
formKey="isRightIconAction"

Check warning on line 299 in packages/documentation/pages/usage/components/form-fields.vue

View workflow job for this annotation

GitHub Actions / eslint

Delete `↹`
helpText="Support Varies"

Check warning on line 300 in packages/documentation/pages/usage/components/form-fields.vue

View workflow job for this annotation

GitHub Actions / eslint

Delete `↹`
isOptional

Check warning on line 301 in packages/documentation/pages/usage/components/form-fields.vue

View workflow job for this annotation

GitHub Actions / eslint

Delete `↹`
label="isRightIconAction"

Check warning on line 302 in packages/documentation/pages/usage/components/form-fields.vue

View workflow job for this annotation

GitHub Actions / eslint

Delete `↹`
type="switch"

Check warning on line 303 in packages/documentation/pages/usage/components/form-fields.vue

View workflow job for this annotation

GitHub Actions / eslint

Delete `↹`
/>

Check warning on line 304 in packages/documentation/pages/usage/components/form-fields.vue

View workflow job for this annotation

GitHub Actions / eslint

Delete `↹`
</div>

Check warning on line 305 in packages/documentation/pages/usage/components/form-fields.vue

View workflow job for this annotation

GitHub Actions / eslint

Delete `↹`

Check warning on line 306 in packages/documentation/pages/usage/components/form-fields.vue

View workflow job for this annotation

GitHub Actions / eslint

Delete `↹↹↹↹↹↹`
<div
v-if="componentDefinition.supports.autoComplete"
class="field-row"
Expand Down Expand Up @@ -1229,6 +1239,7 @@ export default defineComponent({
hasHelpTextSlot: boolean
helpDescription: Kotti.FieldText.Value
helpText: Kotti.FieldText.Value
isRightIconAction: boolean
label: Kotti.FieldText.Value
leftIcon: Yoco.Icon | null
locale: Kotti.I18n.SupportedLanguages
Expand Down Expand Up @@ -1296,13 +1307,14 @@ export default defineComponent({
hasHelpTextSlot: false,
helpDescription: null,
helpText: null,
isRightIconAction: true,
label: 'Label',
leftIcon: null,
locale: 'en-US',
placeholder: null,
placeholder2: null,
prefix: null,
rightIcon: null,
rightIcon: Yoco.Icon.EYE,
size: Kotti.Field.Size.MEDIUM,
suffix: null,
tabIndex: null,
Expand Down Expand Up @@ -1346,6 +1358,7 @@ export default defineComponent({
if (componentDefinition.value.supports.decoration)
Object.assign(additionalProps, {
isRightIconAction: settings.value.isRightIconAction,
leftIcon: settings.value.leftIcon,
prefix: settings.value.prefix,
rightIcon: settings.value.rightIcon,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
v-bind="{ field }"
:getEmptyValue="() => null"
:helpTextSlot="$slots.helpText"
@rightIconClick="handleVisibilityChange"
>
<input v-bind="inputProps" @input="onInput" />
</KtField>
</template>

<script lang="ts">
import { computed, defineComponent } from 'vue'
import { computed, defineComponent, ref } from 'vue'
import type { InputHTMLAttributes } from 'vue/types/jsx'
import { KtField } from '../kotti-field'
Expand All @@ -31,9 +32,8 @@ export default defineComponent({
props,
supports: KOTTI_FIELD_PASSWORD_SUPPORTS,
})
const fieldType = ref('password')
const { forceUpdate, forceUpdateKey } = useForceUpdate()
return {
field,
inputProps: computed(
Expand All @@ -47,7 +47,7 @@ export default defineComponent({
forceUpdateKey: forceUpdateKey.value,
placeholder: props.placeholder ?? undefined,
size: 1,
type: 'password',
type: fieldType.value,
value: field.currentValue ?? '',
}),
),
Expand All @@ -57,6 +57,10 @@ export default defineComponent({
forceUpdate()
},
handleVisibilityChange: () => {
const isValueHidden = fieldType.value === 'password'
fieldType.value = isValueHidden ? 'text' : 'password'
},
}
},
})
Expand Down
14 changes: 11 additions & 3 deletions packages/kotti-ui/source/kotti-field/KtField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@
</slot>
<div
v-if="field.rightIcon"
:class="iconClasses('input-container', ['right'])"
:class="
field.isRightIconAction
? iconClasses('input-container', ['right', 'interactive'])
: iconClasses('input-container', ['right'])
"
@click.stop="handleRightIconClick"
>
<i class="yoco" v-text="field.rightIcon" />
</div>
Expand Down Expand Up @@ -149,8 +154,8 @@ export default defineComponent({
isRange: { default: false, type: Boolean },
useFieldset: { default: false, type: Boolean },
},
emits: ['click', 'mousedown'],
setup(props) {
emits: ['click', 'mousedown', 'rightIconClick'],
setup(props, { emit }) {
const inputId = computed(() =>
props.isRange
? `${props.field.inputProps.id}-start`
Expand Down Expand Up @@ -191,6 +196,9 @@ export default defineComponent({
props.field.setValue(props.getEmptyValue())
focusInput()
},
handleRightIconClick: () => {
if (props.field.isRightIconAction) emit('rightIconClick')
},
hasHelpText: computed(
() => props.helpTextSlot.length > 0 || props.field.helpText !== null,
),
Expand Down
3 changes: 3 additions & 0 deletions packages/kotti-ui/source/kotti-field/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const useDecoration = <DATA_TYPE>({
supports,
}: Pick<KottiField.Hook.Parameters<DATA_TYPE>, 'props' | 'supports'>) => {
return {
isRightIconAction: computed(() =>
supports.decoration ? props.isRightIconAction : false,
),
leftIcon: computed(() => (supports.decoration ? props.leftIcon : null)),
prefix: computed(() => (supports.decoration ? props.prefix : null)),
rightIcon: computed(() => (supports.decoration ? props.rightIcon : null)),
Expand Down
3 changes: 3 additions & 0 deletions packages/kotti-ui/source/kotti-field/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export module KottiField {
| 'isDisabled'
| 'isLoading'
| 'isOptional'
| 'isRightIconAction'
| 'label'
| 'leftIcon'
| 'prefix'
Expand Down Expand Up @@ -131,6 +132,7 @@ export module KottiField {
])
.default(AutoComplete.OFF),
hideClear: z.boolean().default(false),
isRightIconAction: z.boolean().default(false),
leftIcon: yocoIconSchema.nullable().default(null),
prefix: z.string().nullable().default(null),
rightIcon: yocoIconSchema.nullable().default(null),
Expand Down Expand Up @@ -234,6 +236,7 @@ export module KottiField {
* This is never by default and specified by component schemas that need it
*/
isOptional: z.boolean().default(false),
isRightIconAction: z.boolean().default(false),

label: z.string().nullable().default(null),

Expand Down

0 comments on commit c3c940e

Please sign in to comment.