-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(new-hope): add react hook form support in combobox
§
- Loading branch information
Showing
16 changed files
with
2,664 additions
and
72 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
7 changes: 7 additions & 0 deletions
7
packages/plasma-new-hope/src/components/Combobox/ComboboxNew/ui/Form/Form.styles.ts
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { styled } from '@linaria/react'; | ||
|
||
import { applyHidden } from '../../../../../mixins'; | ||
|
||
export const SelectHidden = styled.select` | ||
${applyHidden()}; | ||
`; |
33 changes: 33 additions & 0 deletions
33
packages/plasma-new-hope/src/components/Combobox/ComboboxNew/ui/Form/Form.tsx
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React, { ChangeEvent, forwardRef, useEffect, useRef } from 'react'; | ||
import { useForkRef } from '@salutejs/plasma-core'; | ||
|
||
import { createEvent } from '../../../../../utils/syntheticEvent'; | ||
import { ComboboxProps } from '../../Combobox.types'; | ||
|
||
import { SelectHidden } from './Form.styles'; | ||
|
||
type Props = Pick<ComboboxProps, 'name' | 'value' | 'multiple'> & { | ||
onChange: (value: ChangeEvent<HTMLSelectElement> | null) => void; | ||
}; | ||
|
||
export const Form = forwardRef<HTMLSelectElement, Props>(({ name, multiple, value, onChange }, ref) => { | ||
const values = (multiple ? value : [value]) as string[]; | ||
const selectRef = useRef<HTMLSelectElement | null>(null); | ||
const forkRef = useForkRef(selectRef, ref); | ||
|
||
useEffect(() => { | ||
const event = createEvent(selectRef); | ||
onChange && onChange(event); | ||
}, [values]); | ||
return ( | ||
<> | ||
<SelectHidden ref={forkRef} multiple={multiple} name={name} hidden value={multiple ? values : values[0]}> | ||
{values.map((v) => ( | ||
<option key={v} value={v}> | ||
{v} | ||
</option> | ||
))} | ||
</SelectHidden> | ||
</> | ||
); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { RefObject } from 'react'; | ||
|
||
export const createEvent = <T extends HTMLSelectElement | HTMLInputElement | HTMLTextAreaElement>( | ||
ref: RefObject<T>, | ||
) => { | ||
if (ref.current) { | ||
const event = new Event('change', { bubbles: true }); | ||
Object.defineProperty(event, 'target', { writable: false, value: ref.current }); | ||
const syntheticEvent = createSyntheticEvent(event) as React.ChangeEvent<typeof ref.current>; | ||
return syntheticEvent; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export const createSyntheticEvent = <T extends Element, E extends Event>(event: E): React.SyntheticEvent<T, E> => { | ||
let isDefaultPrevented = false; | ||
let isPropagationStopped = false; | ||
const preventDefault = () => { | ||
isDefaultPrevented = true; | ||
event.preventDefault(); | ||
}; | ||
const stopPropagation = () => { | ||
isPropagationStopped = true; | ||
event.stopPropagation(); | ||
}; | ||
return { | ||
nativeEvent: event, | ||
currentTarget: event.currentTarget as EventTarget & T, | ||
target: event.target as EventTarget & T, | ||
bubbles: event.bubbles, | ||
cancelable: event.cancelable, | ||
defaultPrevented: event.defaultPrevented, | ||
eventPhase: event.eventPhase, | ||
isTrusted: event.isTrusted, | ||
preventDefault, | ||
isDefaultPrevented: () => isDefaultPrevented, | ||
stopPropagation, | ||
isPropagationStopped: () => isPropagationStopped, | ||
persist: () => {}, | ||
timeStamp: event.timeStamp, | ||
type: event.type, | ||
}; | ||
}; |
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
Oops, something went wrong.