Skip to content

Commit

Permalink
fix(Form): 修复search组件报错
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenlingasMx committed Mar 3, 2023
1 parent d6a36be commit 4b75fa0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
13 changes: 8 additions & 5 deletions example/examples/src/routes/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Form/formItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const FormItems = ({ schema = [] }: Pick<FormProps, 'schema'>) => {

const change = (field: KeyType, value: unknown) => {
updateStore?.({ store: { ...store, [field]: value } });
watch[field]?.(value);
watch && watch[field]?.(value);
};

const _renderComponent = (v: FormItemsProps) => {
Expand Down
22 changes: 12 additions & 10 deletions packages/core/src/SearchBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<ViewStyle>;
Expand Down Expand Up @@ -55,16 +55,18 @@ function SearchBar({
}: SearchBarProps) {
const [curValue, setCurValue] = useState<any>(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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 4b75fa0

Please sign in to comment.