-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
158 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 89 additions & 17 deletions
106
react-hook-form-mui/src/react-hook-form/MultiButtonGroup.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,102 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import {useController} from 'react-hook-form'; | ||
import {useRMController} from './useRMController'; | ||
import ButtonGroup from '../forms/ButtonGroup'; | ||
|
||
export default function RHFButtonGroup({control, name, ...rest}) { | ||
function getNextOptions({ | ||
values, | ||
options, | ||
}) { | ||
if (!values.length) return null; | ||
|
||
for (let i = 0; i < options.length; i++) { | ||
const matches = options[i].value.startsWith(values[0]); | ||
if (!matches) continue; | ||
return options[i].options; | ||
} | ||
return null; | ||
} | ||
|
||
function RecursiveButtonGroup({ | ||
values, | ||
options, | ||
onChange, | ||
onBlur, | ||
...rest | ||
}) { | ||
if (!values.length) return null; | ||
|
||
const nextOptions = getNextOptions({values, options}); | ||
if (!nextOptions) return null; | ||
const nextToNextOptions = getNextOptions({options: nextOptions, values: values.slice(1)}); | ||
return ( | ||
<> | ||
<ButtonGroup | ||
exclusive | ||
onBlur={onBlur} | ||
onChange={onChange} | ||
options={nextOptions} | ||
value={(values[1] + (nextToNextOptions ? '-' : '')) || values[0]} | ||
{...rest} | ||
/> | ||
<RecursiveButtonGroup {...{options: nextOptions, values: values.slice(1), onChange, onBlur}}/> | ||
</> | ||
); | ||
} | ||
|
||
RecursiveButtonGroup.propTypes = { | ||
onBlur: PropTypes.func, | ||
onChange: PropTypes.func, | ||
options: PropTypes.array, | ||
values: PropTypes.array, | ||
}; | ||
|
||
function getAllValues(value, startIndex = 0) { | ||
if (!value || (startIndex >= value.length)) return []; | ||
|
||
const ind = value.indexOf(':', startIndex); | ||
if (ind === -1) { | ||
const v = value.replace(/-$/, ''); | ||
return [ | ||
v, | ||
...(v !== value ? [] : [v]), | ||
]; | ||
} | ||
return [ | ||
value.substr(0, ind), | ||
...getAllValues(value, ind + 1), | ||
]; | ||
} | ||
|
||
// eslint-disable-next-line react/no-multi-comp | ||
export default function RHFMultiButtonGroup(props) { | ||
const { | ||
field: {ref, ...inputProps}, | ||
} = useController({ | ||
name, | ||
control, | ||
rules: {required: rest.required}, | ||
defaultValue: rest.defaultValue, | ||
}); | ||
// eslint-disable-next-line no-unused-vars | ||
fieldState, | ||
// eslint-disable-next-line no-unused-vars | ||
formState, | ||
...p | ||
} = useRMController(props); | ||
|
||
const {options} = props; | ||
const {value, onChange, onBlur} = p; | ||
|
||
const values = getAllValues(value); | ||
|
||
return ( | ||
<ButtonGroup | ||
aria-label='text alignment' | ||
exclusive | ||
ref={ref} | ||
{...inputProps} | ||
{...rest} | ||
/> | ||
<> | ||
<ButtonGroup | ||
exclusive | ||
{...p} | ||
value={values[0] + '-'} | ||
/> | ||
<RecursiveButtonGroup {...{options, values, onChange, onBlur}}/> | ||
</> | ||
); | ||
} | ||
|
||
RHFButtonGroup.propTypes = { | ||
RHFMultiButtonGroup.propTypes = { | ||
control: PropTypes.object, | ||
name: PropTypes.string, | ||
options: PropTypes.array, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters