({
label, icon, checkDirtyOnEnableSave, onClick, mode,
})
| 15 | |
| 16 | |
| 17 | export function SaveButton({ |
| 18 | label, icon, checkDirtyOnEnableSave, onClick, mode, |
| 19 | }) { |
| 20 | const [key, setKey] = useState(0); |
| 21 | const schemaState = useContext(SchemaStateContext); |
| 22 | const checkDisabled = (state) => { |
| 23 | const {isDirty, isSaving, errors} = state; |
| 24 | return ( |
| 25 | isSaving || |
| 26 | !(mode === 'edit' || checkDirtyOnEnableSave ? isDirty : true) || |
| 27 | Boolean(errors.name) |
| 28 | ); |
| 29 | }; |
| 30 | const currState = schemaState.state(); |
| 31 | const isDisabled = checkDisabled(currState); |
| 32 | |
| 33 | useEffect(() => { |
| 34 | if (!schemaState) return; |
| 35 | |
| 36 | const refreshOnDisableStateChanged = (newState) => { |
| 37 | if (isDisabled !== checkDisabled(newState)) setKey(Date.now()); |
| 38 | }; |
| 39 | |
| 40 | return schemaState.subscribe([], refreshOnDisableStateChanged, 'states'); |
| 41 | }, [key]); |
| 42 | |
| 43 | return ( |
| 44 | <PrimaryButton |
| 45 | data-test='Save' onClick={onClick} startIcon={icon} |
| 46 | disabled={isDisabled} |
| 47 | > |
| 48 | {label} |
| 49 | </PrimaryButton> |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | SaveButton.propTypes = { |
| 54 | label: PropTypes.string, |
nothing calls this directly
no test coverage detected