diff --git a/example/examples/src/routes/Form/index.tsx b/example/examples/src/routes/Form/index.tsx index 23bffc663..e5dd3e644 100644 --- a/example/examples/src/routes/Form/index.tsx +++ b/example/examples/src/routes/Form/index.tsx @@ -75,16 +75,19 @@ const FormDemo = () => { }, { type: 'search', - field: 'search', - name: '搜索', + field: 'project', + required: false, + name: '项目', options: [ - {label: '上海', value: 1}, - {label: '南京', value: 2}, + {label: '测试系统管理', value: 1}, + {label: '后台管理', value: 2}, ], attr: { - labelInValue: true, showClear: true, + labelInValue: true, }, + placeholder: '请输入', + validate: (val: any) => (!val ? '请输入项目' : ''), }, { type: 'stepper', diff --git a/packages/core/src/Form/formItems.tsx b/packages/core/src/Form/formItems.tsx index 78ff42e1b..19cec5003 100644 --- a/packages/core/src/Form/formItems.tsx +++ b/packages/core/src/Form/formItems.tsx @@ -20,7 +20,7 @@ const FormItems = ({ schema = [] }: Pick) => { const change = (field: KeyType, value: unknown) => { updateStore?.({ store: { ...store, [field]: value } }); - watch[field]?.(value); + watch && watch[field]?.(value); }; const _renderComponent = (v: FormItemsProps) => { diff --git a/packages/core/src/SearchBar/index.tsx b/packages/core/src/SearchBar/index.tsx index ab5ec209b..b5af09e37 100644 --- a/packages/core/src/SearchBar/index.tsx +++ b/packages/core/src/SearchBar/index.tsx @@ -24,9 +24,9 @@ interface SearchBarProps { onBlur?: (e: any | string) => void; labelInValue?: Boolean; disabled?: Boolean; - value?: String; + value?: string | OptionsState; loading?: Boolean; - placeholder?: String; + placeholder?: string; extra?: JSX.Element; showClear?: boolean; contentStyle?: StyleProp; @@ -55,16 +55,18 @@ function SearchBar({ }: SearchBarProps) { const [curValue, setCurValue] = useState(value); const [visible, setVisivble] = useState(false); - const textValue = labelInValue ? curValue && curValue.label : curValue; + let textValue; + if (labelInValue) { + textValue = curValue?.label; + } else { + const { label }: any = options.find((item) => item.value === curValue); + textValue = label; + } useEffect(() => { setCurValue(value); }, [value]); - // useEffect(() => { - // visible && onFocus; - // }, [visible]); - // 搜索 const onHandleChangeText = (val: string) => { onChangeText && onChangeText(val); @@ -134,9 +136,9 @@ function SearchBar({ const selectValue: | any | { - key: string; - label: string; - } = labelInValue ? { key: itm.value, label: itm.label } : itm.value; + key: string; + label: string; + } = labelInValue ? { key: itm.value, label: itm.label } : itm.value; onChange && onChange(selectValue); setCurValue(selectValue); setVisivble(false);