Update snippet descriptions
This commit is contained in:
@ -8,7 +8,7 @@ Renders an accordion menu with multiple collapsible content elements.
|
|||||||
- Define an `AccordionItem` component, that renders a `<button>` which is used to update the component and notify its parent via the `handleClick` callback.
|
- Define an `AccordionItem` component, that renders a `<button>` which is used to update the component and notify its parent via the `handleClick` callback.
|
||||||
- Use the `isCollapsed` prop in `AccordionItem` to determine its appearance and set an appropriate `className`.
|
- Use the `isCollapsed` prop in `AccordionItem` to determine its appearance and set an appropriate `className`.
|
||||||
- Define an `Accordion` component that uses the `useState()` hook to initialize the value of the `bindIndex` state variable to `defaultIndex`.
|
- Define an `Accordion` component that uses the `useState()` hook to initialize the value of the `bindIndex` state variable to `defaultIndex`.
|
||||||
- Filter `children` to remove unnecessary nodes expect for `AccordionItem` by identifying the function's name.
|
- Filter `children` to remove unnecessary nodes except for `AccordionItem` by identifying the function's name.
|
||||||
- Use `Array.prototype.map()` on the collected nodes to render the individual collapsiple elements.
|
- Use `Array.prototype.map()` on the collected nodes to render the individual collapsiple elements.
|
||||||
- Define `changeItem`, which will be executed when clicking an `AccordionItem`'s `<button>`.
|
- Define `changeItem`, which will be executed when clicking an `AccordionItem`'s `<button>`.
|
||||||
- `changeItem` executes the passed callback, `onItemClick`, and updates `bindIndex` based on the clicked element.
|
- `changeItem` executes the passed callback, `onItemClick`, and updates `bindIndex` based on the clicked element.
|
||||||
|
|||||||
@ -5,18 +5,17 @@ tags: components,input,state,beginner
|
|||||||
|
|
||||||
Renders a password input field with a reveal button.
|
Renders a password input field with a reveal button.
|
||||||
|
|
||||||
- Use the `React.useState()` hook to create the `shown` state variable and set its value to `false`.
|
- Use the `useState()` hook to create the `shown` state variable and set its value to `false`.
|
||||||
- Use a`<div>` to wrap both the`<input>` and the `<button>` element that toggles the type of the input field between `"text"` and `"password"`.
|
- When the `<button>` is clicked, execute `setShown`, toggling the `type` of the `<input>` between `"text"` and `"password"`.
|
||||||
|
|
||||||
```jsx
|
```jsx
|
||||||
const PasswordRevealer = ({ value }) => {
|
const PasswordRevealer = ({ value }) => {
|
||||||
const [shown, setShown] = React.useState(false);
|
const [shown, setShown] = React.useState(false);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<>
|
||||||
<input type={shown ? 'text' : 'password'} value={value} onChange={() => {}} />
|
<input type={shown ? 'text' : 'password'} value={value} />
|
||||||
<button onClick={() => setShown(!shown)}>Show/Hide</button>
|
<button onClick={() => setShown(!shown)}>Show/Hide</button>
|
||||||
</div>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|||||||
@ -3,24 +3,23 @@ title: Select
|
|||||||
tags: components,input,beginner
|
tags: components,input,beginner
|
||||||
---
|
---
|
||||||
|
|
||||||
Renders a `<select>` element that uses a callback function to pass its value to the parent component.
|
Renders an uncontrolled `<select>` element that uses a callback function to pass its value to the parent component.
|
||||||
|
|
||||||
- Use object destructuring to set defaults for certain attributes of the `<select>` element.
|
- Use the the `selectedValue` prop as the `defaultValue` of the `<select>` element to set its initial value..
|
||||||
- Render a `<select>` element with the appropriate attributes and use the `callback` function in the `onChange` event to pass the value of the textarea to the parent.
|
- Use the `onChange` event to fire the `onValueChange` callback and send the new value to the parent.
|
||||||
- Use the `selected` attribute to define the `defaultValue` of the `<select>` element.
|
- Use `Array.prototype.map()` on the `values` array to create an `<option>` element for each passed value.
|
||||||
- Use destructuring on the `values` array to pass an array of `value` and `text` elements.
|
- Each item in `values` must be a 2-element array, where the first element is the `value` of the item and the second one is the displayed text for it.
|
||||||
|
|
||||||
```jsx
|
```jsx
|
||||||
const Select = ({ values, callback, disabled = false, readonly = false, selected }) => {
|
const Select = ({ values, onValueChange, selectedValue, ...rest }) => {
|
||||||
return (
|
return (
|
||||||
<select
|
<select
|
||||||
disabled={disabled}
|
defaultValue={selectedValue}
|
||||||
readOnly={readonly}
|
onChange={({ target: { value } }) => onValueChange(value)}
|
||||||
defaultValue={selected}
|
{...rest}
|
||||||
onChange={({ target: { value } }) => callback(value)}
|
|
||||||
>
|
>
|
||||||
{values.map(([value, text]) => (
|
{values.map(([value, text]) => (
|
||||||
<option value={value}>
|
<option key={value} value={value}>
|
||||||
{text}
|
{text}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
@ -34,10 +33,14 @@ const choices = [
|
|||||||
['grapefruit', 'Grapefruit'],
|
['grapefruit', 'Grapefruit'],
|
||||||
['lime', 'Lime'],
|
['lime', 'Lime'],
|
||||||
['coconut', 'Coconut'],
|
['coconut', 'Coconut'],
|
||||||
['mango', 'Mango']
|
['mango', 'Mango'],
|
||||||
];
|
];
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<Select values={choices} selected="lime" callback={val => console.log(val)} />,
|
<Select
|
||||||
|
values={choices}
|
||||||
|
selectedValue="lime"
|
||||||
|
onValueChange={val => console.log(val)}
|
||||||
|
/>,
|
||||||
document.getElementById('root')
|
document.getElementById('root')
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|||||||
@ -3,24 +3,36 @@ title: Slider
|
|||||||
tags: components,input,beginner
|
tags: components,input,beginner
|
||||||
---
|
---
|
||||||
|
|
||||||
Renders a slider element that uses a callback function to pass its value to the parent component.
|
Renders an uncontrolled range input element that uses a callback function to pass its value to the parent component.
|
||||||
|
|
||||||
- Use object destructuring to set defaults for certain attributes of the `<input>` element.
|
- Set the `type` of the `<input>` element to `"range"` to create a slider.
|
||||||
- Render an `<input>` element of type `"range"` and the appropriate attributes, use the `callback` function in the `onChange` event to pass the value of the input to the parent.
|
- Use the `defaultValue` passed down from the parent as the uncontrolled input field's initial value.
|
||||||
|
- Use the `onChange` event to fire the `onValueChange` callback and send the new value to the parent.
|
||||||
|
|
||||||
```jsx
|
```jsx
|
||||||
const Slider = ({ callback, disabled = false, readOnly = false }) => {
|
const Slider = ({
|
||||||
|
min = 0,
|
||||||
|
max = 100,
|
||||||
|
defaultValue,
|
||||||
|
onValueChange,
|
||||||
|
...rest
|
||||||
|
}) => {
|
||||||
return (
|
return (
|
||||||
<input
|
<input
|
||||||
type="range"
|
type="range"
|
||||||
disabled={disabled}
|
min={min}
|
||||||
readOnly={readOnly}
|
max={max}
|
||||||
onChange={({ target: { value } }) => callback(value)}
|
defaultValue={defaultValue}
|
||||||
|
onChange={({ target: { value } }) => onValueChange(value)}
|
||||||
|
{...rest}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
```jsx
|
```jsx
|
||||||
ReactDOM.render(<Slider callback={val => console.log(val)} />, document.getElementById('root'));
|
ReactDOM.render(
|
||||||
|
<Slider onValueChange={val => console.log(val)} />,
|
||||||
|
document.getElementById('root')
|
||||||
|
);
|
||||||
```
|
```
|
||||||
|
|||||||
@ -5,11 +5,11 @@ tags: components,state,children,intermediate
|
|||||||
|
|
||||||
Renders a tabbed menu and view component.
|
Renders a tabbed menu and view component.
|
||||||
|
|
||||||
- Define a `TabItem` component, pass it to the `Tab` and remove unnecessary nodes expect for `TabItem` by identifying the function's name in `children`.
|
- Define a `Tabs` component that uses the `useState()` hook to initialize the value of the `bindIndex` state variable to `defaultIndex`.
|
||||||
- Use the `React.useState()` hook to initialize the value of the `bindIndex` state variable to `defaultIndex`.
|
- Define a `TabItem` component and filter `children` passed to the `Tabs` component to remove unnecessary nodes except for `TabItem` by identifying the function's name.
|
||||||
- Use `Array.prototype.map()` on the collected nodes to render the `tab-menu` and `tab-view`.
|
- Define `changeTab`, which will be executed when clicking a `<button>` from the menu.
|
||||||
- Define `changeTab`, which will be executed when clicking a `<button>` from the `tab-menu`.
|
- `changeTab` executes the passed callback, `onTabClick`, and updates `bindIndex` based on the clicked element.
|
||||||
- `changeTab` executes the passed callback, `onTabClick` and updates `bindIndex`, which in turn causes a re-render, evaluating the `style` and `className` of the `tab-view` items and `tab-menu` buttons according to their `index`.
|
- Use `Array.prototype.map()` on the collected nodes to render the menu and view of the tabs, using the value of `binIndex` to determine the active tab and apply the correct `className`.
|
||||||
|
|
||||||
```css
|
```css
|
||||||
.tab-menu > button {
|
.tab-menu > button {
|
||||||
@ -19,23 +19,31 @@ Renders a tabbed menu and view component.
|
|||||||
border-bottom: 2px solid transparent;
|
border-bottom: 2px solid transparent;
|
||||||
background: none;
|
background: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.tab-menu > button.focus {
|
.tab-menu > button.focus {
|
||||||
border-bottom: 2px solid #007bef;
|
border-bottom: 2px solid #007bef;
|
||||||
}
|
}
|
||||||
|
|
||||||
.tab-menu > button:hover {
|
.tab-menu > button:hover {
|
||||||
border-bottom: 2px solid #007bef;
|
border-bottom: 2px solid #007bef;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.tab-content {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tab-content.selected {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```jsx
|
```jsx
|
||||||
const TabItem = props => {
|
const TabItem = props => <div {...props} />;
|
||||||
return <div {...props} />;
|
|
||||||
};
|
|
||||||
|
|
||||||
const Tabs = ({ defaultIndex = 0, onTabClick, children }) => {
|
const Tabs = ({ defaultIndex = 0, onTabClick, children }) => {
|
||||||
const [bindIndex, setBindIndex] = React.useState(defaultIndex);
|
const [bindIndex, setBindIndex] = React.useState(defaultIndex);
|
||||||
const changeTab = newIndex => {
|
const changeTab = newIndex => {
|
||||||
if (typeof onTabClick === 'function') onTabClick(newIndex);
|
if (typeof onItemClick === 'function') onItemClick(itemIndex);
|
||||||
setBindIndex(newIndex);
|
setBindIndex(newIndex);
|
||||||
};
|
};
|
||||||
const items = children.filter(item => item.type.name === 'TabItem');
|
const items = children.filter(item => item.type.name === 'TabItem');
|
||||||
@ -44,7 +52,11 @@ const Tabs = ({ defaultIndex = 0, onTabClick, children }) => {
|
|||||||
<div className="wrapper">
|
<div className="wrapper">
|
||||||
<div className="tab-menu">
|
<div className="tab-menu">
|
||||||
{items.map(({ props: { index, label } }) => (
|
{items.map(({ props: { index, label } }) => (
|
||||||
<button onClick={() => changeTab(index)} className={bindIndex === index ? 'focus' : ''}>
|
<button
|
||||||
|
key={`tab-btn-${index}`}
|
||||||
|
onClick={() => changeTab(index)}
|
||||||
|
className={bindIndex === index ? 'focus' : ''}
|
||||||
|
>
|
||||||
{label}
|
{label}
|
||||||
</button>
|
</button>
|
||||||
))}
|
))}
|
||||||
@ -53,9 +65,10 @@ const Tabs = ({ defaultIndex = 0, onTabClick, children }) => {
|
|||||||
{items.map(({ props }) => (
|
{items.map(({ props }) => (
|
||||||
<div
|
<div
|
||||||
{...props}
|
{...props}
|
||||||
className="tab-view_item"
|
className={`tab-content ${
|
||||||
key={props.index}
|
bindIndex === props.index ? 'selected' : ''
|
||||||
style={{ display: bindIndex === props.index ? 'block' : 'none' }}
|
}`}
|
||||||
|
key={`tab-content-${props.index}`}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -3,28 +3,26 @@ title: TextArea
|
|||||||
tags: components,input,beginner
|
tags: components,input,beginner
|
||||||
---
|
---
|
||||||
|
|
||||||
Renders a `<textarea>` element that uses a callback function to pass its value to the parent component.
|
Renders an uncontrolled `<textarea>` element that uses a callback function to pass its value to the parent component.
|
||||||
|
|
||||||
- Use object destructuring to set defaults for certain attributes of the `<textarea>` element.
|
- Use the `defaultValue` passed down from the parent as the uncontrolled input field's initial value.
|
||||||
- Render a `<textarea>` element with the appropriate attributes and use the `callback` function in the `onChange` event to pass the value of the textarea to the parent.
|
- Use the `onChange` event to fire the `onValueChange` callback and send the new value to the parent.
|
||||||
|
|
||||||
```jsx
|
```jsx
|
||||||
const TextArea = ({
|
const TextArea = ({
|
||||||
callback,
|
|
||||||
cols = 20,
|
cols = 20,
|
||||||
rows = 2,
|
rows = 2,
|
||||||
disabled = false,
|
defaultValue,
|
||||||
readOnly = false,
|
onValueChange,
|
||||||
placeholder = ''
|
...rest
|
||||||
}) => {
|
}) => {
|
||||||
return (
|
return (
|
||||||
<textarea
|
<textarea
|
||||||
cols={cols}
|
cols={cols}
|
||||||
rows={rows}
|
rows={rows}
|
||||||
disabled={disabled}
|
defaultValue={defaultValue}
|
||||||
readOnly={readOnly}
|
onChange={({ target: { value } }) => onValueChange(value)}
|
||||||
placeholder={placeholder}
|
{...rest}
|
||||||
onChange={({ target: { value } }) => callback(value)}
|
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@ -32,7 +30,10 @@ const TextArea = ({
|
|||||||
|
|
||||||
```jsx
|
```jsx
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<TextArea placeholder="Insert some text here..." callback={val => console.log(val)} />,
|
<TextArea
|
||||||
|
placeholder="Insert some text here..."
|
||||||
|
onValueChange={val => console.log(val)}
|
||||||
|
/>,
|
||||||
document.getElementById('root')
|
document.getElementById('root')
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user