Skip to content

Commit

Permalink
refactor(frontend): use composition api
Browse files Browse the repository at this point in the history
  • Loading branch information
syuilo committed May 14, 2023
1 parent 238d0fa commit 3d4a90b
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 387 deletions.
118 changes: 0 additions & 118 deletions packages/frontend/src/components/MkSample.vue

This file was deleted.

23 changes: 7 additions & 16 deletions packages/frontend/src/components/MkSuperMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,13 @@
</div>
</template>

<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
props: {
def: {
type: Array,
required: true,
},
grid: {
type: Boolean,
required: false,
default: false,
},
},
});
<script lang="ts" setup>
import { } from 'vue';
defineProps<{
def: any[];
grid?: boolean;
}>();
</script>

<style lang="scss" scoped>
Expand Down
209 changes: 72 additions & 137 deletions packages/frontend/src/components/MkTextarea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,153 +26,88 @@
</div>
</template>

<script lang="ts">
import { defineComponent, onMounted, nextTick, ref, watch, computed, toRefs } from 'vue';
<script lang="ts" setup>
import { onMounted, nextTick, ref, watch, computed, toRefs, shallowRef } from 'vue';
import { debounce } from 'throttle-debounce';
import MkButton from '@/components/MkButton.vue';
import { i18n } from '@/i18n';
export default defineComponent({
components: {
MkButton,
},
props: {
modelValue: {
required: true,
},
type: {
type: String,
required: false,
},
required: {
type: Boolean,
required: false,
},
readonly: {
type: Boolean,
required: false,
},
disabled: {
type: Boolean,
required: false,
},
pattern: {
type: String,
required: false,
},
placeholder: {
type: String,
required: false,
},
autofocus: {
type: Boolean,
required: false,
default: false,
},
autocomplete: {
required: false,
},
spellcheck: {
required: false,
},
code: {
type: Boolean,
required: false,
},
tall: {
type: Boolean,
required: false,
default: false,
},
pre: {
type: Boolean,
required: false,
default: false,
},
debounce: {
type: Boolean,
required: false,
default: false,
},
manualSave: {
type: Boolean,
required: false,
default: false,
},
},
emits: ['change', 'keydown', 'enter', 'update:modelValue'],
setup(props, context) {
const { modelValue, autofocus } = toRefs(props);
const v = ref(modelValue.value);
const focused = ref(false);
const changed = ref(false);
const invalid = ref(false);
const filled = computed(() => v.value !== '' && v.value != null);
const inputEl = ref(null);
const focus = () => inputEl.value.focus();
const onInput = (ev) => {
changed.value = true;
context.emit('change', ev);
};
const onKeydown = (ev: KeyboardEvent) => {
if (ev.isComposing || ev.key === 'Process' || ev.keyCode === 229) return;
context.emit('keydown', ev);
if (ev.code === 'Enter') {
context.emit('enter');
}
};
const updated = () => {
changed.value = false;
context.emit('update:modelValue', v.value);
};
const props = defineProps<{
modelValue: string | null;
required?: boolean;
readonly?: boolean;
disabled?: boolean;
pattern?: string;
placeholder?: string;
autofocus?: boolean;
autocomplete?: string;
spellcheck?: boolean;
debounce?: boolean;
manualSave?: boolean;
code?: boolean;
tall?: boolean;
pre?: boolean;
}>();
const emit = defineEmits<{
(ev: 'change', _ev: KeyboardEvent): void;
(ev: 'keydown', _ev: KeyboardEvent): void;
(ev: 'enter'): void;
(ev: 'update:modelValue', value: string): void;
}>();
const { modelValue, autofocus } = toRefs(props);
const v = ref<string>(modelValue.value ?? '');
const focused = ref(false);
const changed = ref(false);
const invalid = ref(false);
const filled = computed(() => v.value !== '' && v.value != null);
const inputEl = shallowRef<HTMLTextAreaElement>();
const focus = () => inputEl.value.focus();
const onInput = (ev) => {
changed.value = true;
emit('change', ev);
};
const onKeydown = (ev: KeyboardEvent) => {
if (ev.isComposing || ev.key === 'Process' || ev.keyCode === 229) return;
emit('keydown', ev);
if (ev.code === 'Enter') {
emit('enter');
}
};
const debouncedUpdated = debounce(1000, updated);
const updated = () => {
changed.value = false;
emit('update:modelValue', v.value ?? '');
};
watch(modelValue, newValue => {
v.value = newValue;
});
const debouncedUpdated = debounce(1000, updated);
watch(v, newValue => {
if (!props.manualSave) {
if (props.debounce) {
debouncedUpdated();
} else {
updated();
}
}
watch(modelValue, newValue => {
v.value = newValue;
});
invalid.value = inputEl.value.validity.badInput;
});
watch(v, newValue => {
if (!props.manualSave) {
if (props.debounce) {
debouncedUpdated();
} else {
updated();
}
}
onMounted(() => {
nextTick(() => {
if (autofocus.value) {
focus();
}
});
});
invalid.value = inputEl.value.validity.badInput;
});
return {
v,
focused,
invalid,
changed,
filled,
inputEl,
focus,
onInput,
onKeydown,
updated,
i18n,
};
},
onMounted(() => {
nextTick(() => {
if (autofocus.value) {
focus();
}
});
});
</script>

Expand Down
Loading

1 comment on commit 3d4a90b

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chromatic detects changes. Please review the changes on Chromatic.

Please sign in to comment.