Skip to content

Commit

Permalink
feat(fixes unit entry): specify characters you want to reject from te…
Browse files Browse the repository at this point in the history
…ra-input (#4463)
  • Loading branch information
shawnyama authored Aug 15, 2024
1 parent f3a1fcb commit 744978c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@
v-else
label="Unit"
placeholder="Add a unit"
:characters-to-reject="[' ']"
:model-value="item.unitExpression ?? ''"
@update:model-value="
($event) => {
const value = $event.replace(/[\s.]+/g, '');
$emit('update-item', { key: 'unitExpression', value });
}
"
@focusout="($event) => ($event.target.value = $event.target.value.replace(/[\s.]+/g, ''))"
@update:model-value="$emit('update-item', { key: 'unitExpression', value: $event })"
/>
</template>
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<script setup lang="ts">
import { numberToNist } from '@/utils/number';
import { isString, toNumber, isNaN } from 'lodash';
import { isString, toNumber, isNaN, isEmpty } from 'lodash';
import { CSSProperties, computed, onMounted, ref } from 'vue';
const props = defineProps<{
Expand All @@ -37,6 +37,7 @@ const props = defineProps<{
placeholder?: string;
autoWidth?: boolean;
autoFocus?: boolean;
charactersToReject?: string[];
}>();
const emit = defineEmits(['update:model-value', 'focusout', 'keyup', 'blur', 'focus', 'keypress']);
Expand Down Expand Up @@ -83,8 +84,13 @@ function formatValue(value: string | number | undefined) {
const updateValue = (event: Event) => {
const target = event.target as HTMLInputElement;
const value = target.value;
emit('update:model-value', value);
if (props.charactersToReject && !isEmpty(props.charactersToReject)) {
const start = target.selectionStart;
const end = target.selectionEnd;
target.value = target.value.replace(new RegExp(`[${props.charactersToReject.join('')}]`, 'g'), ''); // Create a regex pattern from charactersToReject to remove them
target.setSelectionRange(start, end); // Maintain cursor position, is needed if we are entering in the middle of the input
}
emit('update:model-value', target.value);
};
const onFocus = (event) => {
Expand Down

0 comments on commit 744978c

Please sign in to comment.