SearchBar.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <template>
  2. <FlexRow
  3. center
  4. :innerStyle="{
  5. ...themeStyles.view.value,
  6. ...innerStyle,
  7. background: theme.resolveThemeColor(inputBackgroundColor),
  8. }"
  9. :flexShrink="0"
  10. overflow="hidden"
  11. >
  12. <FlexRow
  13. center
  14. :innerStyle="{
  15. ...themeStyles.input.value,
  16. ...containerStyle
  17. }"
  18. :gap="20"
  19. >
  20. <slot name="leftIcon">
  21. <Icon
  22. v-if="leftIcon"
  23. :icon="leftIcon"
  24. :size="leftIconSize"
  25. :color="leftIconColor"
  26. v-bind="leftIconProps"
  27. />
  28. </slot>
  29. <input
  30. :style="{
  31. ...themeStyles.inputInner.value,
  32. ...inputStyle,
  33. color: theme.resolveThemeColor(inputColor)
  34. }"
  35. :value="valueTemp"
  36. :placeholder="placeholder"
  37. :placeholder-style="`color: ${theme.resolveThemeColor(placeholderTextColor)}`"
  38. type="text"
  39. confirm-type="search"
  40. @input="onInput"
  41. @focus="onFocus"
  42. @blur="onBlur"
  43. @confirm="onSumit"
  44. />
  45. <slot name="rightIcon" />
  46. </FlexRow>
  47. <slot
  48. v-if="(
  49. cancelState === 'show' ||
  50. (inputFocus && (cancelState === 'show-active' || cancelState === 'show-active-or-no-empty')) ||
  51. (valueTemp !== '' && (cancelState === 'show-no-empty' || cancelState === 'show-active-or-no-empty'))
  52. )"
  53. name="cancelButton"
  54. >
  55. <Button
  56. type="text"
  57. size="small"
  58. :text="cancelText"
  59. :textStyle="{
  60. color: theme.resolveThemeColor(cancelTextColor),
  61. ...cancelTextStyle
  62. }"
  63. :innerStyle="{ ...themeStyles.cancel.value, ...cancelStyle }"
  64. @click="onCancelPressed"
  65. />
  66. </slot>
  67. <slot
  68. v-if="(
  69. searchState === 'show' ||
  70. (inputFocus && (searchState === 'show-active' || searchState === 'show-active-or-no-empty')) ||
  71. (valueTemp !== '' && (searchState === 'show-no-empty' || searchState === 'show-active-or-no-empty'))
  72. )"
  73. name="searchButton"
  74. :onSearchButtonClick="onSearchButtonClick"
  75. >
  76. <IconButton
  77. :icon="searchIcon"
  78. :size="searchIconSize"
  79. :color="searchIconColor"
  80. v-bind="searchIconProps"
  81. @click="onSearchButtonClick"
  82. />
  83. </slot>
  84. <slot name="rightButton" :onCustomButtonClick="onCustomButtonClick"/>
  85. </FlexRow>
  86. </template>
  87. <script setup lang="ts">
  88. import { ref, watch } from 'vue';
  89. import type { IconProps } from '../basic/Icon.vue';
  90. import { type ViewStyle, type TextStyle, useTheme, propGetThemeVar } from '../theme/ThemeDefine';
  91. import FlexRow from '../layout/FlexRow.vue';
  92. import Button from '../basic/Button.vue';
  93. import { DynamicSize } from '../theme/ThemeTools';
  94. import Icon from '../basic/Icon.vue';
  95. import IconButton from '../basic/IconButton.vue';
  96. export interface SearchBarProp {
  97. /**
  98. * 输入框初始值
  99. */
  100. modelValue?: string;
  101. /**
  102. * 输入框的文字颜色
  103. * @default text.content
  104. */
  105. inputColor?: string;
  106. /**
  107. * 输入框的背景颜色
  108. * @default white
  109. */
  110. inputBackgroundColor?: string;
  111. /**
  112. * 输入框的placeholder
  113. */
  114. placeholder?: string;
  115. /**
  116. * 输入框的placeholderTextColor
  117. * @default text.second
  118. */
  119. placeholderTextColor?: string;
  120. /**
  121. * 取消按钮的文字
  122. * @default '取消'
  123. */
  124. cancelText?: string;
  125. /**
  126. * 取消按钮的文字颜色
  127. * @default primary
  128. */
  129. cancelTextColor?: string;
  130. /**
  131. * 点击取消按钮是否自动让输入框失去焦点
  132. * @default true
  133. */
  134. cancalLostFocus?: boolean;
  135. /**
  136. * 点击取消按钮是否自动清空输入框
  137. * @default true
  138. */
  139. cancalClear?: boolean;
  140. /**
  141. * 左侧图标
  142. * @default 'search'
  143. */
  144. leftIcon?: string|null;
  145. leftIconSize?: string|number;
  146. leftIconColor?: string;
  147. /**
  148. * 图标自定义属性
  149. */
  150. leftIconProps?: Partial<IconProps>;
  151. /**
  152. * 取消按钮显示
  153. * * hidden 不显示
  154. * * show 一直显示
  155. * * show-active 当输入框激活时显示
  156. * * show-no-empty 当输入框有文字时显示
  157. * * show-active-or-no-empty 当输入框激活或者有文字时显示
  158. * @default 'show-active-or-no-empty'
  159. */
  160. cancelState?: 'hidden'|'show'|'show-active'|'show-no-empty'|'show-active-or-no-empty';
  161. /**
  162. * 搜索按钮显示
  163. * * hidden 不显示
  164. * * show 一直显示
  165. * * show-active 当输入框激活时显示
  166. * * show-no-empty 当输入框有文字时显示
  167. * * show-active-or-no-empty 当输入框激活或者有文字时显示
  168. * @default 'hidden'
  169. */
  170. searchState?: 'hidden'|'show'|'show-active'|'show-no-empty'|'show-active-or-no-empty';
  171. /**
  172. * 搜索按钮图标
  173. * @default 'search'
  174. */
  175. searchIcon?: string;
  176. /**
  177. * 搜索按钮图标大小
  178. * @default 40
  179. */
  180. searchIconSize?: string|number;
  181. /**
  182. * 搜索按钮图标颜色
  183. * @default text.content
  184. */
  185. searchIconColor?: string;
  186. /**
  187. * 图标自定义属性
  188. */
  189. searchIconProps?: Partial<IconProps>;
  190. /**
  191. * 自定义样式
  192. */
  193. innerStyle?: ViewStyle;
  194. /**
  195. * 容器自定义样式
  196. */
  197. containerStyle?: ViewStyle;
  198. /**
  199. * 自定义样式
  200. */
  201. inputStyle?: TextStyle;
  202. /**
  203. * 自定义样式
  204. */
  205. cancelStyle?: ViewStyle;
  206. /**
  207. * 自定义样式
  208. */
  209. cancelTextStyle?: TextStyle;
  210. }
  211. const emit = defineEmits([ 'search', 'update:modelValue', 'blur', 'focus', 'cancel' ])
  212. const theme = useTheme();
  213. const props = withDefaults(defineProps<SearchBarProp>(), {
  214. inputColor: () => propGetThemeVar('SearchBarTextColor', 'text.content'),
  215. inputBackgroundColor: () => propGetThemeVar('SearchBarBackgroundColor', 'white'),
  216. searchState: 'hidden',
  217. searchIcon: () => propGetThemeVar('SearchBarSearchIcon', 'search'),
  218. searchIconSize: () => propGetThemeVar('SearchBarSearchIconSize', 40),
  219. searchIconColor: () => propGetThemeVar('SearchBarSearchIconColor', 'text.content'),
  220. placeholder: '',
  221. placeholderTextColor: () => propGetThemeVar('SearchBarCancelTextColor', 'text.second'),
  222. cancelText: '取消',
  223. cancelTextColor: () => propGetThemeVar('SearchBarCancelTextColor', 'primary'),
  224. cancalLostFocus: true,
  225. cancalClear: true,
  226. leftIcon: () => propGetThemeVar('SearchBarLeftIcon', 'search'),
  227. leftIconSize: () => propGetThemeVar('SearchBarLeftIconSize', 40),
  228. leftIconColor: () => propGetThemeVar('SearchBarLeftIconColor', 'text.content'),
  229. cancelState: 'show-active-or-no-empty',
  230. });
  231. const valueTemp = ref(props.modelValue ?? '');
  232. watch(() => props.modelValue, () => {
  233. valueTemp.value = props.modelValue ?? '';
  234. });
  235. function updateValue(v: string) {
  236. valueTemp.value = v;
  237. emit('update:modelValue', v);
  238. }
  239. const themeStyles = theme.useThemeStyles({
  240. view: {
  241. position: 'relative',
  242. borderRadius: DynamicSize('SearchBarBorderRadius', 40),
  243. paddingHorizontal: DynamicSize('SearchBarPaddingHorizontal', 28),
  244. paddingVertical: DynamicSize('SearchBarPaddingVertical', 16),
  245. },
  246. input: {
  247. flex: 1,
  248. position: 'relative',
  249. },
  250. inputInner: {
  251. flex: 1,
  252. paddingHorizontal: DynamicSize('SearchBarCancelPaddingHorizontal', 20),
  253. paddingVertical: DynamicSize('SearchBarCancelPaddingVertical', 16),
  254. },
  255. cancel: {
  256. paddingTop: DynamicSize('SearchBarCancelPaddingVertical', 0),
  257. paddingBottom: DynamicSize('SearchBarCancelPaddingVertical', 0),
  258. paddingHorizontal: DynamicSize('SearchBarCancelPaddingHorizontal', 20),
  259. },
  260. });
  261. const inputFocus = ref(false);
  262. function onInput(e: any) {
  263. inputFocus.value = true;
  264. updateValue(e.detail.value);
  265. }
  266. function onFocus() {
  267. inputFocus.value = true;
  268. emit('focus');
  269. }
  270. function onBlur() {
  271. inputFocus.value = false;
  272. emit('blur');
  273. }
  274. function onCancelPressed() {
  275. if (props.cancalLostFocus)
  276. uni.hideKeyboard()
  277. if (props.cancalClear)
  278. updateValue('');
  279. emit('cancel');
  280. }
  281. function onSearchButtonClick() {
  282. emit('search', valueTemp.value);
  283. uni.hideKeyboard()
  284. }
  285. function onSumit() {
  286. emit('search', valueTemp.value);
  287. uni.hideKeyboard()
  288. }
  289. function onCustomButtonClick(type: 'search'|'cancel') {
  290. if (type === 'search') onSumit();
  291. else if (type === 'cancel') onCancelPressed();
  292. }
  293. </script>