({label, icon, onClick})
| 15 | |
| 16 | |
| 17 | export function ResetButton({label, icon, onClick}) { |
| 18 | const [key, setKey] = useState(0); |
| 19 | const schemaState = useContext(SchemaStateContext); |
| 20 | const checkDisabled = (state) => (state.isSaving || !state.isDirty); |
| 21 | const currState = schemaState.state(); |
| 22 | const isDisabled = checkDisabled(currState); |
| 23 | |
| 24 | useEffect(() => { |
| 25 | if (!schemaState) return; |
| 26 | |
| 27 | const refreshOnDisableStateChanged = (newState) => { |
| 28 | if (isDisabled !== checkDisabled(newState)) setKey(Date.now()); |
| 29 | }; |
| 30 | |
| 31 | return schemaState.subscribe([], refreshOnDisableStateChanged, 'states'); |
| 32 | }, [key]); |
| 33 | |
| 34 | return ( |
| 35 | <DefaultButton |
| 36 | data-test='Reset' onClick={onClick} |
| 37 | startIcon={icon} |
| 38 | disabled={isDisabled} |
| 39 | className='Dialog-buttonMargin'> |
| 40 | { label } |
| 41 | </DefaultButton> |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | ResetButton.propTypes = { |
| 46 | label: PropTypes.string, |
nothing calls this directly
no test coverage detected