Beautiful Open-Source Form Builder
Select Design Changes (#409)
Chirag Chhatrala795fcfc97384
diff --git a/client/components/forms/SelectInput.vue b/client/components/forms/SelectInput.vueindex aab21b3f7..ee6b2990e 100644--- a/client/components/forms/SelectInput.vue+++ b/client/components/forms/SelectInput.vue@@ -3,14 +3,17 @@<template #label><slot name="label" /></template>+<v-selectv-model="compVal"+ :dusk="name":data="finalOptions":label="label":option-key="optionKey":emit-key="emitKey":required="required":multiple="multiple"+ :clearable="clearable":searchable="searchable":loading="loading":color="color"@@ -19,31 +22,33 @@:theme="theme":has-error="hasError":allow-creation="allowCreation"- :disabled="disabled ? true : null"+ :disabled="disabled":help="help":help-position="helpPosition"+ :remote="remote"+ :dropdown-class="dropdownClass"@update-options="updateOptions"@update:model-value="updateModelValue"><template #selected="{ option }">- <slot- name="selected"- :option="option"- :option-name="getOptionName(option)"- >- <template v-if="multiple">- <div class="flex items-center truncate mr-6">- <span class="truncate">- {{ getOptionNames(selectedValues).join(", ") }}- </span>- </div>- </template>- <template v-else>+ <template v-if="multiple">+ <div class="flex items-center truncate mr-6">+ <span class="truncate">+ {{ getOptionNames(selectedValues).join(', ') }}+ </span>+ </div>+ </template>+ <template v-else>+ <slot+ name="selected"+ :option="option"+ :option-name="getOptionName(option)"+ ><div class="flex items-center truncate mr-6"><div>{{ getOptionName(option) }}</div></div>- </template>- </slot>+ </slot>+ </template></template><template #option="{ option, selected }"><slot@@ -51,25 +56,18 @@:option="option":selected="selected">- <span class="flex group-hover:text-white">- <p class="flex-grow group-hover:text-white">+ <span class="flex">+ <p class="flex-grow">{{ option.name }}</p><spanv-if="selected"class="absolute inset-y-0 right-0 flex items-center pr-4 dark:text-white">- <svg- class="h-5 w-5"- viewBox="0 0 20 20"- fill="currentColor"- >- <path- fill-rule="evenodd"- d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"- clip-rule="evenodd"- />- </svg>+ <Icon+ name="heroicons:check-16-solid"+ class="w-5 h-5"+ /></span></span></slot>@@ -87,68 +85,86 @@</template><script>-import { inputProps, useFormInput } from "./useFormInput.js"-import InputWrapper from "./components/InputWrapper.vue"+import { inputProps, useFormInput } from './useFormInput.js'+import InputWrapper from './components/InputWrapper.vue'/*** Options: {name,value} objects*/export default {- name: "SelectInput",+ name: 'SelectInput',components: { InputWrapper },props: {...inputProps,options: { type: Array, required: true },- optionKey: { type: String, default: "value" },- emitKey: { type: String, default: "value" },- displayKey: { type: String, default: "name" },+ optionKey: { type: String, default: 'value' },+ emitKey: { type: String, default: 'value' },+ displayKey: { type: String, default: 'name' },loading: { type: Boolean, default: false },multiple: { type: Boolean, default: false },searchable: { type: Boolean, default: false },+ clearable: { type: Boolean, default: false },allowCreation: { type: Boolean, default: false },+ dropdownClass: { type: String, default: 'w-full' },+ remote: { type: Function, default: null }},-- setup(props, context) {+ setup (props, context) {return {- ...useFormInput(props, context),+ ...useFormInput(props, context)}},-- data() {+ data () {return {additionalOptions: [],- selectedValues: [],+ selectedValues: []}},-computed: {- finalOptions() {+ finalOptions () {return this.options.concat(this.additionalOptions)- },+ }+ },+ watch: {+ compVal: {+ handler (newVal, oldVal) {+ if (!oldVal) {+ this.handleCompValChanged()+ }+ },+ immediate: false+ }+ },+ mounted () {+ this.handleCompValChanged()},methods: {- getOptionName(val) {+ getOptionName (val) {const option = this.finalOptions.find((optionCandidate) => {return optionCandidate[this.optionKey] === val})if (option) return option[this.displayKey]return null},- getOptionNames(values) {- return values.map((val) => {+ getOptionNames (values) {+ return values.map(val => {return this.getOptionName(val)})},- updateModelValue(newValues) {+ updateModelValue (newValues) {if (newValues === null) newValues = []this.selectedValues = newValues},- updateOptions(newItem) {+ updateOptions (newItem) {if (newItem) {this.additionalOptions.push(newItem)}},- },+ handleCompValChanged () {+ if (this.compVal) {+ this.selectedValues = this.compVal+ }+ }+ }}</script>diff --git a/client/components/forms/components/VSelect.vue b/client/components/forms/components/VSelect.vueindex c7b843717..8278b31b0 100644--- a/client/components/forms/components/VSelect.vue+++ b/client/components/forms/components/VSelect.vue@@ -4,24 +4,18 @@class="v-select relative":class="[{ 'w-0': multiple, 'min-w-full': multiple }]">- <span class="inline-block w-full rounded-md">+ <div+ class="inline-block w-full flex overflow-hidden"+ :style="inputStyle"+ :class="[theme.SelectInput.input, { '!ring-red-500 !ring-2 !border-transparent': hasError, '!cursor-not-allowed !bg-gray-200': disabled }, inputClass]"+ ><buttontype="button"aria-haspopup="listbox"aria-expanded="true"aria-labelledby="listbox-label"- class="cursor-pointer"- :style="inputStyle"- :class="[- theme.SelectInput.input,- {- 'py-2': !multiple || loading,- 'py-1': multiple,- '!ring-red-500 !ring-2 !border-transparent': hasError,- '!cursor-not-allowed !bg-gray-200': disabled,- },- inputClass,- ]"+ class="cursor-pointer w-full flex-grow relative"+ :class="[{'py-2': !multiple || loading, 'py-1': multiple},theme.default.inputSpacing.horizontal]"@click="toggleDropdown"><div :class="{ 'h-6': !multiple, 'min-h-8': multiple && !loading }">@@ -43,6 +37,7 @@<slotname="selected":option="modelValue"+ :toggle="select"/></div><div@@ -60,51 +55,55 @@</div></transition></div>- <span- class="absolute inset-y-0 right-0 flex items-center pr-2 pointer-events-none"- >- <svg- class="h-5 w-5 text-gray-400"- viewBox="0 0 20 20"- fill="none"- stroke="currentColor"- >- <path- d="M7 7l3-3 3 3m0 6l-3 3-3-3"- stroke-width="1.5"- stroke-linecap="round"- stroke-linejoin="round"- />- </svg>+ <span class="absolute inset-y-0 right-0 flex items-center pr-2 pointer-events-none">+ <Icon+ name="heroicons:chevron-up-down-16-solid"+ class="h-5 w-5 text-gray-500"+ /></span></button>- </span>+ <button+ v-if="clearable && !isEmpty"+ class="hover:bg-gray-50 dark:hover:bg-gray-900 border-l px-2"+ :class="[theme.default.inputSpacing.vertical]"+ @click.prevent="clear()"+ >+ <Icon+ name="heroicons:x-mark-20-solid"+ class="w-5 h-5 text-gray-500"+ width="2em"+ dynamic+ />+ </button>+ </div><collapsiblev-model="isOpen"- class="absolute mt-1 rounded-md bg-white dark:bg-notion-dark-light shadow-xl z-10"- :class="dropdownClass"+ class="absolute mt-1 bg-white overflow-auto dark:bg-notion-dark-light shadow-xl z-10"+ :class="[dropdownClass,theme.SelectInput.dropdown]"@click-away="onClickAway"><ultabindex="-1"role="listbox"- class="rounded-md text-base leading-6 shadow-xs overflow-auto focus:outline-none sm:text-sm sm:leading-5 relative"- :class="{- 'max-h-42 py-1': !isSearchable,- 'max-h-48 pb-1': isSearchable,- }"+ class="text-base leading-6 shadow-xs overflow-auto focus:outline-none sm:text-sm sm:leading-5 relative"+ :class="{ 'max-h-42': !isSearchable, 'max-h-48': isSearchable }"><divv-if="isSearchable"- class="px-2 pt-2 sticky top-0 bg-white dark-bg-notion-dark-light z-10"+ class="sticky top-0 z-10 flex border-b border-gray-300">- <text-input+ <inputv-model="searchTerm"- name="search"- :color="color"- :theme="theme"- placeholder="Search..."- />+ type="text"+ class="flex-grow pl-3 pr-7 py-3 w-full focus:outline-none dark:text-white"+ placeholder="Search"+ >+ <div class="flex absolute right-0 inset-y-0 items-center px-2 justify-center pointer-events-none">+ <Icon+ name="heroicons:magnifying-glass-solid"+ class="h-5 w-5 text-gray-500 dark:text-gray-400"+ />+ </div></div><divv-if="loading"@@ -112,14 +111,17 @@><Loader class="h-6 w-6 text-nt-blue mx-auto" /></div>- <template v-if="filteredOptions.length > 0">+ <div+ v-if="filteredOptions.length > 0"+ class="p-1"+ ><liv-for="item in filteredOptions":key="item[optionKey]"role="option":style="optionStyle"- :class="{ 'px-3 pr-9': multiple, 'px-3': !multiple }"- class="text-gray-900 cursor-default select-none relative py-2 cursor-pointer group hover:text-white hover:bg-form-color focus:outline-none focus-text-white focus-nt-blue"+ :class="[{ 'px-3 pr-9': multiple, 'px-3': !multiple },dropdownClass,theme.SelectInput.option]"+ class="text-gray-900 cursor-default select-none relative py-2 cursor-pointer group hover:bg-gray-100 dark:hover:bg-gray-900 rounded focus:outline-none"@click="select(item)"><slot@@ -128,131 +130,128 @@:selected="isSelected(item)"/></li>- </template>+ </div><pv-else-if="!loading && !(allowCreation && searchTerm)"class="w-full text-gray-500 text-center py-2">- {{- allowCreation- ? "Type something to add an option"- : "No option available"- }}.+ {{ (allowCreation ? 'Type something to add an option' : 'No option available') }}.</p>- <li+ <divv-if="allowCreation && searchTerm"- role="option"- :style="optionStyle"- :class="{ 'px-3 pr-9': multiple, 'px-3': !multiple }"- class="text-gray-900 cursor-default select-none relative py-2 cursor-pointer group hover:text-white dark:text-white hover:bg-form-color focus:outline-none focus-text-white focus-nt-blue"- @click="createOption(searchTerm)"+ class="border-t border-gray-300 p-1">- Create- <b class="px-1 bg-gray-300 rounded group-hover-text-black">{{- searchTerm- }}</b>- </li>+ <li+ role="option"+ :style="optionStyle"+ :class="[{ 'px-3 pr-9': multiple, 'px-3': !multiple },dropdownClass,theme.SelectInput.option]"+ class="text-gray-900 cursor-default select-none relative py-2 cursor-pointer group hover:bg-gray-100 dark:hover:bg-gray-900 rounded focus:outline-none"+ @click="createOption(searchTerm)"+ >+ Create <span class="px-2 bg-gray-100 border border-gray-300 rounded group-hover-text-black">{{ searchTerm+ }}</span>+ </li>+ </div></ul></collapsible></div></template><script>-import Collapsible from "~/components/global/transitions/Collapsible.vue"-import { themes } from "~/lib/forms/form-themes.js"-import TextInput from "../TextInput.vue"-import debounce from "lodash/debounce"-import Fuse from "fuse.js"+import Collapsible from '~/components/global/transitions/Collapsible.vue'+import { themes } from '../../../lib/forms/form-themes.js'+import debounce from 'debounce'+import Fuse from 'fuse.js'export default {- name: "VSelect",- components: { Collapsible, TextInput },+ name: 'VSelect',+ components: { Collapsible },directives: {},props: {data: Array,modelValue: { default: null, type: [String, Number, Array, Object] },inputClass: { type: String, default: null },- dropdownClass: { type: String, default: "w-full" },+ dropdownClass: { type: String, default: 'w-full' },loading: { type: Boolean, default: false },required: { type: Boolean, default: false },multiple: { type: Boolean, default: false },searchable: { type: Boolean, default: false },+ clearable: { type: Boolean, default: false },hasError: { type: Boolean, default: false },remote: { type: Function, default: null },- searchKeys: { type: Array, default: () => ["name"] },- optionKey: { type: String, default: "id" },+ searchKeys: { type: Array, default: () => ['name'] },+ optionKey: { type: String, default: 'id' },emitKey: { type: String, default: null },- color: { type: String, default: "#3B82F6" },+ color: { type: String, default: '#3B82F6' },placeholder: { type: String, default: null },uppercaseLabels: { type: Boolean, default: true },theme: { type: Object, default: () => themes.default },allowCreation: { type: Boolean, default: false },- disabled: { type: Boolean, default: false },+ disabled: { type: Boolean, default: false }},- emits: ["update:modelValue", "update-options"],- data() {+ emits: ['update:modelValue', 'update-options'],+ data () {return {isOpen: false,- searchTerm: "",- defaultValue: this.modelValue ?? null,+ searchTerm: '',+ defaultValue: this.modelValue ?? null}},computed: {- optionStyle() {+ optionStyle () {return {- "--bg-form-color": this.color,+ '--bg-form-color': this.color}},- inputStyle() {+ inputStyle () {return {- "--tw-ring-color": this.color,+ '--tw-ring-color': this.color}},- debouncedRemote() {+ debouncedRemote () {if (this.remote) {return debounce(this.remote, 300)}return null},- filteredOptions() {+ filteredOptions () {if (!this.data) return []- if (!this.searchable || this.remote || this.searchTerm === "") {+ if (!this.searchable || this.remote || this.searchTerm === '') {return this.data}// Fuse searchconst fuzeOptions = {- keys: this.searchKeys,+ keys: this.searchKeys}const fuse = new Fuse(this.data, fuzeOptions)return fuse.search(this.searchTerm).map((res) => {return res.item})},- isSearchable() {+ isSearchable () {return this.searchable || this.remote !== null || this.allowCreation},+ isEmpty () {+ return this.multiple ? !this.modelValue || this.modelValue.length === 0 : !this.modelValue+ }},watch: {- searchTerm(val) {+ searchTerm (val) {if (!this.debouncedRemote) return- if (- (this.remote && val) ||- (val === "" && !this.modelValue) ||- (val === "" && this.isOpen)- ) {+ if ((this.remote && val) || (val === '' && !this.modelValue) || (val === '' && this.isOpen)) {return this.debouncedRemote(val)}- },+ }},methods: {- onClickAway(event) {+ onClickAway (event) {// Check that event target isn't children of dropdownif (this.$refs.select && !this.$refs.select.contains(event.target)) {this.isOpen = false}},- isSelected(value) {+ isSelected (value) {if (!this.modelValue) return falseif (this.emitKey && value[this.emitKey]) {@@ -264,17 +263,17 @@ export default {}return this.modelValue === value},- toggleDropdown() {+ toggleDropdown () {if (this.disabled) {this.isOpen = false} else {this.isOpen = !this.isOpen}if (!this.isOpen) {- this.searchTerm = ""+ this.searchTerm = ''}},- select(value) {+ select (value) {if (!this.multiple) {// Close after selectthis.toggleDropdown()@@ -285,48 +284,43 @@ export default {}if (this.multiple) {- const emitValue = Array.isArray(this.modelValue)- ? [...this.modelValue]- : []+ const emitValue = Array.isArray(this.modelValue) ? [...this.modelValue] : []if (this.isSelected(value)) {- this.$emit(- "update:modelValue",- emitValue.filter((item) => {- if (this.emitKey) {- return item !== value- }- return (- item[this.optionKey] !== value &&- item[this.optionKey] !== value[this.optionKey]- )- }),- )+ this.$emit('update:modelValue', emitValue.filter((item) => {+ if (this.emitKey) {+ return item !== value+ }+ return item[this.optionKey] !== value && item[this.optionKey] !== value[this.optionKey]+ }))return}emitValue.push(value)- this.$emit("update:modelValue", emitValue)+ this.$emit('update:modelValue', emitValue)} else {if (this.modelValue === value) {- this.$emit("update:modelValue", this.defaultValue ?? null)+ this.$emit('update:modelValue', this.defaultValue ?? null)} else {- this.$emit("update:modelValue", value)+ this.$emit('update:modelValue', value)}}},- createOption(newOption) {+ clear () {+ this.$emit('update:modelValue', this.multiple ? [] : null)+ },+ createOption (newOption) {if (newOption) {const newItem = {name: newOption,value: newOption,- id: newOption,+ id: newOption}- this.$emit("update-options", newItem)+ this.$emit('update-options', newItem)this.select(newItem)- this.searchTerm = ""+ this.searchTerm = ''}- },- },+ }+ }}</script>diff --git a/client/lib/forms/form-themes.js b/client/lib/forms/form-themes.jsindex da38cd4f1..230982dc0 100644--- a/client/lib/forms/form-themes.js+++ b/client/lib/forms/form-themes.js@@ -32,8 +32,10 @@ export const themes = {},SelectInput: {label: 'text-gray-700 dark:text-gray-300 font-semibold',- input: 'relative w-full rounded-lg border-gray-300 flex-1 appearance-none border border-gray-300 dark:border-gray-600 w-full px-4 bg-white text-gray-700 placeholder-gray-400 dark:bg-notion-dark-light dark:text-gray-300 dark:placeholder-gray-600 shadow-sm text-base focus:outline-none focus:ring-2 focus:border-transparent',- help: 'text-gray-400 dark:text-gray-500'+ input: 'relative w-full rounded-lg border-gray-300 flex-1 appearance-none border border-gray-300 dark:border-gray-600 w-full bg-white text-gray-700 placeholder-gray-400 dark:bg-notion-dark-light dark:text-gray-300 dark:placeholder-gray-600 shadow-sm text-base focus:outline-none focus:ring-2 focus:border-transparent',+ help: 'text-gray-400 dark:text-gray-500',+ dropdown: 'rounded-lg border border-gray-300 dark:border-gray-600',+ option: 'rounded-md'},ScaleInput: {label: 'text-gray-700 dark:text-gray-300 font-semibold',@@ -76,8 +78,10 @@ export const themes = {},SelectInput: {label: 'text-gray-700 dark:text-gray-300 font-semibold',- input: 'relative w-full flex-1 appearance-none border border-gray-300 dark:border-gray-600 w-full px-2 bg-white text-gray-700 placeholder-gray-400 dark:bg-notion-dark-light dark:text-gray-300 dark:placeholder-gray-600 text-base focus:outline-none focus:ring-2 focus:border-transparent',- help: 'text-gray-400 dark:text-gray-500'+ input: 'relative w-full flex-1 appearance-none border border-gray-300 dark:border-gray-600 w-full bg-white text-gray-700 placeholder-gray-400 dark:bg-notion-dark-light dark:text-gray-300 dark:placeholder-gray-600 text-base focus:outline-none focus:ring-2 focus:border-transparent',+ help: 'text-gray-400 dark:text-gray-500',+ dropdown: 'border border-gray-300 dark:border-gray-600',+ option: ''},CodeInput: {label: 'text-gray-700 dark:text-gray-300 font-semibold',@@ -131,7 +135,9 @@ export const themes = {SelectInput: {label: 'text-gray-900 dark:text-gray-100 mb-2 block mt-4',input: 'rounded relative w-full border-transparent flex-1 appearance-none bg-notion-input-background shadow-inner-notion w-full px-2 text-gray-900 placeholder-gray-400 dark:bg-notion-dark-light dark:placeholder-gray-500 text-base focus:outline-none focus:ring-0 focus:border-transparent focus:shadow-focus-notion',- help: 'text-notion-input-help dark:text-gray-500'+ help: 'text-notion-input-help dark:text-gray-500',+ dropdown: 'rounded border border-gray-300 dark:border-gray-600',+ option: 'rounded'},CodeInput: {label: 'text-gray-900 dark:text-gray-100 mb-2 block mt-4',diff --git a/client/package.json b/client/package.jsonindex 47bb8ece6..2a2235bef 100644--- a/client/package.json+++ b/client/package.json@@ -46,6 +46,7 @@"codemirror": "^6.0.1","crisp-sdk-web": "^1.0.21","date-fns": "^2.30.0",+ "debounce": "^1.2.1","fuse.js": "^6.4.6","js-sha256": "^0.10.0","libphonenumber-js": "^1.10.44",