增加对于默认表单值的处理。
This commit is contained in:
parent
9fa05824d2
commit
838f0c0fa0
|
@ -7,14 +7,14 @@ import styles from './FloatColorPicker.module.css';
|
||||||
type FloatColorPickerProps = {
|
type FloatColorPickerProps = {
|
||||||
name?: string;
|
name?: string;
|
||||||
color?: string;
|
color?: string;
|
||||||
onPick?: (color: string) => void;
|
onPick?: (color: string | null | undefined) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function FloatColorPicker({ name, color, onPick }: FloatColorPickerProps) {
|
export function FloatColorPicker({ name, color, onPick }: FloatColorPickerProps) {
|
||||||
const [pickedColor, setPicked] = useState<string | null>(color ?? null);
|
const [pickedColor, setPicked] = useState<string | null>(color ?? null);
|
||||||
const [showPicker, setPickerShow] = useState(false);
|
const [showPicker, setPickerShow] = useState(false);
|
||||||
const handlePickAction = useCallback(
|
const handlePickAction = useCallback(
|
||||||
(value: string | null) => {
|
(value: string) => {
|
||||||
setPicked(value);
|
setPicked(value);
|
||||||
onPick?.(value);
|
onPick?.(value);
|
||||||
},
|
},
|
||||||
|
@ -42,7 +42,7 @@ export function FloatColorPicker({ name, color, onPick }: FloatColorPickerProps)
|
||||||
</div>
|
</div>
|
||||||
{showPicker && (
|
{showPicker && (
|
||||||
<div className={styles.picker}>
|
<div className={styles.picker}>
|
||||||
<ColorPicker color={pickedColor} onSelect={handlePickAction} />
|
<ColorPicker color={pickedColor ?? null} onSelect={handlePickAction} />
|
||||||
<div className={styles.btns}>
|
<div className={styles.btns}>
|
||||||
<button type="button" className="primary" onClick={() => setPickerShow(false)}>
|
<button type="button" className="primary" onClick={() => setPickerShow(false)}>
|
||||||
Done
|
Done
|
||||||
|
@ -50,7 +50,7 @@ export function FloatColorPicker({ name, color, onPick }: FloatColorPickerProps)
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
{!isNil(name) && <input type="hidden" name={name} value={pickedColor} />}
|
{!isNil(name) && <input type="hidden" name={name} value={pickedColor ?? ''} />}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
16
src/utls.ts
Normal file
16
src/utls.ts
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
import { isEmpty, isNil } from 'lodash-es';
|
||||||
|
|
||||||
|
export function defaultEmptyFormData<D>(formData: FormData, param: string, defaultValue: D): D {
|
||||||
|
const value = formData.get(param);
|
||||||
|
if (isNil(value) || isEmpty(value)) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function defaultEmptyValue<T, D>(value: T, defaultValue: D): T | D {
|
||||||
|
if (isNil(value) || isEmpty(value)) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user