Form/Common: disable save button if form values are not changed
This commit is contained in:
parent
381ba678d9
commit
be938b4d62
@ -9,7 +9,7 @@ import { useCallback } from 'react';
|
|||||||
import { useContext } from 'react';
|
import { useContext } from 'react';
|
||||||
|
|
||||||
export function Buttons() {
|
export function Buttons() {
|
||||||
const { reset, resetValidation, setValidation, values } = useFormStore();
|
const { reset, resetValidation, setValidation, status, values } = useFormStore();
|
||||||
const { pageUrlParams, setFormState } = useContext(FormContext);
|
const { pageUrlParams, setFormState } = useContext(FormContext);
|
||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@ -69,7 +69,9 @@ export function Buttons() {
|
|||||||
<Button intent="outline-secondary" onClick={handleRetract}>
|
<Button intent="outline-secondary" onClick={handleRetract}>
|
||||||
Возврат на доработку
|
Возврат на доработку
|
||||||
</Button>
|
</Button>
|
||||||
<Button onClick={handleSave}>Сохранить</Button>
|
<Button onClick={handleSave} disabled={status !== 'edited'}>
|
||||||
|
Сохранить
|
||||||
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,7 @@ type FormState = {
|
|||||||
resetValidation: () => void;
|
resetValidation: () => void;
|
||||||
setValidation: (input: { name: string } & ElementValidation) => void;
|
setValidation: (input: { name: string } & ElementValidation) => void;
|
||||||
setValue: ({ name, value }: { name: string; value: Values[number] }) => void;
|
setValue: ({ name, value }: { name: string; value: Values[number] }) => void;
|
||||||
|
status?: 'init' | 'edited';
|
||||||
validation: Record<string, ElementValidation | undefined>;
|
validation: Record<string, ElementValidation | undefined>;
|
||||||
values: Values;
|
values: Values;
|
||||||
};
|
};
|
||||||
@ -24,10 +25,12 @@ export const useFormStore = create<FormState>((set) => ({
|
|||||||
init: (values) =>
|
init: (values) =>
|
||||||
set(() => ({
|
set(() => ({
|
||||||
defaultValues: values,
|
defaultValues: values,
|
||||||
|
status: 'init',
|
||||||
values,
|
values,
|
||||||
})),
|
})),
|
||||||
reset: () =>
|
reset: () =>
|
||||||
set((state) => ({
|
set((state) => ({
|
||||||
|
status: 'init',
|
||||||
validation: {},
|
validation: {},
|
||||||
values: state.defaultValues,
|
values: state.defaultValues,
|
||||||
})),
|
})),
|
||||||
@ -44,6 +47,7 @@ export const useFormStore = create<FormState>((set) => ({
|
|||||||
})),
|
})),
|
||||||
setValue: ({ name, value }) =>
|
setValue: ({ name, value }) =>
|
||||||
set((state) => ({
|
set((state) => ({
|
||||||
|
status: 'edited',
|
||||||
validation: {
|
validation: {
|
||||||
...state.validation,
|
...state.validation,
|
||||||
[name]: undefined,
|
[name]: undefined,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user